1//===-- MathExtras.cpp - Implement the MathExtras header --------------===//
2//
3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6//
7//===----------------------------------------------------------------------===//
8//
9// This file implements the MathExtras.h header
10//
11//===----------------------------------------------------------------------===//
12
13#include "llvm/Support/MathExtras.h"
14
15#ifdef _MSC_VER
16#include <limits>
17#else
18#include <cmath>
19#endif
20
21namespace llvm {
22
23#if defined(_MSC_VER)
24 // Visual Studio defines the HUGE_VAL class of macros using purposeful
25 // constant arithmetic overflow, which it then warns on when encountered.
26 const float huge_valf = std::numeric_limits<float>::infinity();
27#else
28 const float huge_valf = HUGE_VALF;
29#endif
30
31 /// Returns the number of digits in the given integer.
32 int NumDigitsBase10(uint64_t X) {
33 static constexpr struct ConstexprData {
34 uint8_t AtLeast[65] = {};
35 uint64_t Boundaries[20] = {};
36 static constexpr int NumDigitsConstexpr(uint64_t N) {
37 int res = 1;
38 while (N >= 10) {
39 res++;
40 N /= 10;
41 }
42 return res;
43 }
44 constexpr ConstexprData() {
45 uint64_t Val = ~0ull;
46 for (uint64_t i = 0; i <= 64; i++) {
47 uint64_t Digits = NumDigitsConstexpr(N: Val) - 1;
48 AtLeast[i] = Digits;
49 Val >>= 1;
50 }
51 // Special case because X=0 should return 1 and not 0
52 Boundaries[0] = 0;
53 Val = 10;
54 for (uint64_t i = 1; i < 20; i++) {
55 Boundaries[i] = Val;
56 Val *= 10;
57 }
58 }
59 } Data;
60
61 uint64_t Base2 = X ? countl_zero(Val: X) : 64;
62 uint64_t Digits = Data.AtLeast[Base2];
63 return Digits + (X >= Data.Boundaries[Digits]);
64 }
65
66} // namespace llvm
67