1 //===-- Properties of floating point numbers --------------------*- C++ -*-===//
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
7 //===----------------------------------------------------------------------===//
9 #ifndef LLVM_LIBC_UTILS_FPUTIL_FLOAT_PROPERTIES_H
10 #define LLVM_LIBC_UTILS_FPUTIL_FLOAT_PROPERTIES_H
14 namespace __llvm_libc
{
17 template <typename T
> struct FloatProperties
{};
19 template <> struct FloatProperties
<float> {
20 typedef uint32_t BitsType
;
21 static_assert(sizeof(BitsType
) == sizeof(float),
22 "Unexpected size of 'float' type.");
24 static constexpr uint32_t mantissaWidth
= 23;
25 static constexpr BitsType mantissaMask
= 0x007fffffU
;
26 static constexpr BitsType signMask
= 0x80000000U
;
27 static constexpr uint32_t exponentOffset
= 127;
29 // If a number x is a NAN, then it is a quiet NAN if:
30 // QuietNaNMask & bits(x) != 0
31 // Else, it is a signalling NAN.
32 static constexpr BitsType quietNaNMask
= 0x00400000U
;
35 template <> struct FloatProperties
<double> {
36 typedef uint64_t BitsType
;
37 static_assert(sizeof(BitsType
) == sizeof(double),
38 "Unexpected size of 'double' type.");
40 static constexpr uint32_t mantissaWidth
= 52;
41 static constexpr BitsType mantissaMask
= 0x000fffffffffffffU
;
42 static constexpr BitsType signMask
= 0x8000000000000000ULL
;
43 static constexpr uint32_t exponentOffset
= 1023;
45 // If a number x is a NAN, then it is a quiet NAN if:
46 // QuietNaNMask & bits(x) != 0
47 // Else, it is a signalling NAN.
48 static constexpr BitsType quietNaNMask
= 0x0008000000000000ULL
;
51 // Define the float type corresponding to the BitsType.
52 template <typename BitsType
> struct FloatType
;
54 template <> struct FloatType
<uint32_t> {
55 static_assert(sizeof(uint32_t) == sizeof(float),
56 "Unexpected size of 'float' type.");
60 template <> struct FloatType
<uint64_t> {
61 static_assert(sizeof(uint64_t) == sizeof(double),
62 "Unexpected size of 'double' type.");
66 template <typename BitsType
>
67 using FloatTypeT
= typename FloatType
<BitsType
>::Type
;
70 } // namespace __llvm_libc
72 #endif // LLVM_LIBC_UTILS_FPUTIL_FLOAT_PROPERTIES_H