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 #include "ryu/common.h"
18 
19 // Returns floor(log_10(2^e)); requires 0 <= e <= 1650.
__log10Pow2(const int32_t e)20 uint32_t __log10Pow2(const int32_t e) {
21   // The first value this approximation fails for is 2^1651 which is just greater than 10^297.
22   assert(e >= 0);
23   assert(e <= 1650);
24   return (((uint32_t) e) * 78913) >> 18;
25 }
26 
27 // Returns floor(log_10(5^e)); requires 0 <= e <= 2620.
__log10Pow5(const int32_t e)28 uint32_t __log10Pow5(const int32_t e) {
29   // The first value this approximation fails for is 5^2621 which is just greater than 10^1832.
30   assert(e >= 0);
31   assert(e <= 2620);
32   return (((uint32_t) e) * 732923) >> 20;
33 }
34 
35