LLVM 23.0.0git
MathExtras.cpp
Go to the documentation of this file.
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
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.
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(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(X) : 64;
62 uint64_t Digits = Data.AtLeast[Base2];
63 return Digits + (X >= Data.Boundaries[Digits]);
64 }
65
66} // namespace llvm
static TableGen::Emitter::OptClass< SkeletonEmitter > X("gen-skeleton-class", "Generate example skeleton class")
This is an optimization pass for GlobalISel generic memory operations.
Definition Types.h:26
int NumDigitsBase10(uint64_t X)
Returns the number of digits in the given integer.
int countl_zero(T Val)
Count number of 0's from the most significant bit to the least stopping at the first 1.
Definition bit.h:236
FunctionAddr VTableAddr uintptr_t uintptr_t Data
Definition InstrProf.h:189
LLVM_ABI const float huge_valf
Use this rather than HUGE_VALF; the latter causes warnings on MSVC.
#define N