SH4ZAM! 0.1.0
Fast math library for the Sega Dreamcast's SH4 CPU
Loading...
Searching...
No Matches
shz_trig.hpp
Go to the documentation of this file.
1/*! \file
2 * \brief C++ trigonometry API.
3 * \ingroup trig
4 *
5 * This file provides an API offering fast versions of trigonometry functions
6 * for C++23.
7 *
8 * \author 2025, 2026 Falco Girgis
9 * \copyright MIT License
10 */
11
12#ifndef SHZ_TRIG_HPP
13#define SHZ_TRIG_HPP
14
15#include <tuple>
16#include <utility>
17
18#include "shz_trig.h"
19
20namespace shz {
21 //! Floating-point constant approximation for Pi.
22 constexpr float pi_f = SHZ_F_PI;
23 //! Floating-point constant approximation for Pi/2.
24 constexpr float pi_f_2 = SHZ_F_PI_2;
25 //! Floating-point constant approximation for Pi/4.
26 constexpr float pi_f_4 = SHZ_F_PI_4;
27 //! Scaling factor used to scale the input to FSCA from radians.
29 //! Scaling factor used to scale the input to FSCA from degrees.
31
32 //! Converts degrees to radians.
33 SHZ_FORCE_INLINE constexpr float deg_to_rad(float deg) noexcept { return SHZ_DEG_TO_RAD(deg); }
34 //! Converts radians to degrees.
35 SHZ_FORCE_INLINE constexpr float rad_to_deg(float rad) noexcept { return SHZ_RAD_TO_DEG(rad); }
36
37 /*! C++ sine/cosine approximation pairs.
38
39 This structure contains the precomputed values for both sine
40 and cosine of an angle.
41 */
42 struct sincos: shz_sincos_t {
43 //! Converting constructor from C struct.
44 SHZ_FORCE_INLINE sincos(shz_sincos_t val) noexcept:
45 shz_sincos_t(val) {}
46
47 //! Returns a new sin/cos pair from the given angle in radians.
48 SHZ_FORCE_INLINE static sincos from_radians(float rad) noexcept {
49 return shz_sincosf(rad);
50 }
51
52 //! Returns a new sin/cos pair from the given 16-bit integer angle in radians.
53 SHZ_FORCE_INLINE static sincos from_radians(uint16_t rad) noexcept {
54 return shz_sincosu16(rad);
55 }
56
57 //! Returns a new sin/cos pair from the given angle in degrees.
58 SHZ_FORCE_INLINE static sincos from_degrees(float deg) noexcept {
59 return shz_sincosf_deg(deg);
60 }
61
62 //! Returns the sine component from the pair.
63 SHZ_FORCE_INLINE float sinf() const noexcept { return this->sin; }
64 //! Returns the cosine component from the pair.
65 SHZ_FORCE_INLINE float cosf() const noexcept { return this->cos; }
66 //! Calculates the tangent from the given pair.
67 SHZ_FORCE_INLINE float tanf() const noexcept { return shz_sincos_tanf(*this); }
68 //! Calculates the secant from the given pair.
69 SHZ_FORCE_INLINE float secf() const noexcept { return shz_sincos_secf(*this); }
70 //! Calculates the cosecant from the given pair.
71 SHZ_FORCE_INLINE float cscf() const noexcept { return shz_sincos_cscf(*this); }
72 //! Calculates the cotangent from the given pair.
73 SHZ_FORCE_INLINE float cotf() const noexcept { return shz_sincos_cotf(*this); }
74
75 // Conversion operator to a std::pair<> of two floats, for sine and cosine values.
76 SHZ_FORCE_INLINE operator std::pair<float, float>() const noexcept {
77 return std::pair(sinf(), cosf());
78 }
79 };
80
81 //! C++ alias for sincos for those who like POSIX-style typenames.
82 using sincos_t = sincos;
83
84 /*! \name Sine/Cosine pairs
85 \brief Routines operating on pairs of sine and cosine values.
86 @{
87 */
88
89 //! C++ wrapper around shz_sincosu16().
90 constexpr auto sincosu16 = shz_sincosu16;
91 //! C++ wrapper around shz_sincosf().
92 constexpr auto sincosf = shz_sincosf;
93 //! C++ wrapper around shz_sincosf_deg().
94 constexpr auto sincosf_deg = shz_sincosf_deg;
95 //! C++ wrapper around shz_sincos_tanf().
96 constexpr auto sincos_tanf = shz_sincos_tanf;
97 //! C++ wrapper around shz_sincos_secf().
98 constexpr auto sincos_secf = shz_sincos_secf;
99 //! C++ wrapper around shz_sincos_cscf().
100 constexpr auto sincos_cscf = shz_sincos_cscf;
101 //! C++ wrapper around shz_sincos_cotf().
102 constexpr auto sincos_cotf = shz_sincos_cotf;
103
104 //! @}
105
106 /*! \name One-off Trig Functions
107 \brief Routines for calculating results of single trig functions.
108 @{
109 */
110
111 //! C++ wrapper around shz_sinf().
112 constexpr auto sinf = shz_sinf;
113 //! C++ wrapper around shz_sinf_deg().
114 constexpr auto sinf_deg = shz_sinf_deg;
115 //! C++ wrapper around shz_cosf().
116 constexpr auto cosf = shz_cosf;
117 //! C++ wrapper around shz_cosf_deg().
118 constexpr auto cosf_deg = shz_cosf_deg;
119 //! C++ wrapper around shz_tanf().
120 constexpr auto tanf = shz_tanf;
121 //! C++ wrapper around shz_tanf_deg().
122 constexpr auto tanf_deg = shz_tanf_deg;
123 //! C++ wrapper around shz_secf().
124 constexpr auto secf = shz_secf;
125 //! C++ wrapper around shz_secf_deg().
126 constexpr auto secf_deg = shz_secf_deg;
127 //! C++ wrapper around shz_cscf().
128 constexpr auto cscf = shz_cscf;
129 //! C++ wrapper around shz_cscf_deg().
130 constexpr auto cscf_deg = shz_cscf_deg;
131 //! C++ wrapper around shz_cotf().
132 constexpr auto cotf = shz_cotf;
133 //! C++ wrapper around shz_cotf_deg().
134 constexpr auto cotf_deg = shz_cotf_deg;
135
136 //! @}
137
138 /*! \name Inverse Trig Functions
139 \brief Routines for calculating results of inverse trig functions.
140 @{
141 */
142
143 //! C++ wrapper around shz_atanf_unit().
144 constexpr auto atanf_unit = shz_atanf_unit;
145 //! C++ wrapper around shz_atanf_q1().
146 constexpr auto atanf_q1 = shz_atanf_q1;
147 //! C++ wrapper around shz_atanf().
148 constexpr auto atanf = shz_atanf;
149 //! C++ wrapper around shz_atan2f().
150 constexpr auto atan2f = shz_atan2f;
151 //! C++ wrapper around shz_asinf().
152 constexpr auto asinf = shz_asinf;
153 //! C++ wrapper around shz_acosf().
154 constexpr auto acosf = shz_acosf;
155 //! C++ wrapper around shz_asecf().
156 constexpr auto asecf = shz_asecf;
157 //! C++ wrapper around shz_acscf().
158 constexpr auto acscf = shz_acscf;
159 //! C++ wrapper around shz_acotf().
160 constexpr auto acotf = shz_acotf;
161
162 //! @}
163
164
165 /*! \name Hyperbolic Functions
166 \brief Trigonometric functions for hyperbolas
167 @{
168 */
169
170 //! C++ wrapper around shz_sinhf().
171 SHZ_FORCE_INLINE float sinhf(float x) noexcept { return shz_sinhf(x); }
172 //! C++ wrapper around shz_coshf().
173 SHZ_FORCE_INLINE float coshf(float x) noexcept { return shz_coshf(x); }
174 //! C++ wrapper around shz_tanhf().
175 SHZ_FORCE_INLINE float tanhf(float x) noexcept { return shz_tanhf(x); }
176 //! C++ wrapper around shz_cschf().
177 SHZ_FORCE_INLINE float cschf(float x) noexcept { return shz_cschf(x); }
178 //! C++ wrapper around shz_sechf().
179 SHZ_FORCE_INLINE float sechf(float x) noexcept { return shz_sechf(x); }
180 //! C++ wrapper around shz_cothf().
181 SHZ_FORCE_INLINE float cothf(float x) noexcept { return shz_cothf(x); }
182
183 //! @}
184
185 /*! \name Inverse Hyperbolic Functions
186 \brief Inverse trigonometric functions for hyperbolas
187 @{
188 */
189
190 //! C++ wrapper around shz_asinhf().
191 SHZ_FORCE_INLINE float asinhf(float x) noexcept { return shz_asinhf(x); }
192 //! C++ wrapper around shz_acoshf().
193 SHZ_FORCE_INLINE float acoshf(float x) noexcept { return shz_acoshf(x); }
194 //! C++ wrapper around shz_atanhf().
195 SHZ_FORCE_INLINE float atanhf(float x) noexcept { return shz_atanhf(x); }
196 //! C++ wrapper around shz_acscf().
197 SHZ_FORCE_INLINE float acschf(float x) noexcept { return shz_acschf(x); }
198 //! C++ wrapper around shz_asechf().
199 SHZ_FORCE_INLINE float asechf(float x) noexcept { return shz_asechf(x); }
200 //! C++ wrapper around shz_acothf().
201 SHZ_FORCE_INLINE float acothf(float x) noexcept { return shz_acothf(x); }
202
203 //! @}
204}
205
206#endif
Namespace enclosing the SH4ZAM C++ API.
Definition shz_cdefs.hpp:21
constexpr float rad_to_deg(float rad) noexcept
Converts radians to degrees.
Definition shz_trig.hpp:35
constexpr auto sincos_cotf
C++ wrapper around shz_sincos_cotf().
Definition shz_trig.hpp:102
float coshf(float x) noexcept
C++ wrapper around shz_coshf().
Definition shz_trig.hpp:173
constexpr auto tanf_deg
C++ wrapper around shz_tanf_deg().
Definition shz_trig.hpp:122
constexpr float deg_to_rad(float deg) noexcept
Converts degrees to radians.
Definition shz_trig.hpp:33
constexpr auto asecf
C++ wrapper around shz_asecf().
Definition shz_trig.hpp:156
constexpr auto sinf_deg
C++ wrapper around shz_sinf_deg().
Definition shz_trig.hpp:114
float cschf(float x) noexcept
C++ wrapper around shz_cschf().
Definition shz_trig.hpp:177
constexpr auto cscf
C++ wrapper around shz_cscf().
Definition shz_trig.hpp:128
constexpr auto secf_deg
C++ wrapper around shz_secf_deg().
Definition shz_trig.hpp:126
constexpr auto sinf
C++ wrapper around shz_sinf().
Definition shz_trig.hpp:112
float acoshf(float x) noexcept
C++ wrapper around shz_acoshf().
Definition shz_trig.hpp:193
constexpr auto sincos_secf
C++ wrapper around shz_sincos_secf().
Definition shz_trig.hpp:98
constexpr auto sincos_tanf
C++ wrapper around shz_sincos_tanf().
Definition shz_trig.hpp:96
constexpr auto acosf
C++ wrapper around shz_acosf().
Definition shz_trig.hpp:154
constexpr float fsca_rad_factor
Scaling factor used to scale the input to FSCA from radians.
Definition shz_trig.hpp:28
constexpr auto tanf
C++ wrapper around shz_tanf().
Definition shz_trig.hpp:120
constexpr float pi_f
Floating-point constant approximation for Pi.
Definition shz_trig.hpp:22
constexpr auto atanf_q1
C++ wrapper around shz_atanf_q1().
Definition shz_trig.hpp:146
float sechf(float x) noexcept
C++ wrapper around shz_sechf().
Definition shz_trig.hpp:179
constexpr auto cosf
C++ wrapper around shz_cosf().
Definition shz_trig.hpp:116
constexpr float pi_f_2
Floating-point constant approximation for Pi/2.
Definition shz_trig.hpp:24
float cothf(float x) noexcept
C++ wrapper around shz_cothf().
Definition shz_trig.hpp:181
constexpr auto cscf_deg
C++ wrapper around shz_cscf_deg().
Definition shz_trig.hpp:130
float asinhf(float x) noexcept
C++ wrapper around shz_asinhf().
Definition shz_trig.hpp:191
constexpr auto secf
C++ wrapper around shz_secf().
Definition shz_trig.hpp:124
constexpr auto atanf
C++ wrapper around shz_atanf().
Definition shz_trig.hpp:148
constexpr auto cosf_deg
C++ wrapper around shz_cosf_deg().
Definition shz_trig.hpp:118
constexpr auto cotf
C++ wrapper around shz_cotf().
Definition shz_trig.hpp:132
float tanhf(float x) noexcept
C++ wrapper around shz_tanhf().
Definition shz_trig.hpp:175
float acothf(float x) noexcept
C++ wrapper around shz_acothf().
Definition shz_trig.hpp:201
constexpr float fsca_deg_factor
Scaling factor used to scale the input to FSCA from degrees.
Definition shz_trig.hpp:30
float asechf(float x) noexcept
C++ wrapper around shz_asechf().
Definition shz_trig.hpp:199
constexpr auto sincos_cscf
C++ wrapper around shz_sincos_cscf().
Definition shz_trig.hpp:100
constexpr auto acscf
C++ wrapper around shz_acscf().
Definition shz_trig.hpp:158
float sinhf(float x) noexcept
C++ wrapper around shz_sinhf().
Definition shz_trig.hpp:171
constexpr auto acotf
C++ wrapper around shz_acotf().
Definition shz_trig.hpp:160
constexpr auto sincosf_deg
C++ wrapper around shz_sincosf_deg().
Definition shz_trig.hpp:94
constexpr float pi_f_4
Floating-point constant approximation for Pi/4.
Definition shz_trig.hpp:26
constexpr auto atanf_unit
C++ wrapper around shz_atanf_unit().
Definition shz_trig.hpp:144
float atanhf(float x) noexcept
C++ wrapper around shz_atanhf().
Definition shz_trig.hpp:195
constexpr auto sincosf
C++ wrapper around shz_sincosf().
Definition shz_trig.hpp:92
float acschf(float x) noexcept
C++ wrapper around shz_acscf().
Definition shz_trig.hpp:197
constexpr auto atan2f
C++ wrapper around shz_atan2f().
Definition shz_trig.hpp:150
constexpr auto asinf
C++ wrapper around shz_asinf().
Definition shz_trig.hpp:152
constexpr auto sincosu16
C++ wrapper around shz_sincosu16().
Definition shz_trig.hpp:90
constexpr auto cotf_deg
C++ wrapper around shz_cotf_deg().
Definition shz_trig.hpp:134
float shz_cscf(float radians) SHZ_NOEXCEPT
One-off routine for returning only cosecant (1 / sin(x)) from an angle in radians.
float shz_acotf(float x) SHZ_NOEXCEPT
Fast arccotangent/inverse cotangent approximation; taking units in radians.
float shz_cothf(float x) SHZ_NOEXCEPT
Fast hyperbolic cotangent function.
float shz_tanf(float radians) SHZ_NOEXCEPT
One-off routine for returning only tanf() from an angle in radians.
#define SHZ_F_PI
Single-precision floating-point PI approximation (do not use M_PI!)
Definition shz_trig.h:27
float shz_cosf_deg(float degrees) SHZ_NOEXCEPT
One-off routine for returning only cosf() from an angle in degrees.
float shz_acosf(float x) SHZ_NOEXCEPT
Fast arccosine approximation; equivalent to C's acosf().
float shz_sincos_secf(shz_sincos_t sincos) SHZ_NOEXCEPT
Returns the floating-point secant (1.0f / cosf(x)) from the given pre-computed sincos pair.
float shz_sincos_cotf(shz_sincos_t sincos) SHZ_NOEXCEPT
Returns the floating-point cotangent (1.0f / tanf(x)) from the given pre-computed sincos pair.
shz_sincos_t shz_sincosu16(uint16_t radians16) SHZ_NOEXCEPT
Returns sinf()/cosf() pairs for the given unsigned 16-bit angle in radians.
float shz_acothf(float x) SHZ_NOEXCEPT
Fast hyperbolic arccotangent function.
float shz_cschf(float x) SHZ_NOEXCEPT
Fast hyperbolic cosecant function.
float shz_tanf_deg(float degrees) SHZ_NOEXCEPT
One-off routine for returning only tanf() from an angle in degrees.
#define SHZ_FSCA_DEG_FACTOR
Multiplicative factor for passing the FSCA instrution angles in degrees.
Definition shz_trig.h:35
#define SHZ_F_PI_4
Single-precision FP PI approximation divided by 4.
Definition shz_trig.h:31
float shz_atanf(float x) SHZ_NOEXCEPT
Fast arctangent approximation; equvalent to C's atanf().
float shz_atanf_unit(float x) SHZ_NOEXCEPT
Fast arctangent approximation for unit values between 0.0f and 1.0f.
float shz_secf(float radians) SHZ_NOEXCEPT
One-off routine for returning only secant (1 / cos(x)) from an angle in radians.
#define SHZ_FSCA_RAD_FACTOR
Multiplicative factor for passing the FSCA instruction angles in radians.
Definition shz_trig.h:33
float shz_coshf(float x) SHZ_NOEXCEPT
Fast hyperbolic cosine function.
float shz_acoshf(float x) SHZ_NOEXCEPT
Fast hyperbolic arccosine function.
shz_sincos_t shz_sincosf_deg(float degrees) SHZ_NOEXCEPT
Returns sinf/cosf() pairs for the given floating-point angle in degrees.
shz_sincos_t shz_sincosf(float radians) SHZ_NOEXCEPT
Returns sinf()/cosf() pairs for the given floating-point angle in radians.
#define SHZ_RAD_TO_DEG(rad)
Converts the given angle in radians to degrees.
Definition shz_trig.h:42
float shz_cosf(float radians) SHZ_NOEXCEPT
One-off routine for returning only cosf() from an angle in radians.
float shz_sincos_cscf(shz_sincos_t sincos) SHZ_NOEXCEPT
Returns the floating-point cosecant (1.0f / sinf(x)) from the given pre-computed sincos pair.
float shz_sinf(float radians) SHZ_NOEXCEPT
One-off routine for returning only sinf() from an angle in radians.
float shz_sechf(float x) SHZ_NOEXCEPT
Fast hyperbolic secant function.
float shz_cotf(float radians) SHZ_NOEXCEPT
One-off routine for returning only cotangent (1 / tan(x)) from an angle in radians.
float shz_cotf_deg(float degrees) SHZ_NOEXCEPT
One-off routine for returning only cotangent (1 / cot(x)) from an angle in degrees.
float shz_acscf(float x) SHZ_NOEXCEPT
Fast arccosecant/inverse cosecant approximation; taking units in radians.
float shz_asechf(float x) SHZ_NOEXCEPT
Fast hyperbolic arcsecant function.
float shz_atanhf(float x) SHZ_NOEXCEPT
Fast hyperbolic arctangent function.
float shz_asinf(float x) SHZ_NOEXCEPT
Fast arcsine approximation; equivalent to C's asinf().
float shz_cscf_deg(float degrees) SHZ_NOEXCEPT
One-off routine for returning only cosecant (1 / sin(x)) from an angle in degrees.
float shz_asecf(float x) SHZ_NOEXCEPT
Fast arcsecant/inverse secant approximation, taking units in radians.
float shz_atan2f(float y, float x) SHZ_NOEXCEPT
Computes arctangent of y / x, using the signs of arguments to determine correct quadtrant....
float shz_secf_deg(float degrees) SHZ_NOEXCEPT
One-off routine for returning only secant (1 / cos(x)) from an angle in degrees.
float shz_sinf_deg(float degrees) SHZ_NOEXCEPT
One-off routine for returning only sinf() from an angle in degrees.
float shz_asinhf(float x) SHZ_NOEXCEPT
Fast hyperbolic arcsine function.
float shz_sinhf(float x) SHZ_NOEXCEPT
Fast hyperbolic sine function.
float shz_tanhf(float x) SHZ_NOEXCEPT
Fast hyperbolic tangent function.
float shz_atanf_q1(float x) SHZ_NOEXCEPT
Fast arctangent approximation for values lying within the first quadrant (>= 0.0f).
#define SHZ_DEG_TO_RAD(deg)
Converts the given angle in degrees to radians.
Definition shz_trig.h:40
float shz_sincos_tanf(shz_sincos_t sincos) SHZ_NOEXCEPT
Returns tanf() from the given pre-computed sincos pair.
#define SHZ_F_PI_2
Single-precision FP PI approximation divided by 2.
Definition shz_trig.h:29
float shz_acschf(float x) SHZ_NOEXCEPT
Fast hyperbolic arccosecant function.
C++ sine/cosine approximation pairs.
Definition shz_trig.hpp:42
float secf() const noexcept
Calculates the secant from the given pair.
Definition shz_trig.hpp:69
static sincos from_radians(float rad) noexcept
Returns a new sin/cos pair from the given angle in radians.
Definition shz_trig.hpp:48
float sinf() const noexcept
Returns the sine component from the pair.
Definition shz_trig.hpp:63
sincos(shz_sincos_t val) noexcept
Converting constructor from C struct.
Definition shz_trig.hpp:44
float cotf() const noexcept
Calculates the cotangent from the given pair.
Definition shz_trig.hpp:73
static sincos from_radians(uint16_t rad) noexcept
Returns a new sin/cos pair from the given 16-bit integer angle in radians.
Definition shz_trig.hpp:53
float cosf() const noexcept
Returns the cosine component from the pair.
Definition shz_trig.hpp:65
float cscf() const noexcept
Calculates the cosecant from the given pair.
Definition shz_trig.hpp:71
static sincos from_degrees(float deg) noexcept
Returns a new sin/cos pair from the given angle in degrees.
Definition shz_trig.hpp:58
float tanf() const noexcept
Calculates the tangent from the given pair.
Definition shz_trig.hpp:67
float cos
cosf() approximation for the angle
Definition shz_trig.h:57
float sin
sinf() approximation for the angle
Definition shz_trig.h:56