xref: /aosp_15_r20/external/llvm-libc/src/__support/FPUtil/aarch64/fenv_darwin_impl.h (revision 71db0c75aadcf003ffe3238005f61d7618a3fead)
1 //===- darwin-aarch64 floating point env manipulation functions -*- C++ -*-===//
2 //
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
6 //
7 //===----------------------------------------------------------------------===//
8 
9 #ifndef LLVM_LIBC_SRC___SUPPORT_FPUTIL_AARCH64_FENV_DARWIN_IMPL_H
10 #define LLVM_LIBC_SRC___SUPPORT_FPUTIL_AARCH64_FENV_DARWIN_IMPL_H
11 
12 #include "src/__support/macros/attributes.h" // LIBC_INLINE
13 #include "src/__support/macros/config.h"
14 #include "src/__support/macros/properties/architectures.h"
15 
16 #if !defined(LIBC_TARGET_ARCH_IS_AARCH64) || !defined(__APPLE__)
17 #error "Invalid include"
18 #endif
19 
20 #include <arm_acle.h>
21 #include <stdint.h>
22 
23 #include "hdr/fenv_macros.h"
24 #include "hdr/types/fenv_t.h"
25 #include "src/__support/FPUtil/FPBits.h"
26 
27 namespace LIBC_NAMESPACE_DECL {
28 namespace fputil {
29 
30 struct FEnv {
31   struct FPState {
32     uint64_t StatusWord;
33     uint64_t ControlWord;
34   };
35 
36   static_assert(
37       sizeof(fenv_t) == sizeof(FPState),
38       "Internal floating point state does not match the public fenv_t type.");
39 
40   static constexpr uint32_t TONEAREST = 0x0;
41   static constexpr uint32_t UPWARD = 0x1;
42   static constexpr uint32_t DOWNWARD = 0x2;
43   static constexpr uint32_t TOWARDZERO = 0x3;
44 
45   // These will be the exception flags we use for exception values normalized
46   // from both status word and control word.
47   // We add EX_ prefix to the names since macOS <math.h> defines OVERFLOW and
48   // UNDERFLOW macros.
49   static constexpr uint32_t EX_INVALID = 0x1;
50   static constexpr uint32_t EX_DIVBYZERO = 0x2;
51   static constexpr uint32_t EX_OVERFLOW = 0x4;
52   static constexpr uint32_t EX_UNDERFLOW = 0x8;
53   static constexpr uint32_t EX_INEXACT = 0x10;
54   // __APPLE__ ARM64 has an extra flag that is raised when a denormal is flushed
55   // to zero.
56   static constexpr uint32_t EX_FLUSHTOZERO = 0x20;
57 
58   // Zero-th bit is the first bit.
59   static constexpr uint32_t ROUNDING_CONTROL_BIT_POSITION = 22;
60 
61   // In addition to the 5 floating point exceptions, macOS on arm64 defines
62   // another floating point exception: FE_FLUSHTOZERO, which is controlled by
63   // __fpcr_flush_to_zero bit in the FPCR register.  This control bit is
64   // located in a different place from FE_FLUSHTOZERO status bit relative to
65   // the other exceptions.
exception_value_from_statusFEnv66   LIBC_INLINE static uint32_t exception_value_from_status(int status) {
67     return ((status & FE_INVALID) ? EX_INVALID : 0) |
68            ((status & FE_DIVBYZERO) ? EX_DIVBYZERO : 0) |
69            ((status & FE_OVERFLOW) ? EX_OVERFLOW : 0) |
70            ((status & FE_UNDERFLOW) ? EX_UNDERFLOW : 0) |
71            ((status & FE_INEXACT) ? EX_INEXACT : 0) |
72            ((status & FE_FLUSHTOZERO) ? EX_FLUSHTOZERO : 0);
73   }
74 
exception_value_from_controlFEnv75   LIBC_INLINE static uint32_t exception_value_from_control(int control) {
76     return ((control & __fpcr_trap_invalid) ? EX_INVALID : 0) |
77            ((control & __fpcr_trap_divbyzero) ? EX_DIVBYZERO : 0) |
78            ((control & __fpcr_trap_overflow) ? EX_OVERFLOW : 0) |
79            ((control & __fpcr_trap_underflow) ? EX_UNDERFLOW : 0) |
80            ((control & __fpcr_trap_inexact) ? EX_INEXACT : 0) |
81            ((control & __fpcr_flush_to_zero) ? EX_FLUSHTOZERO : 0);
82   }
83 
exception_value_to_statusFEnv84   LIBC_INLINE static int exception_value_to_status(uint32_t excepts) {
85     return ((excepts & EX_INVALID) ? FE_INVALID : 0) |
86            ((excepts & EX_DIVBYZERO) ? FE_DIVBYZERO : 0) |
87            ((excepts & EX_OVERFLOW) ? FE_OVERFLOW : 0) |
88            ((excepts & EX_UNDERFLOW) ? FE_UNDERFLOW : 0) |
89            ((excepts & EX_INEXACT) ? FE_INEXACT : 0) |
90            ((excepts & EX_FLUSHTOZERO) ? FE_FLUSHTOZERO : 0);
91   }
92 
exception_value_to_controlFEnv93   LIBC_INLINE static int exception_value_to_control(uint32_t excepts) {
94     return ((excepts & EX_INVALID) ? __fpcr_trap_invalid : 0) |
95            ((excepts & EX_DIVBYZERO) ? __fpcr_trap_divbyzero : 0) |
96            ((excepts & EX_OVERFLOW) ? __fpcr_trap_overflow : 0) |
97            ((excepts & EX_UNDERFLOW) ? __fpcr_trap_underflow : 0) |
98            ((excepts & EX_INEXACT) ? __fpcr_trap_inexact : 0) |
99            ((excepts & EX_FLUSHTOZERO) ? __fpcr_flush_to_zero : 0);
100   }
101 
get_control_wordFEnv102   LIBC_INLINE static uint32_t get_control_word() { return __arm_rsr("fpcr"); }
103 
set_control_wordFEnv104   LIBC_INLINE static void set_control_word(uint32_t fpcr) {
105     __arm_wsr("fpcr", fpcr);
106   }
107 
get_status_wordFEnv108   LIBC_INLINE static uint32_t get_status_word() { return __arm_rsr("fpsr"); }
109 
set_status_wordFEnv110   LIBC_INLINE static void set_status_word(uint32_t fpsr) {
111     __arm_wsr("fpsr", fpsr);
112   }
113 };
114 
enable_except(int excepts)115 LIBC_INLINE int enable_except(int excepts) {
116   uint32_t new_excepts = FEnv::exception_value_from_status(excepts);
117   uint32_t control_word = FEnv::get_control_word();
118   uint32_t old_excepts = FEnv::exception_value_from_control(control_word);
119   if (new_excepts != old_excepts) {
120     control_word |= FEnv::exception_value_to_control(new_excepts);
121     FEnv::set_control_word(control_word);
122   }
123   return FEnv::exception_value_to_status(old_excepts);
124 }
125 
disable_except(int excepts)126 LIBC_INLINE int disable_except(int excepts) {
127   uint32_t disabled_excepts = FEnv::exception_value_from_status(excepts);
128   uint32_t control_word = FEnv::get_control_word();
129   uint32_t old_excepts = FEnv::exception_value_from_control(control_word);
130   control_word &= ~FEnv::exception_value_to_control(disabled_excepts);
131   FEnv::set_control_word(control_word);
132   return FEnv::exception_value_to_status(old_excepts);
133 }
134 
get_except()135 LIBC_INLINE int get_except() {
136   uint32_t control_word = FEnv::get_control_word();
137   uint32_t enabled_excepts = FEnv::exception_value_from_control(control_word);
138   return FEnv::exception_value_to_status(enabled_excepts);
139 }
140 
clear_except(int excepts)141 LIBC_INLINE int clear_except(int excepts) {
142   uint32_t status_word = FEnv::get_status_word();
143   uint32_t except_value = FEnv::exception_value_from_status(excepts);
144   status_word &= ~FEnv::exception_value_to_status(except_value);
145   FEnv::set_status_word(status_word);
146   return 0;
147 }
148 
test_except(int excepts)149 LIBC_INLINE int test_except(int excepts) {
150   uint32_t statusWord = FEnv::get_status_word();
151   uint32_t ex_value = FEnv::exception_value_from_status(excepts);
152   return statusWord & FEnv::exception_value_to_status(ex_value);
153 }
154 
set_except(int excepts)155 LIBC_INLINE int set_except(int excepts) {
156   uint32_t status_word = FEnv::get_status_word();
157   uint32_t new_exceptions = FEnv::exception_value_from_status(excepts);
158   status_word |= FEnv::exception_value_to_status(new_exceptions);
159   FEnv::set_status_word(status_word);
160   return 0;
161 }
162 
raise_except(int excepts)163 LIBC_INLINE int raise_except(int excepts) {
164   float zero = 0.0f;
165   float one = 1.0f;
166   float large_value = FPBits<float>::max_normal().get_val();
167   float small_value = FPBits<float>::min_normal().get_val();
168   auto divfunc = [](float a, float b) {
169     __asm__ __volatile__("ldr  s0, %0\n\t"
170                          "ldr  s1, %1\n\t"
171                          "fdiv s0, s0, s1\n\t"
172                          : // No outputs
173                          : "m"(a), "m"(b)
174                          : "s0", "s1" /* s0 and s1 are clobbered */);
175   };
176 
177   uint32_t to_raise = FEnv::exception_value_from_status(excepts);
178   int result = 0;
179 
180   if (to_raise & FEnv::EX_INVALID) {
181     divfunc(zero, zero);
182     uint32_t status_word = FEnv::get_status_word();
183     if (!(FEnv::exception_value_from_status(status_word) & FEnv::EX_INVALID))
184       result = -1;
185   }
186 
187   if (to_raise & FEnv::EX_DIVBYZERO) {
188     divfunc(one, zero);
189     uint32_t status_word = FEnv::get_status_word();
190     if (!(FEnv::exception_value_from_status(status_word) & FEnv::EX_DIVBYZERO))
191       result = -1;
192   }
193   if (to_raise & FEnv::EX_OVERFLOW) {
194     divfunc(large_value, small_value);
195     uint32_t status_word = FEnv::get_status_word();
196     if (!(FEnv::exception_value_from_status(status_word) & FEnv::EX_OVERFLOW))
197       result = -1;
198   }
199   if (to_raise & FEnv::EX_UNDERFLOW) {
200     divfunc(small_value, large_value);
201     uint32_t status_word = FEnv::get_status_word();
202     if (!(FEnv::exception_value_from_status(status_word) & FEnv::EX_UNDERFLOW))
203       result = -1;
204   }
205   if (to_raise & FEnv::EX_INEXACT) {
206     float two = 2.0f;
207     float three = 3.0f;
208     // 2.0 / 3.0 cannot be represented exactly in any radix 2 floating point
209     // format.
210     divfunc(two, three);
211     uint32_t status_word = FEnv::get_status_word();
212     if (!(FEnv::exception_value_from_status(status_word) & FEnv::EX_INEXACT))
213       result = -1;
214   }
215   if (to_raise & FEnv::EX_FLUSHTOZERO) {
216     // TODO: raise the flush to zero floating point exception.
217     result = -1;
218   }
219   return result;
220 }
221 
get_round()222 LIBC_INLINE int get_round() {
223   uint32_t rounding_mode =
224       (FEnv::get_control_word() >> FEnv::ROUNDING_CONTROL_BIT_POSITION) & 0x3;
225   switch (rounding_mode) {
226   case FEnv::TONEAREST:
227     return FE_TONEAREST;
228   case FEnv::DOWNWARD:
229     return FE_DOWNWARD;
230   case FEnv::UPWARD:
231     return FE_UPWARD;
232   case FEnv::TOWARDZERO:
233     return FE_TOWARDZERO;
234   default:
235     return -1; // Error value.
236   }
237 }
238 
set_round(int mode)239 LIBC_INLINE int set_round(int mode) {
240   uint16_t bit_value;
241   switch (mode) {
242   case FE_TONEAREST:
243     bit_value = FEnv::TONEAREST;
244     break;
245   case FE_DOWNWARD:
246     bit_value = FEnv::DOWNWARD;
247     break;
248   case FE_UPWARD:
249     bit_value = FEnv::UPWARD;
250     break;
251   case FE_TOWARDZERO:
252     bit_value = FEnv::TOWARDZERO;
253     break;
254   default:
255     return 1; // To indicate failure
256   }
257 
258   uint32_t control_word = FEnv::get_control_word();
259   control_word &= ~(0x3 << FEnv::ROUNDING_CONTROL_BIT_POSITION);
260   control_word |= (bit_value << FEnv::ROUNDING_CONTROL_BIT_POSITION);
261   FEnv::set_control_word(control_word);
262 
263   return 0;
264 }
265 
get_env(fenv_t * envp)266 LIBC_INLINE int get_env(fenv_t *envp) {
267   FEnv::FPState *state = reinterpret_cast<FEnv::FPState *>(envp);
268   state->ControlWord = FEnv::get_control_word();
269   state->StatusWord = FEnv::get_status_word();
270   return 0;
271 }
272 
set_env(const fenv_t * envp)273 LIBC_INLINE int set_env(const fenv_t *envp) {
274   if (envp == FE_DFL_ENV) {
275     // Default status and control words bits are all zeros so we just
276     // write zeros.
277     FEnv::set_status_word(0);
278     FEnv::set_control_word(0);
279     return 0;
280   }
281   const FEnv::FPState *state = reinterpret_cast<const FEnv::FPState *>(envp);
282   FEnv::set_control_word(static_cast<uint32_t>(state->ControlWord));
283   FEnv::set_status_word(static_cast<uint32_t>(state->StatusWord));
284   return 0;
285 }
286 
287 } // namespace fputil
288 } // namespace LIBC_NAMESPACE_DECL
289 
290 #endif // LLVM_LIBC_SRC___SUPPORT_FPUTIL_AARCH64_FENV_DARWIN_IMPL_H
291