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