[libc][NFC] Move aligned access implementations to separate header
[llvm-project.git] / libc / src / __support / FPUtil / rounding_mode.h
blob28e2189ccb012397b20778e2855313ee4a573349
1 //===---- Free-standing function to detect rounding mode --------*- 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 //===----------------------------------------------------------------------===//
9 #ifndef LLVM_LIBC_SRC_SUPPORT_FPUTIL_ROUNDING_MODE_H
10 #define LLVM_LIBC_SRC_SUPPORT_FPUTIL_ROUNDING_MODE_H
12 #include "src/__support/macros/attributes.h" // LIBC_INLINE
14 #include <fenv.h>
16 namespace __llvm_libc::fputil {
18 // Quick free-standing test whether fegetround() == FE_UPWARD.
19 // Using the following observation:
20 // 1.0f + 2^-25 = 1.0f for FE_TONEAREST, FE_DOWNWARD, FE_TOWARDZERO
21 // = 0x1.000002f for FE_UPWARD.
22 LIBC_INLINE bool fenv_is_round_up() {
23 volatile float x = 0x1.0p-25f;
24 return (1.0f + x != 1.0f);
27 // Quick free-standing test whether fegetround() == FE_DOWNWARD.
28 // Using the following observation:
29 // -1.0f - 2^-25 = -1.0f for FE_TONEAREST, FE_UPWARD, FE_TOWARDZERO
30 // = -0x1.000002f for FE_DOWNWARD.
31 LIBC_INLINE bool fenv_is_round_down() {
32 volatile float x = 0x1.0p-25f;
33 return (-1.0f - x != -1.0f);
36 // Quick free-standing test whether fegetround() == FE_TONEAREST.
37 // Using the following observation:
38 // 1.5f + 2^-24 = 1.5f for FE_TONEAREST, FE_DOWNWARD, FE_TOWARDZERO
39 // = 0x1.100002p0f for FE_UPWARD,
40 // 1.5f - 2^-24 = 1.5f for FE_TONEAREST, FE_UPWARD
41 // = 0x1.0ffffep-1f for FE_DOWNWARD, FE_TOWARDZERO
42 LIBC_INLINE bool fenv_is_round_to_nearest() {
43 static volatile float x = 0x1.0p-24f;
44 float y = x;
45 return (1.5f + y == 1.5f - y);
48 // Quick free-standing test whether fegetround() == FE_TOWARDZERO.
49 // Using the following observation:
50 // 1.0f + 2^-23 + 2^-24 = 0x1.000002p0f for FE_DOWNWARD, FE_TOWARDZERO
51 // = 0x1.000004p0f for FE_TONEAREST, FE_UPWARD,
52 // -1.0f - 2^-24 = -1.0f for FE_TONEAREST, FE_UPWARD, FE_TOWARDZERO
53 // = -0x1.000002p0f for FE_DOWNWARD
54 // So:
55 // (0x1.000002p0f + 2^-24) + (-1.0f - 2^-24) = 2^-23 for FE_TOWARDZERO
56 // = 2^-22 for FE_TONEAREST, FE_UPWARD
57 // = 0 for FE_DOWNWARD
58 LIBC_INLINE bool fenv_is_round_to_zero() {
59 static volatile float x = 0x1.0p-24f;
60 float y = x;
61 return ((0x1.000002p0f + y) + (-1.0f - y) == 0x1.0p-23f);
64 // Quick free standing get rounding mode based on the above observations.
65 LIBC_INLINE int quick_get_round() {
66 static volatile float x = 0x1.0p-24f;
67 float y = x;
68 float z = (0x1.000002p0f + y) + (-1.0f - y);
70 if (z == 0.0f)
71 return FE_DOWNWARD;
72 if (z == 0x1.0p-23f)
73 return FE_TOWARDZERO;
74 return (2.0f + y == 2.0f) ? FE_TONEAREST : FE_UPWARD;
77 } // namespace __llvm_libc::fputil
79 #endif // LLVM_LIBC_SRC_SUPPORT_FPUTIL_ROUNDING_MODE_H