• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 //===-- Bit patterns of common floating point numbers -----------*- C++ -*-===//
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 #ifndef LLVM_LIBC_UTILS_FPUTIL_BIT_PATTERNS_H
10 #define LLVM_LIBC_UTILS_FPUTIL_BIT_PATTERNS_H
11 
12 #include "FloatProperties.h"
13 
14 #include <float.h>
15 
16 static_assert(
17     FLT_RADIX == 2,
18     "LLVM libc only supports radix 2 IEEE 754 floating point formats.");
19 
20 namespace __llvm_libc {
21 namespace fputil {
22 
23 template <typename T> struct BitPatterns {};
24 
25 template <> struct BitPatterns<float> {
26   using BitsType = FloatProperties<float>::BitsType;
27 
28   static constexpr BitsType inf = 0x7f800000U;
29   static constexpr BitsType negInf = 0xff800000U;
30 
31   static constexpr BitsType zero = 0x0;
32   static constexpr BitsType negZero = 0x80000000U;
33 
34   static constexpr BitsType one = 0x3f800000U;
35 
36   // Examples of quiet NAN.
37   static constexpr BitsType aQuietNaN = 0x7fc00000U;
38   static constexpr BitsType aNegativeQuietNaN = 0xffc00000U;
39 
40   // Examples of signalling NAN.
41   static constexpr BitsType aSignallingNaN = 0x7f800001U;
42   static constexpr BitsType aNegativeSignallingNaN = 0xff800001U;
43 };
44 
45 template <> struct BitPatterns<double> {
46   using BitsType = FloatProperties<double>::BitsType;
47 
48   static constexpr BitsType inf = 0x7ff0000000000000ULL;
49   static constexpr BitsType negInf = 0xfff0000000000000ULL;
50 
51   static constexpr BitsType zero = 0x0ULL;
52   static constexpr BitsType negZero = 0x8000000000000000ULL;
53 
54   static constexpr BitsType one = 0x3FF0000000000000ULL;
55 
56   // Examples of quiet NAN.
57   static constexpr BitsType aQuietNaN = 0x7ff8000000000000ULL;
58   static constexpr BitsType aNegativeQuietNaN = 0xfff8000000000000ULL;
59 
60   // Examples of signalling NAN.
61   static constexpr BitsType aSignallingNaN = 0x7ff0000000000001ULL;
62   static constexpr BitsType aNegativeSignallingNaN = 0xfff0000000000001ULL;
63 };
64 
65 } // namespace fputil
66 } // namespace __llvm_libc
67 
68 #endif // LLVM_LIBC_UTILS_FPUTIL_BIT_PATTERNS_H
69