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_F2S_INTRINSICS_H
18 #define RYU_F2S_INTRINSICS_H
19
20 // Defines RYU_32_BIT_PLATFORM if applicable.
21 #include "common.h"
22
23 #define FLOAT_POW5_INV_BITCOUNT (DOUBLE_POW5_INV_BITCOUNT - 64)
24 #define FLOAT_POW5_BITCOUNT (DOUBLE_POW5_BITCOUNT - 64)
25
pow5factor_32(uint32_t value)26 static inline uint32_t pow5factor_32(uint32_t value) {
27 uint32_t count = 0;
28 for (;;) {
29 assert(value != 0);
30 const uint32_t q = value / 5;
31 const uint32_t r = value % 5;
32 if (r != 0) {
33 break;
34 }
35 value = q;
36 ++count;
37 }
38 return count;
39 }
40
41 // Returns true if value is divisible by 5^p.
multipleOfPowerOf5_32(const uint32_t value,const uint32_t p)42 static inline bool multipleOfPowerOf5_32(const uint32_t value, const uint32_t p) {
43 return pow5factor_32(value) >= p;
44 }
45
46 // Returns true if value is divisible by 2^p.
multipleOfPowerOf2_32(const uint32_t value,const uint32_t p)47 static inline bool multipleOfPowerOf2_32(const uint32_t value, const uint32_t p) {
48 // __builtin_ctz doesn't appear to be faster here.
49 return (value & ((1u << p) - 1)) == 0;
50 }
51
52 uint32_t __mulPow5InvDivPow2(const uint32_t m, const uint32_t q, const int32_t j);
53 #define mulPow5InvDivPow2(m,q,j) __mulPow5InvDivPow2(m,q,j)
54
55 uint32_t __mulPow5divPow2(const uint32_t m, const uint32_t i, const int32_t j);
56 #define mulPow5divPow2(m,i,j) __mulPow5divPow2(m,i,j)
57
58 #endif // RYU_F2S_INTRINSICS_H
59