Debian Codegraph @

Source/Core/Common/FloatUtils.h

raw symbols SCIP
1 // Copyright 2018 Dolphin Emulator Project
2 // SPDX-License-Identifier: GPL-2.0-or-later
3
4 #pragma once
5
6 #include <array>
7 #include <bit>
8 #include <limits>
9
10 #include "Common/CommonTypes.h"
11
12 namespace Common
13 {
14 template <typename T>
15 constexpr T SNANConstant()
16 {
17 return std::numeric_limits<T>::signaling_NaN();
18 }
19
20 // The most significant bit of the fraction is an is-quiet bit on all architectures we care about.
21 static constexpr u64 DOUBLE_QBIT = 0x0008000000000000ULL;
22 static constexpr u64 DOUBLE_SIGN = 0x8000000000000000ULL;
23 static constexpr u64 DOUBLE_EXP = 0x7FF0000000000000ULL;
24 static constexpr u64 DOUBLE_FRAC = 0x000FFFFFFFFFFFFFULL;
25 static constexpr u64 DOUBLE_ZERO = 0x0000000000000000ULL;
26 static constexpr int DOUBLE_EXP_WIDTH = 11;
27 static constexpr int DOUBLE_FRAC_WIDTH = 52;
28
29 static constexpr u32 FLOAT_SIGN = 0x80000000;
30 static constexpr u32 FLOAT_EXP = 0x7F800000;
31 static constexpr u32 FLOAT_FRAC = 0x007FFFFF;
32 static constexpr u32 FLOAT_ZERO = 0x00000000;
33 static constexpr int FLOAT_EXP_WIDTH = 8;
34 static constexpr int FLOAT_FRAC_WIDTH = 23;
35
36 inline bool IsQNAN(double d)
37 {
38 const u64 i = std::bit_cast<u64>(d);
39 return ((i & DOUBLE_EXP) == DOUBLE_EXP) && ((i & DOUBLE_QBIT) == DOUBLE_QBIT);
40 }
41
42 inline bool IsSNAN(double d)
43 {
44 const u64 i = std::bit_cast<u64>(d);
45 return ((i & DOUBLE_EXP) == DOUBLE_EXP) && ((i & DOUBLE_FRAC) != DOUBLE_ZERO) &&
46 ((i & DOUBLE_QBIT) == DOUBLE_ZERO);
47 }
48
49 inline float FlushToZero(float f)
50 {
51 u32 i = std::bit_cast<u32>(f);
52 if ((i & FLOAT_EXP) == 0)
53 {
54 // Turn into signed zero
55 i &= FLOAT_SIGN;
56 }
57 return std::bit_cast<float>(i);
58 }
59
60 inline double FlushToZero(double d)
61 {
62 u64 i = std::bit_cast<u64>(d);
63 if ((i & DOUBLE_EXP) == 0)
64 {
65 // Turn into signed zero
66 i &= DOUBLE_SIGN;
67 }
68 return std::bit_cast<double>(i);
69 }
70
71 inline double MakeQuiet(double d)
72 {
73 const u64 integral = std::bit_cast<u64>(d) | Common::DOUBLE_QBIT;
74
75 return std::bit_cast<double>(integral);
76 }
77
78 enum PPCFpClass
79 {
80 PPC_FPCLASS_QNAN = 0x11,
81 PPC_FPCLASS_NINF = 0x9,
82 PPC_FPCLASS_NN = 0x8,
83 PPC_FPCLASS_ND = 0x18,
84 PPC_FPCLASS_NZ = 0x12,
85 PPC_FPCLASS_PZ = 0x2,
86 PPC_FPCLASS_PD = 0x14,
87 PPC_FPCLASS_PN = 0x4,
88 PPC_FPCLASS_PINF = 0x5,
89 };
90
91 // Uses PowerPC conventions for the return value, so it can be easily
92 // used directly in CPU emulation.
93 u32 ClassifyDouble(double dvalue);
94 u32 ClassifyFloat(float fvalue);
95
96 struct BaseAndDec
97 {
98 int m_base;
99 int m_dec;
100 };
101 extern const std::array<BaseAndDec, 32> frsqrte_expected;
102 extern const std::array<BaseAndDec, 32> fres_expected;
103
104 // PowerPC approximation algorithms
105 double ApproximateReciprocalSquareRoot(double val);
106 double ApproximateReciprocal(double val);
107
108 } // namespace Common