Skip to content
Projects
Groups
Snippets
Help
Loading...
Sign in / Register
Toggle navigation
R
rapidjson
Project
Project
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Packages
Packages
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
submodule
rapidjson
Commits
fdc2b569
Commit
fdc2b569
authored
Aug 20, 2014
by
miloyip
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Optimize number parsing for 64-bit architecture
parent
1adeecb1
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
51 additions
and
24 deletions
+51
-24
rapidjson.h
include/rapidjson/rapidjson.h
+12
-0
reader.h
include/rapidjson/reader.h
+39
-24
No files found.
include/rapidjson/rapidjson.h
View file @
fdc2b569
...
@@ -147,6 +147,18 @@
...
@@ -147,6 +147,18 @@
# endif
# endif
#endif // RAPIDJSON_ENDIAN
#endif // RAPIDJSON_ENDIAN
///////////////////////////////////////////////////////////////////////////////
// RAPIDJSON_64BIT
//! Whether using 64-bit architecture
#ifndef RAPIDJSON_64BIT
#if defined(__LP64__) || defined(_WIN64)
#define RAPIDJSON_64BIT 1
#else
#define RAPIDJSON_64BIT 0
#endif
#endif // RAPIDJSON_64BIT
///////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////
// RAPIDJSON_ALIGN
// RAPIDJSON_ALIGN
...
...
include/rapidjson/reader.h
View file @
fdc2b569
...
@@ -760,7 +760,8 @@ private:
...
@@ -760,7 +760,8 @@ private:
// Parse int: zero / ( digit1-9 *DIGIT )
// Parse int: zero / ( digit1-9 *DIGIT )
unsigned
i
=
0
;
unsigned
i
=
0
;
bool
try64bit
=
false
;
uint64_t
i64
=
0
;
bool
use64bit
=
false
;
if
(
s
.
Peek
()
==
'0'
)
{
if
(
s
.
Peek
()
==
'0'
)
{
i
=
0
;
i
=
0
;
s
.
Take
();
s
.
Take
();
...
@@ -772,7 +773,8 @@ private:
...
@@ -772,7 +773,8 @@ private:
while
(
s
.
Peek
()
>=
'0'
&&
s
.
Peek
()
<=
'9'
)
{
while
(
s
.
Peek
()
>=
'0'
&&
s
.
Peek
()
<=
'9'
)
{
if
(
i
>=
214748364
)
{
// 2^31 = 2147483648
if
(
i
>=
214748364
)
{
// 2^31 = 2147483648
if
(
i
!=
214748364
||
s
.
Peek
()
>
'8'
)
{
if
(
i
!=
214748364
||
s
.
Peek
()
>
'8'
)
{
try64bit
=
true
;
i64
=
i
;
use64bit
=
true
;
break
;
break
;
}
}
}
}
...
@@ -782,7 +784,8 @@ private:
...
@@ -782,7 +784,8 @@ private:
while
(
s
.
Peek
()
>=
'0'
&&
s
.
Peek
()
<=
'9'
)
{
while
(
s
.
Peek
()
>=
'0'
&&
s
.
Peek
()
<=
'9'
)
{
if
(
i
>=
429496729
)
{
// 2^32 - 1 = 4294967295
if
(
i
>=
429496729
)
{
// 2^32 - 1 = 4294967295
if
(
i
!=
429496729
||
s
.
Peek
()
>
'5'
)
{
if
(
i
!=
429496729
||
s
.
Peek
()
>
'5'
)
{
try64bit
=
true
;
i64
=
i
;
use64bit
=
true
;
break
;
break
;
}
}
}
}
...
@@ -793,14 +796,14 @@ private:
...
@@ -793,14 +796,14 @@ private:
RAPIDJSON_PARSE_ERROR
(
kParseErrorValueInvalid
,
s
.
Tell
());
RAPIDJSON_PARSE_ERROR
(
kParseErrorValueInvalid
,
s
.
Tell
());
// Parse 64bit int
// Parse 64bit int
uint64_t
i64
=
0
;
double
d
=
0
.
0
;
bool
useDouble
=
false
;
bool
useDouble
=
false
;
if
(
try64bit
)
{
if
(
use64bit
)
{
i64
=
i
;
if
(
minus
)
if
(
minus
)
while
(
s
.
Peek
()
>=
'0'
&&
s
.
Peek
()
<=
'9'
)
{
while
(
s
.
Peek
()
>=
'0'
&&
s
.
Peek
()
<=
'9'
)
{
if
(
i64
>=
RAPIDJSON_UINT64_C2
(
0x0CCCCCCC
,
0xCCCCCCCC
))
// 2^63 = 9223372036854775808
if
(
i64
>=
RAPIDJSON_UINT64_C2
(
0x0CCCCCCC
,
0xCCCCCCCC
))
// 2^63 = 9223372036854775808
if
(
i64
!=
RAPIDJSON_UINT64_C2
(
0x0CCCCCCC
,
0xCCCCCCCC
)
||
s
.
Peek
()
>
'8'
)
{
if
(
i64
!=
RAPIDJSON_UINT64_C2
(
0x0CCCCCCC
,
0xCCCCCCCC
)
||
s
.
Peek
()
>
'8'
)
{
d
=
(
double
)
i64
;
useDouble
=
true
;
useDouble
=
true
;
break
;
break
;
}
}
...
@@ -810,6 +813,7 @@ private:
...
@@ -810,6 +813,7 @@ private:
while
(
s
.
Peek
()
>=
'0'
&&
s
.
Peek
()
<=
'9'
)
{
while
(
s
.
Peek
()
>=
'0'
&&
s
.
Peek
()
<=
'9'
)
{
if
(
i64
>=
RAPIDJSON_UINT64_C2
(
0x19999999
,
0x99999999
))
// 2^64 - 1 = 18446744073709551615
if
(
i64
>=
RAPIDJSON_UINT64_C2
(
0x19999999
,
0x99999999
))
// 2^64 - 1 = 18446744073709551615
if
(
i64
!=
RAPIDJSON_UINT64_C2
(
0x19999999
,
0x99999999
)
||
s
.
Peek
()
>
'5'
)
{
if
(
i64
!=
RAPIDJSON_UINT64_C2
(
0x19999999
,
0x99999999
)
||
s
.
Peek
()
>
'5'
)
{
d
=
(
double
)
i64
;
useDouble
=
true
;
useDouble
=
true
;
break
;
break
;
}
}
...
@@ -818,9 +822,7 @@ private:
...
@@ -818,9 +822,7 @@ private:
}
}
// Force double for big integer
// Force double for big integer
double
d
=
0
.
0
;
if
(
useDouble
)
{
if
(
useDouble
)
{
d
=
(
double
)
i64
;
while
(
s
.
Peek
()
>=
'0'
&&
s
.
Peek
()
<=
'9'
)
{
while
(
s
.
Peek
()
>=
'0'
&&
s
.
Peek
()
<=
'9'
)
{
if
(
d
>=
1.7976931348623157e307
)
// DBL_MAX / 10.0
if
(
d
>=
1.7976931348623157e307
)
// DBL_MAX / 10.0
RAPIDJSON_PARSE_ERROR
(
kParseErrorNumberTooBig
,
s
.
Tell
());
RAPIDJSON_PARSE_ERROR
(
kParseErrorNumberTooBig
,
s
.
Tell
());
...
@@ -831,33 +833,46 @@ private:
...
@@ -831,33 +833,46 @@ private:
// Parse frac = decimal-point 1*DIGIT
// Parse frac = decimal-point 1*DIGIT
int
expFrac
=
0
;
int
expFrac
=
0
;
if
(
s
.
Peek
()
==
'.'
)
{
if
(
s
.
Peek
()
==
'.'
)
{
s
.
Take
();
#if RAPIDJSON_64BIT
// Use i64 to store significand in 64-bit architecture
if
(
!
useDouble
)
{
if
(
!
useDouble
)
{
d
=
try64bit
?
(
double
)
i64
:
(
double
)
i
;
if
(
!
use64bit
)
useDouble
=
true
;
i64
=
i
;
while
(
s
.
Peek
()
>=
'0'
&&
s
.
Peek
()
<=
'9'
)
{
if
(
i64
>=
RAPIDJSON_UINT64_C2
(
0x19999999
,
0x99999999
))
break
;
else
{
i64
=
i64
*
10
+
static_cast
<
unsigned
>
(
s
.
Take
()
-
'0'
);
--
expFrac
;
}
}
d
=
(
double
)
i64
;
}
}
s
.
Take
();
#else
// Use double to store significand in 32-bit architecture
if
(
!
useDouble
)
d
=
use64bit
?
(
double
)
i64
:
(
double
)
i
;
#endif
useDouble
=
true
;
if
(
s
.
Peek
()
>=
'0'
&&
s
.
Peek
()
<=
'9'
)
{
while
(
s
.
Peek
()
>=
'0'
&&
s
.
Peek
()
<=
'9'
)
{
d
=
d
*
10
+
(
s
.
Take
()
-
'0'
);
d
=
d
*
10
+
(
s
.
Take
()
-
'0'
);
--
expFrac
;
--
expFrac
;
}
}
else
RAPIDJSON_PARSE_ERROR
(
kParseErrorNumberMissFraction
,
s
.
Tell
());
while
(
s
.
Peek
()
>=
'0'
&&
s
.
Peek
()
<=
'9'
)
{
if
(
expFrac
==
0
)
if
(
expFrac
>
-
16
)
{
RAPIDJSON_PARSE_ERROR
(
kParseErrorNumberMissFraction
,
s
.
Tell
());
d
=
d
*
10
+
(
s
.
Peek
()
-
'0'
);
--
expFrac
;
}
s
.
Take
();
}
}
}
// Parse exp = e [ minus / plus ] 1*DIGIT
// Parse exp = e [ minus / plus ] 1*DIGIT
int
exp
=
0
;
int
exp
=
0
;
if
(
s
.
Peek
()
==
'e'
||
s
.
Peek
()
==
'E'
)
{
if
(
s
.
Peek
()
==
'e'
||
s
.
Peek
()
==
'E'
)
{
if
(
!
useDouble
)
{
if
(
!
useDouble
)
{
d
=
try
64bit
?
(
double
)
i64
:
(
double
)
i
;
d
=
use
64bit
?
(
double
)
i64
:
(
double
)
i
;
useDouble
=
true
;
useDouble
=
true
;
}
}
s
.
Take
();
s
.
Take
();
...
@@ -900,7 +915,7 @@ private:
...
@@ -900,7 +915,7 @@ private:
cont
=
handler
.
Double
(
minus
?
-
d
:
d
);
cont
=
handler
.
Double
(
minus
?
-
d
:
d
);
}
}
else
{
else
{
if
(
try
64bit
)
{
if
(
use
64bit
)
{
if
(
minus
)
if
(
minus
)
cont
=
handler
.
Int64
(
-
(
int64_t
)
i64
);
cont
=
handler
.
Int64
(
-
(
int64_t
)
i64
);
else
else
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment