Debian Codegraph @

thirdparty/jolt_physics/Jolt/Core/FPControlWord.h

raw symbols SCIP
1 // Jolt Physics Library (https://github.com/jrouwe/JoltPhysics)
2 // SPDX-FileCopyrightText: 2021 Jorrit Rouwe
3 // SPDX-License-Identifier: MIT
4
5 #pragma once
6
7 #include <Jolt/Core/NonCopyable.h>
8
9 JPH_NAMESPACE_BEGIN
10
11 #if defined(JPH_CPU_WASM)
12
13 // Not supported
14
15 #elif defined(JPH_USE_SSE)
16
17 /// Helper class that needs to be put on the stack to update the state of the floating point control word.
18 /// This state is kept per thread.
19 template <uint Value, uint Mask>
20 class FPControlWord : public NonCopyable
21 {
22 public:
23 FPControlWord()
24 {
25 mPrevState = _mm_getcsr();
26 _mm_setcsr((mPrevState & ~Mask) | Value);
27 }
28
29 ~FPControlWord()
30 {
31 _mm_setcsr((_mm_getcsr() & ~Mask) | (mPrevState & Mask));
32 }
33
34 private:
35 uint mPrevState;
36 };
37
38 #elif defined(JPH_CPU_ARM) && defined(JPH_COMPILER_MSVC)
39
40 /// Helper class that needs to be put on the stack to update the state of the floating point control word.
41 /// This state is kept per thread.
42 template <unsigned int Value, unsigned int Mask>
43 class FPControlWord : public NonCopyable
44 {
45 public:
46 FPControlWord()
47 {
48 // Read state before change
49 _controlfp_s(&mPrevState, 0, 0);
50
51 // Update the state
52 unsigned int dummy;
53 _controlfp_s(&dummy, Value, Mask);
54 }
55
56 ~FPControlWord()
57 {
58 // Restore state
59 unsigned int dummy;
60 _controlfp_s(&dummy, mPrevState, Mask);
61 }
62
63 private:
64 unsigned int mPrevState;
65 };
66
67 #elif defined(JPH_CPU_ARM) && defined(JPH_USE_NEON)
68
69 /// Helper class that needs to be put on the stack to update the state of the floating point control word.
70 /// This state is kept per thread.
71 template <uint64 Value, uint64 Mask>
72 class FPControlWord : public NonCopyable
73 {
74 public:
75 FPControlWord()
76 {
77 uint64 val;
78 asm volatile("mrs %0, fpcr" : "=r" (val));
79 mPrevState = val;
80 val &= ~Mask;
81 val |= Value;
82 asm volatile("msr fpcr, %0" : /* no output */ : "r" (val));
83 }
84
85 ~FPControlWord()
86 {
87 uint64 val;
88 asm volatile("mrs %0, fpcr" : "=r" (val));
89 val &= ~Mask;
90 val |= mPrevState & Mask;
91 asm volatile("msr fpcr, %0" : /* no output */ : "r" (val));
92 }
93
94 private:
95 uint64 mPrevState;
96 };
97
98 #elif defined(JPH_CPU_ARM)
99
100 /// Helper class that needs to be put on the stack to update the state of the floating point control word.
101 /// This state is kept per thread.
102 template <uint32 Value, uint32 Mask>
103 class FPControlWord : public NonCopyable
104 {
105 public:
106 FPControlWord()
107 {
108 uint32 val;
109 asm volatile("vmrs %0, fpscr" : "=r" (val));
110 mPrevState = val;
111 val &= ~Mask;
112 val |= Value;
113 asm volatile("vmsr fpscr, %0" : /* no output */ : "r" (val));
114 }
115
116 ~FPControlWord()
117 {
118 uint32 val;
119 asm volatile("vmrs %0, fpscr" : "=r" (val));
120 val &= ~Mask;
121 val |= mPrevState & Mask;
122 asm volatile("vmsr fpscr, %0" : /* no output */ : "r" (val));
123 }
124
125 private:
126 uint32 mPrevState;
127 };
128
129 #elif defined(JPH_CPU_RISCV)
130
131 // RISC-V only implements manually checking if exceptions occurred by reading the fcsr register. It doesn't generate exceptions.
132
133 #elif defined(JPH_CPU_PPC) || defined(JPH_CPU_LOONGARCH)
134
135 // Not implemented right now
136
137 #else
138
139 #error Unsupported CPU architecture
140
141 #endif
142
143 JPH_NAMESPACE_END