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 
18 #include "ryu/common.h"
19 
20 // Returns e == 0 ? 1 : [log_2(5^e)]; requires 0 <= e <= 3528.
__log2pow5(const int32_t e)21 int32_t __log2pow5(const int32_t e) {
22   // This approximation works up to the point that the multiplication overflows at e = 3529.
23   // If the multiplication were done in 64 bits, it would fail at 5^4004 which is just greater
24   // than 2^9297.
25   assert(e >= 0);
26   assert(e <= 3528);
27   return (int32_t) ((((uint32_t) e) * 1217359) >> 19);
28 }
29 
30 // Returns e == 0 ? 1 : ceil(log_2(5^e)); requires 0 <= e <= 3528.
__ceil_log2pow5(const int32_t e)31 int32_t __ceil_log2pow5(const int32_t e) {
32   return log2pow5(e) + 1;
33 }
34 
35