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 
max_int(int a,int b)35 static inline int max_int(int a, int b) { return a > b ? a : b; }
min_int(int a,int b)36 static inline int min_int(int a, int b) { return a < b ? a : b; }
37 
38 // Returns e == 0 ? 1 : [log_2(5^e)]; requires 0 <= e <= 3528.
39 int32_t __log2pow5(const int32_t e);
40 
41 #define log2pow5(e) __log2pow5(e)
42 
43 // Returns e == 0 ? 1 : ceil(log_2(5^e)); requires 0 <= e <= 3528.
44 int32_t __ceil_log2pow5(const int32_t e);
45 
46 #define ceil_log2pow5(e) __ceil_log2pow5(e)
47 
48 // Returns e == 0 ? 1 : ceil(log_2(5^e)); requires 0 <= e <= 3528.
49 int32_t __pow5bits(const int32_t e);
50 
51 #define pow5bits(e) __pow5bits(e)
52 
53 // Returns floor(log_10(2^e)); requires 0 <= e <= 1650.
54 uint32_t __log10Pow2(const int32_t e);
55 
56 #define log10Pow2(e) __log10Pow2(e)
57 
58 // Returns floor(log_10(5^e)); requires 0 <= e <= 2620.
59 uint32_t __log10Pow5(const int32_t e);
60 
61 #define log10Pow5(e) __log10Pow5(e)
62 
float_to_bits(const float f)63 static inline uint32_t float_to_bits(const float f) {
64   uint32_t bits = 0;
65   memcpy(&bits, &f, sizeof(float));
66   return bits;
67 }
68 
double_to_bits(const double d)69 static inline uint64_t double_to_bits(const double d) {
70   uint64_t bits = 0;
71   memcpy(&bits, &d, sizeof(double));
72   return bits;
73 }
74 
75 // These tables are generated by PrintDoubleLookupTable.
76 #define DOUBLE_POW5_INV_BITCOUNT 125
77 #define DOUBLE_POW5_BITCOUNT 125
78 
79 void __double_computePow5(const uint32_t i, uint64_t* const result);
80 
81 void __double_computeInvPow5(const uint32_t i, uint64_t* const result);
82 
83 #endif // RYU_COMMON_H
84