Commit 5e5fb7b3 authored by Milo Yip's avatar Milo Yip

Change UINT64_C() to RAPIDJSON_UINT64_C2()

Fix #94
parent 41dde39b
......@@ -415,9 +415,9 @@ public:
data_.n.i64 = i64;
if (i64 >= 0) {
flags_ |= kNumberUint64Flag;
if (!(static_cast<uint64_t>(i64) & UINT64_C(0xFFFFFFFF00000000)))
if (!(static_cast<uint64_t>(i64) & RAPIDJSON_UINT64_C2(0xFFFFFFFF, 0x00000000)))
flags_ |= kUintFlag;
if (!(static_cast<uint64_t>(i64) & UINT64_C(0xFFFFFFFF80000000)))
if (!(static_cast<uint64_t>(i64) & RAPIDJSON_UINT64_C2(0xFFFFFFFF, 0x80000000)))
flags_ |= kIntFlag;
}
else if (i64 >= INT64_C(-2147483648))
......@@ -427,11 +427,11 @@ public:
//! Constructor for uint64_t value.
explicit GenericValue(uint64_t u64) : data_(), flags_(kNumberUint64Flag) {
data_.n.u64 = u64;
if (!(u64 & UINT64_C(0x8000000000000000)))
if (!(u64 & RAPIDJSON_UINT64_C2(0x80000000, 0x00000000)))
flags_ |= kInt64Flag;
if (!(u64 & UINT64_C(0xFFFFFFFF00000000)))
if (!(u64 & RAPIDJSON_UINT64_C2(0xFFFFFFFF, 0x00000000)))
flags_ |= kUintFlag;
if (!(u64 & UINT64_C(0xFFFFFFFF80000000)))
if (!(u64 & RAPIDJSON_UINT64_C2(0xFFFFFFFF, 0x80000000)))
flags_ |= kIntFlag;
}
......
......@@ -108,8 +108,17 @@ inline char* i32toa(int32_t value, char* buffer) {
inline char* u64toa(uint64_t value, char* buffer) {
const char* cDigitsLut = GetDigitsLut();
if (value < UINT64_C(100000000)) {
const uint64_t kTen8 = 100000000;
const uint64_t kTen9 = kTen8 * 10;
const uint64_t kTen10 = kTen8 * 100;
const uint64_t kTen11 = kTen8 * 1000;
const uint64_t kTen12 = kTen8 * 10000;
const uint64_t kTen13 = kTen8 * 100000;
const uint64_t kTen14 = kTen8 * 1000000;
const uint64_t kTen15 = kTen8 * 10000000;
const uint64_t kTen16 = kTen8 * kTen8;
if (value < kTen8) {
uint32_t v = static_cast<uint32_t>(value);
if (v < 10000) {
const uint32_t d1 = (v / 100) << 1;
......@@ -148,9 +157,9 @@ inline char* u64toa(uint64_t value, char* buffer) {
*buffer++ = cDigitsLut[d4 + 1];
}
}
else if (value < UINT64_C(10000000000000000)) {
const uint32_t v0 = static_cast<uint32_t>(value / UINT64_C(100000000));
const uint32_t v1 = static_cast<uint32_t>(value % UINT64_C(100000000));
else if (value < kTen16) {
const uint32_t v0 = static_cast<uint32_t>(value / kTen8);
const uint32_t v1 = static_cast<uint32_t>(value % kTen8);
const uint32_t b0 = v0 / 10000;
const uint32_t c0 = v0 % 10000;
......@@ -170,21 +179,21 @@ inline char* u64toa(uint64_t value, char* buffer) {
const uint32_t d7 = (c1 / 100) << 1;
const uint32_t d8 = (c1 % 100) << 1;
if (value >= UINT64_C(1000000000000000))
if (value >= kTen15)
*buffer++ = cDigitsLut[d1];
if (value >= UINT64_C(100000000000000))
if (value >= kTen14)
*buffer++ = cDigitsLut[d1 + 1];
if (value >= UINT64_C(10000000000000))
if (value >= kTen13)
*buffer++ = cDigitsLut[d2];
if (value >= UINT64_C(1000000000000))
if (value >= kTen12)
*buffer++ = cDigitsLut[d2 + 1];
if (value >= UINT64_C(100000000000))
if (value >= kTen11)
*buffer++ = cDigitsLut[d3];
if (value >= UINT64_C(10000000000))
if (value >= kTen10)
*buffer++ = cDigitsLut[d3 + 1];
if (value >= UINT64_C(1000000000))
if (value >= kTen9)
*buffer++ = cDigitsLut[d4];
if (value >= UINT64_C(100000000))
if (value >= kTen8)
*buffer++ = cDigitsLut[d4 + 1];
*buffer++ = cDigitsLut[d5];
......@@ -197,8 +206,8 @@ inline char* u64toa(uint64_t value, char* buffer) {
*buffer++ = cDigitsLut[d8 + 1];
}
else {
const uint32_t a = static_cast<uint32_t>(value / UINT64_C(10000000000000000)); // 1 to 1844
value %= UINT64_C(10000000000000000);
const uint32_t a = static_cast<uint32_t>(value / kTen16); // 1 to 1844
value %= kTen16;
if (a < 10)
*buffer++ = '0' + static_cast<char>(a);
......@@ -223,8 +232,8 @@ inline char* u64toa(uint64_t value, char* buffer) {
*buffer++ = cDigitsLut[j + 1];
}
const uint32_t v0 = static_cast<uint32_t>(value / UINT64_C(100000000));
const uint32_t v1 = static_cast<uint32_t>(value % UINT64_C(100000000));
const uint32_t v0 = static_cast<uint32_t>(value / kTen8);
const uint32_t v1 = static_cast<uint32_t>(value % kTen8);
const uint32_t b0 = v0 / 10000;
const uint32_t c0 = v0 % 10000;
......
......@@ -16,14 +16,10 @@
///////////////////////////////////////////////////////////////////////////////
// RAPIDJSON_NO_INT64DEFINE
// Here defines int64_t and uint64_t types in global namespace as well as the
// (U)INT64_C constant macros.
// Here defines int64_t and uint64_t types in global namespace
// If user have their own definition, can define RAPIDJSON_NO_INT64DEFINE to disable this.
#ifndef RAPIDJSON_NO_INT64DEFINE
//!@cond RAPIDJSON_HIDDEN_FROM_DOXYGEN
#ifndef __STDC_CONSTANT_MACROS
# define __STDC_CONSTANT_MACROS 1 // required by C++ standard
#endif
#ifdef _MSC_VER
#include "msinttypes/stdint.h"
#include "msinttypes/inttypes.h"
......@@ -109,6 +105,19 @@
#define RAPIDJSON_ALIGN(x) ((x + 3u) & ~3u)
#endif
///////////////////////////////////////////////////////////////////////////////
// RAPIDJSON_UINT64_C2
//! Construct a 64-bit literal by a pair of 32-bit integer.
/*!
64-bit literal with or without ULL suffix is prone to compiler warnings.
UINT64_C() is C macro which cause compilation problems.
Use this macro to define 64-bit constants by a pair of 32-bit integer.
*/
#ifndef RAPIDJSON_UINT64_C2
#define RAPIDJSON_UINT64_C2(high32, low32) ((static_cast<uint64_t>(high32) << 32) | static_cast<uint64_t>(low32))
#endif
///////////////////////////////////////////////////////////////////////////////
// RAPIDJSON_SSE2/RAPIDJSON_SSE42/RAPIDJSON_SIMD
......
......@@ -721,8 +721,8 @@ private:
i64 = i;
if (minus)
while (s.Peek() >= '0' && s.Peek() <= '9') {
if (i64 >= UINT64_C(922337203685477580)) // 2^63 = 9223372036854775808
if (i64 != UINT64_C(922337203685477580) || s.Peek() > '8') {
if (i64 >= RAPIDJSON_UINT64_C2(0x0CCCCCCC, 0xCCCCCCCC)) // 2^63 = 9223372036854775808
if (i64 != RAPIDJSON_UINT64_C2(0x0CCCCCCC, 0xCCCCCCCC) || s.Peek() > '8') {
useDouble = true;
break;
}
......@@ -730,8 +730,8 @@ private:
}
else
while (s.Peek() >= '0' && s.Peek() <= '9') {
if (i64 >= UINT64_C(1844674407370955161)) // 2^64 - 1 = 18446744073709551615
if (i64 != UINT64_C(1844674407370955161) || s.Peek() > '5') {
if (i64 >= RAPIDJSON_UINT64_C2(0x19999999, 0x99999999)) // 2^64 - 1 = 18446744073709551615
if (i64 != RAPIDJSON_UINT64_C2(0x19999999, 0x99999999) || s.Peek() > '5') {
useDouble = true;
break;
}
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment