1 // Copyright 2018 Ulf Adams
2 //
3 // The contents of this file may be used under the terms of the Apache License,
4 // Version 2.0.
5 //
6 //    (See accompanying file LICENSE-Apache or copy at
7 //     http://www.apache.org/licenses/LICENSE-2.0)
8 //
9 // Alternatively, the contents of this file may be used under the terms of
10 // the Boost Software License, Version 1.0.
11 //    (See accompanying file LICENSE-Boost or copy at
12 //     https://www.boost.org/LICENSE_1_0.txt)
13 //
14 // Unless required by applicable law or agreed to in writing, this software
15 // is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16 // KIND, either express or implied.
17 #ifndef RYU_COMMON_H
18 #define RYU_COMMON_H
19 
20 
21 #if defined(_M_IX86) || defined(_M_ARM)
22 #define RYU_32_BIT_PLATFORM
23 #endif
24 
25 // Returns the number of decimal digits in v, which must not contain more than 9 digits.
decimalLength9(const uint32_t v)26 static inline uint32_t decimalLength9(const uint32_t v) {
27   // Function precondition: v is not a 10-digit number.
28   // (f2s: 9 digits are sufficient for round-tripping.)
29   // (d2fixed: We print 9-digit blocks.)
30   assert(v < 1000000000);
31   if (v >= 100000000) { return 9; }
32   if (v >= 10000000) { return 8; }
33   if (v >= 1000000) { return 7; }
34   if (v >= 100000) { return 6; }
35   if (v >= 10000) { return 5; }
36   if (v >= 1000) { return 4; }
37   if (v >= 100) { return 3; }
38   if (v >= 10) { return 2; }
39   return 1;
40 }
41 
42 // Returns e == 0 ? 1 : [log_2(5^e)]; requires 0 <= e <= 3528.
log2pow5(const int32_t e)43 static inline int32_t log2pow5(const int32_t e) {
44   // This approximation works up to the point that the multiplication overflows at e = 3529.
45   // If the multiplication were done in 64 bits, it would fail at 5^4004 which is just greater
46   // than 2^9297.
47   assert(e >= 0);
48   assert(e <= 3528);
49   return (int32_t) ((((uint32_t) e) * 1217359) >> 19);
50 }
51 
52 // Returns e == 0 ? 1 : ceil(log_2(5^e)); requires 0 <= e <= 3528.
pow5bits(const int32_t e)53 static inline int32_t pow5bits(const int32_t e) {
54   // This approximation works up to the point that the multiplication overflows at e = 3529.
55   // If the multiplication were done in 64 bits, it would fail at 5^4004 which is just greater
56   // than 2^9297.
57   assert(e >= 0);
58   assert(e <= 3528);
59   return (int32_t) (((((uint32_t) e) * 1217359) >> 19) + 1);
60 }
61 
62 // Returns e == 0 ? 1 : ceil(log_2(5^e)); requires 0 <= e <= 3528.
ceil_log2pow5(const int32_t e)63 static inline int32_t ceil_log2pow5(const int32_t e) {
64   return log2pow5(e) + 1;
65 }
66 
67 // Returns floor(log_10(2^e)); requires 0 <= e <= 1650.
log10Pow2(const int32_t e)68 static inline uint32_t log10Pow2(const int32_t e) {
69   // The first value this approximation fails for is 2^1651 which is just greater than 10^297.
70   assert(e >= 0);
71   assert(e <= 1650);
72   return (((uint32_t) e) * 78913) >> 18;
73 }
74 
75 // Returns floor(log_10(5^e)); requires 0 <= e <= 2620.
log10Pow5(const int32_t e)76 static inline uint32_t log10Pow5(const int32_t e) {
77   // The first value this approximation fails for is 5^2621 which is just greater than 10^1832.
78   assert(e >= 0);
79   assert(e <= 2620);
80   return (((uint32_t) e) * 732923) >> 20;
81 }
82 
float_to_bits(const float f)83 static inline uint32_t float_to_bits(const float f) {
84   uint32_t bits = 0;
85   memcpy(&bits, &f, sizeof(float));
86   return bits;
87 }
88 
double_to_bits(const double d)89 static inline uint64_t double_to_bits(const double d) {
90   uint64_t bits = 0;
91   memcpy(&bits, &d, sizeof(double));
92   return bits;
93 }
94 
95 #endif // RYU_COMMON_H
96