Jolt Physics
A multi core friendly Game Physics Engine
Loading...
Searching...
No Matches
FPControlWord.h
Go to the documentation of this file.
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
8
10
11#if defined(JPH_CPU_WASM)
12
13// Not supported
14
15#elif defined(JPH_USE_SSE)
16
19template <uint Value, uint Mask>
20class FPControlWord : public NonCopyable
21{
22public:
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
34private:
35 uint mPrevState;
36};
37
38#elif defined(JPH_CPU_ARM) && defined(JPH_COMPILER_MSVC)
39
42template <unsigned int Value, unsigned int Mask>
43class FPControlWord : public NonCopyable
44{
45public:
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
63private:
64 unsigned int mPrevState;
65};
66
67#elif defined(JPH_CPU_ARM) && defined(JPH_USE_NEON)
68
71template <uint64 Value, uint64 Mask>
72class FPControlWord : public NonCopyable
73{
74public:
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
94private:
95 uint64 mPrevState;
96};
97
98#elif defined(JPH_CPU_ARM)
99
102template <uint32 Value, uint32 Mask>
103class FPControlWord : public NonCopyable
104{
105public:
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
125private:
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
std::uint64_t uint64
Definition Core.h:485
unsigned int uint
Definition Core.h:481
#define JPH_NAMESPACE_END
Definition Core.h:414
std::uint32_t uint32
Definition Core.h:484
#define JPH_NAMESPACE_BEGIN
Definition Core.h:408
Class that makes another class non-copyable. Usage: Inherit from NonCopyable.
Definition NonCopyable.h:11