ieeefloat.h
1 // Copyright 2017 Alexander Bolz
2 //
3 // Permission is hereby granted, free of charge, to any person obtaining a copy
4 // of this software and associated documentation files (the "Software"), to deal
5 // in the Software without restriction, including without limitation the rights
6 // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
7 // copies of the Software, and to permit persons to whom the Software is
8 // furnished to do so, subject to the following conditions:
9 //
10 // The above copyright notice and this permission notice shall be included in
11 // all copies or substantial portions of the Software.
12 //
13 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14 // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
16 // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
17 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
18 // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
19 // SOFTWARE.
20 // Header-only U+1F926 U+1F937
21 // Modified by Roman Gershman
22 #pragma once
23 
24 #include <cstdint>
25 #include <limits>
26 #include <type_traits>
27 
28 namespace util {
29 
30 template <typename Float>
31 struct IEEEFloat {
32  using NL = std::numeric_limits<Float>;
33 
34  static constexpr bool const is_single = (NL::digits == 24) && (NL::max_exponent) == 128;
35 
36  static constexpr bool const is_double = (NL::digits == 53) && (NL::max_exponent == 1024);
37 
38  static_assert(NL::is_iec559 && (is_single || is_double), "IEEE-754 implementation required");
39 
40  using Uint = typename std::conditional<is_single, uint32_t, uint64_t>::type;
41 
42  static_assert(sizeof(Float) == sizeof(Uint), "Size mismatch");
43 
44  static constexpr unsigned const kPrecision = is_single ? 24 : 53; // = p (includes the hidden bit!)
45  static constexpr unsigned const kSignificandLen = kPrecision - 1;
46  static constexpr unsigned const kMaxExponent = is_single ? 0xFF : 0x7FF;
47  static constexpr unsigned const kExponentBias = kMaxExponent / 2;
48  static constexpr Uint const kHiddenBit = Uint{1} << kSignificandLen;
49  static constexpr Uint const kSignMask = Uint{1} << (is_single ? 31 : 63);
50 
51  static constexpr Uint const kExponentMask = Uint{kMaxExponent} << kSignificandLen;
52  static constexpr Uint const kSignificandMask = kHiddenBit - 1;
53 
54  union { // XXX: memcpy?
55  Float value;
56  Uint bits;
57  };
58 
59  constexpr explicit IEEEFloat(Float value_) : value(value_) {}
60  constexpr explicit IEEEFloat(Uint bits_) : bits(bits_) {}
61  constexpr IEEEFloat(Uint exponent, Uint significand)
62  : bits(exponent << kSignificandLen | (significand & kSignificandMask)) {}
63 
64  constexpr Uint ExponentBits() const { return (bits & kExponentMask) >> (kPrecision - 1); }
65 
66  constexpr Uint SignificandBits() const { return (bits & kSignificandMask); }
67 
68  // Returns true if the sign-bit is set.
69  constexpr bool IsNegative() const { return (bits & kSignMask) != 0; }
70 
71  // Returns true if this value is -0 or +0.
72  bool IsZero() const { return (bits & ~kSignMask) == 0; }
73 
74  // Returns true if this value is denormal or 0.
75  bool IsDenormal() const { return (bits & kExponentMask) == 0; }
76 
77  // Returns true if this value is NaN
78  bool IsNaN() const {
79  return (bits & kExponentMask) == kExponentMask && (bits & kSignificandMask) != 0;
80  }
81 
82  // Returns true if this value is -Inf or +Inf.
83  bool IsInf() const {
84  return (bits & kExponentMask) == kExponentMask && (bits & kSignificandMask) == 0;
85  }
86 
87  // Returns this value with the sign-bit cleared.
88  Float Abs() const { return IEEEFloat(bits & ~kSignMask).value; }
89 
90 };
91 
92 
93 } // namespace util