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 #include <stdint.h>
21 #include <string.h>
22 #include <stdbool.h>
23 
24 #define assert(x)
25 //#include <assert.h>
26 
27 #if __SIZEOF_POINTER__ == 4
28 #define RYU_32_BIT_PLATFORM
29 #endif
30 
31 #ifdef __SIZEOF_INT128__
32 #define HAS_UINT128
33 #endif
34 
35 #if __SIZEOF_DOUBLE__ == 8
36 #define RYU64   double
37 #elif __SIZEOF_LONG_DOUBLE__ == 8
38 #define RYU64   long double
39 #endif
40 
max_int(int a,int b)41 static inline int max_int(int a, int b) { return a > b ? a : b; }
min_int(int a,int b)42 static inline int min_int(int a, int b) { return a < b ? a : b; }
43 
44 // Returns e == 0 ? 1 : [log_2(5^e)]; requires 0 <= e <= 3528.
45 int32_t __log2pow5(const int32_t e);
46 
47 #define log2pow5(e) __log2pow5(e)
48 
49 // Returns e == 0 ? 1 : ceil(log_2(5^e)); requires 0 <= e <= 3528.
50 int32_t __ceil_log2pow5(const int32_t e);
51 
52 #define ceil_log2pow5(e) __ceil_log2pow5(e)
53 
54 // Returns e == 0 ? 1 : ceil(log_2(5^e)); requires 0 <= e <= 3528.
55 int32_t __pow5bits(const int32_t e);
56 
57 #define pow5bits(e) __pow5bits(e)
58 
59 // Returns floor(log_10(2^e)); requires 0 <= e <= 1650.
60 uint32_t __log10Pow2(const int32_t e);
61 
62 #define log10Pow2(e) __log10Pow2(e)
63 
64 // Returns floor(log_10(5^e)); requires 0 <= e <= 2620.
65 uint32_t __log10Pow5(const int32_t e);
66 
67 #define log10Pow5(e) __log10Pow5(e)
68 
float_to_bits(const float f)69 static inline uint32_t float_to_bits(const float f) {
70   uint32_t bits = 0;
71   memcpy(&bits, &f, sizeof(float));
72   return bits;
73 }
74 
75 #ifdef RYU64
76 
ryu64_to_bits(const RYU64 d)77 static inline uint64_t ryu64_to_bits(const RYU64 d) {
78   uint64_t bits = 0;
79   memcpy(&bits, &d, sizeof(d));
80   return bits;
81 }
82 
83 #endif
84 
85 // These tables are generated by PrintDoubleLookupTable.
86 #define DOUBLE_POW5_INV_BITCOUNT 125
87 #define DOUBLE_POW5_BITCOUNT 125
88 
89 void __double_computePow5(const uint32_t i, uint64_t* const result);
90 
91 void __double_computeInvPow5(const uint32_t i, uint64_t* const result);
92 
93 #endif // RYU_COMMON_H
94