SH4ZAM! 0.1.0
Fast math library for the Sega Dreamcast's SH4 CPU
Loading...
Searching...
No Matches
shz_complex.h
Go to the documentation of this file.
1/*! \file
2 \brief Complex number API
3 \ingroup complex
4
5 This file contains a collection of routines for working with complex
6 (real + imaginary) numbers. The API is mostly modeled after C99's
7 <complex.h>, although it also supports being used from C++ as well.
8
9 \author 2026 Falco Girgis
10 \copyright MIT License
11
12 \todo
13 - to/from shz_mat2x2_t and XMTRX
14 - FFT utilities
15 - inverse FFT
16*/
17
18#ifndef SHZ_COMPLEX_H
19#define SHZ_COMPLEX_H
20
21#include "shz_cdefs.h"
22#include <stddef.h>
23#include <stdbool.h>
24
25/*! \defgroup complex Complex
26 \brief API for representing and operating on complex numbers.
27
28 The complex number API serves as a replacement for C and C++'s <complex.h>,
29 offering fast, accelerated approximations for each routine. The API also
30 features shz_fft(), an SH4 optimized fast-fourier transform.
31*/
32
33/*! \name Preprocessor
34 \brief Macros and preprocessor constants.
35 @{
36*/
37
38#define SHZ_CMPLXF(x, y) shz_cinitf(x, y) //!< Initializes a complex number.
39#define SHZ_CMPLXF32(x, y) SHZ_CMPLXF(x, y) //!< Initializes a complex number.
40#define SHZ_I SHZ_CMPLXF32(0.0f, 1.0f) //!< Unit imaginary constant, 'I'.
41
42//! @}
43
44SHZ_DECLS_BEGIN
45
46//! Represents a floating-point complex number real/imaginary pair.
47typedef struct shz_complex {
48 float real; //!< The real portion of the complex number.
49 float imag; //!< The imaginary portion of the complex number.
50} shz_complex_t;
51
52//! shz_complex_t alias for those who don't like POSIX-style.
53typedef shz_complex_t shz_complex;
54
55/*! \name Basic
56 \brief Basic operations on complex numbers.
57 @{
58*/
59
60//! Returns a complex number which has been initialized to contain the given component values.
61SHZ_INLINE shz_complex_t shz_cinitf(float real, float imag) SHZ_NOEXCEPT;
62
63//! Returns a complex number with a magnitude of \p r, and a phase angle of \p theta (in radians).
64SHZ_INLINE shz_complex_t shz_cpolarf(float r, float theta) SHZ_NOEXCEPT;
65
66//! Checks for equality between two complex numbers with either an absolute or relative tolerance.
67SHZ_INLINE bool shz_cequalf(shz_complex_t lhs, shz_complex_t rhs) SHZ_NOEXCEPT;
68
69//! Returns the real component of a complex number.
70SHZ_INLINE float shz_crealf(shz_complex_t c) SHZ_NOEXCEPT;
71
72//! Returns the imaginary component of a complex number.
73SHZ_INLINE float shz_cimagf(shz_complex_t c) SHZ_NOEXCEPT;
74
75//! @}
76
77/*! \name Arithmetic
78 \brief Arithmetic operations on complex numbers.
79 @{
80*/
81
82//! Adds two complex numbers together and returns the result.
83SHZ_INLINE shz_complex_t shz_caddf(shz_complex_t lhs, shz_complex_t rhs) SHZ_NOEXCEPT;
84
85//! Subtracts \p rhs from \p lhs and returns the complex result.
86SHZ_INLINE shz_complex_t shz_csubf(shz_complex_t lhs, shz_complex_t rhs) SHZ_NOEXCEPT;
87
88//! Multiplies two complex numbers together, returning the complex result.
89SHZ_INLINE shz_complex_t shz_cmulf(shz_complex_t lhs, shz_complex_t rhs) SHZ_NOEXCEPT;
90
91//! Divides \p lhs by \p rhs, returning the complex result.
92SHZ_INLINE shz_complex_t shz_cdivf(shz_complex_t lhs, shz_complex_t rhs) SHZ_NOEXCEPT;
93
94//! Scales both componets of the complex number, \p c, by the value, \p v, returning the result.
95SHZ_INLINE shz_complex_t shz_cscalef(shz_complex_t c, float v) SHZ_NOEXCEPT;
96
97//! Returns the multiplicative inverse or reciprocal of the complex number, \p c.
98SHZ_INLINE shz_complex_t shz_crecipf(shz_complex_t c) SHZ_NOEXCEPT;
99
100//! @}
101
102/*! \name Manipulation
103 \brief Extract or modify complex components.
104 @{
105*/
106
107//! Returns the absolute value (or magnitude) of the complex number, \p c.
108SHZ_INLINE float shz_cabsf(shz_complex_t c) SHZ_NOEXCEPT;
109
110//! Returns the inverse of the absolute value (or inverse of the magnitude) of the complex number, \p c.
111SHZ_INLINE float shz_inv_cabsf(shz_complex_t c) SHZ_NOEXCEPT;
112
113//! Returns the squared magnitude of the complex number, \p c.
114SHZ_INLINE float shz_cnormf(shz_complex_t c) SHZ_NOEXCEPT;
115
116//! Returns the phase angle of the complex number, \p c.
117SHZ_INLINE float shz_cargf(shz_complex_t c) SHZ_NOEXCEPT;
118
119//! Returns the complex conjugate of \p c.
120SHZ_INLINE shz_complex_t shz_conjf(shz_complex_t c) SHZ_NOEXCEPT;
121
122//! Calculates the projection of the complex number, \p c, onto the Riemann sphere.
123SHZ_INLINE shz_complex_t shz_cprojf(shz_complex_t c) SHZ_NOEXCEPT;
124
125//! @}
126
127/*! \name Transcendental
128 \brief Complex transcendental functions.
129 @{
130*/
131
132//! Computes and returns the complex square root of \p c.
133SHZ_INLINE shz_complex_t shz_csqrtf(shz_complex_t c) SHZ_NOEXCEPT;
134
135//! Raises the complex \p base to the complex \p exp power, returning a complex result.
136SHZ_INLINE shz_complex_t shz_cpowf(shz_complex_t base, shz_complex_t exp) SHZ_NOEXCEPT;
137
138//! Returns the complex base `e` exponential of \p c.
139SHZ_INLINE shz_complex_t shz_cexpf(shz_complex_t c) SHZ_NOEXCEPT;
140
141//! Computes and returns the complex natural logarithm of \p c.
142SHZ_INLINE shz_complex_t shz_clogf(shz_complex_t c) SHZ_NOEXCEPT;
143
144//! Computes and returns the complex base 10 logarithm of \p c.
145SHZ_INLINE shz_complex_t shz_clog10f(shz_complex_t c) SHZ_NOEXCEPT;
146
147//! @}
148
149/*! \name Spherical Trigonometry
150 \brief Complex spherical trigonometric functions.
151 @{
152*/
153
154//! Calculates and returns the complex sine of \p c.
155SHZ_INLINE shz_complex_t shz_csinf(shz_complex_t c) SHZ_NOEXCEPT;
156
157//! Calculates and returns the complex cosine of \p c.
158SHZ_INLINE shz_complex_t shz_ccosf(shz_complex_t c) SHZ_NOEXCEPT;
159
160//! Calculates and returns the complex tangent of \p c.
161SHZ_INLINE shz_complex_t shz_ctanf(shz_complex_t c) SHZ_NOEXCEPT;
162
163//! Calculates and returns the complex cosecant of \p c.
164SHZ_INLINE shz_complex_t shz_ccscf(shz_complex_t c) SHZ_NOEXCEPT;
165
166//! Calculates and returns the complex secant of \p c.
167SHZ_INLINE shz_complex_t shz_csecf(shz_complex_t c) SHZ_NOEXCEPT;
168
169//! Calculates and returns the complex cotangent of \p c.
170SHZ_INLINE shz_complex_t shz_ccotf(shz_complex_t c) SHZ_NOEXCEPT;
171
172//! Calculates and returns the complex arcsine of \p c.
173SHZ_INLINE shz_complex_t shz_casinf(shz_complex_t c) SHZ_NOEXCEPT;
174
175//! Calculates and returns the complex arccosine of \p c.
176SHZ_INLINE shz_complex_t shz_cacosf(shz_complex_t c) SHZ_NOEXCEPT;
177
178//! Calculates and returns the complex arctangent of \p c.
179SHZ_INLINE shz_complex_t shz_catanf(shz_complex_t c) SHZ_NOEXCEPT;
180
181//! Calculates and returns the complex arccosecant of \p c.
182SHZ_INLINE shz_complex_t shz_cacscf(shz_complex_t c) SHZ_NOEXCEPT;
183
184//! Calculates and returns the complex arcsecant of \p c.
185SHZ_INLINE shz_complex_t shz_casecf(shz_complex_t c) SHZ_NOEXCEPT;
186
187//! Calculates and returns the complex arccotangent of \p c.
188SHZ_INLINE shz_complex_t shz_cacotf(shz_complex_t c) SHZ_NOEXCEPT;
189
190//! @}
191
192/*! \name Hyperbolic Trigonometry
193 \brief Complex hyperbolic trigonometric functions.
194 @{
195*/
196
197//! Calculates and returns the complex hyperbolic sine of \p c.
198SHZ_INLINE shz_complex_t shz_csinhf(shz_complex_t c) SHZ_NOEXCEPT;
199
200//! Calculates and returns the complex hyperbolic cosine of \p c.
201SHZ_INLINE shz_complex_t shz_ccoshf(shz_complex_t c) SHZ_NOEXCEPT;
202
203//! Calculates and returns the complex hyperbolic tangent of \p c.
204SHZ_INLINE shz_complex_t shz_ctanhf(shz_complex_t c) SHZ_NOEXCEPT;
205
206//! Calculates and returns the complex hyperbolic cosecant of \p c.
207SHZ_INLINE shz_complex_t shz_ccschf(shz_complex_t c) SHZ_NOEXCEPT;
208
209//! Calculates and returns the complex hyperbolic secant of \p c.
210SHZ_INLINE shz_complex_t shz_csechf(shz_complex_t c) SHZ_NOEXCEPT;
211
212//! Calculates and returns the complex hyperbolic cotangent of \p c.
213SHZ_INLINE shz_complex_t shz_ccothf(shz_complex_t c) SHZ_NOEXCEPT;
214
215//! Calculates and returns the complex hyperbolic arcsine of \p c.
216SHZ_INLINE shz_complex_t shz_casinhf(shz_complex_t c) SHZ_NOEXCEPT;
217
218//! Calculates and returns the complex hyperbolic arccosine of \p c.
219SHZ_INLINE shz_complex_t shz_cacoshf(shz_complex_t c) SHZ_NOEXCEPT;
220
221//! Calculates and returns the complex hyperbolic arctangent of \p c.
222SHZ_INLINE shz_complex_t shz_catanhf(shz_complex_t c) SHZ_NOEXCEPT;
223
224//! Calculates and returns the complex hyperbolic arccosecant of \p c.
225SHZ_INLINE shz_complex_t shz_cacschf(shz_complex_t c) SHZ_NOEXCEPT;
226
227//! Calculates and returns the complex hyperbolic arcsecant of \p c.
228SHZ_INLINE shz_complex_t shz_casechf(shz_complex_t c) SHZ_NOEXCEPT;
229
230//! Calculates and returns the complex hyperbolic arccotangent of \p c.
231SHZ_INLINE shz_complex_t shz_cacothf(shz_complex_t c) SHZ_NOEXCEPT;
232
233//! @}
234
235/*! \name Signal Processing
236 \brief Functions for processing complex signals.
237 @{
238*/
239
240/*! Fast Fourier Transform
241
242 Applies a fast fourier transform to convert the complex array
243 of samples, \p s, of the given \p size, from the time to the
244 frequency domain. The conversion is done in-place.
245
246 \note
247 The underlying implementation uses the Radix2 variant of the
248 Cooley-Tukey FFT algorithm, using an XMTRX-accelerated 2 point
249 butterfly DIF.
250
251 \warning \p size must be a power-of-two!
252 \warning This routine clobbers `XMTRX`!
253*/
254void shz_fft(shz_complex_t* s, size_t size) SHZ_NOEXCEPT;
255
256//! @}
257
258#include "inline/shz_complex.inl.h"
259
260SHZ_DECLS_END
261
262#endif
float shz_inv_cabsf(shz_complex_t c) SHZ_NOEXCEPT
Returns the inverse of the absolute value (or inverse of the magnitude) of the complex number,...
shz_complex_t shz_cacoshf(shz_complex_t c) SHZ_NOEXCEPT
Calculates and returns the complex hyperbolic arccosine of c.
#define SHZ_CMPLXF(x, y)
Initializes a complex number.
Definition shz_complex.h:38
shz_complex_t shz_ccotf(shz_complex_t c) SHZ_NOEXCEPT
Calculates and returns the complex cotangent of c.
shz_complex_t shz_cmulf(shz_complex_t lhs, shz_complex_t rhs) SHZ_NOEXCEPT
Multiplies two complex numbers together, returning the complex result.
void shz_fft(shz_complex_t *s, size_t size) SHZ_NOEXCEPT
Fast Fourier Transform.
float shz_cimagf(shz_complex_t c) SHZ_NOEXCEPT
Returns the imaginary component of a complex number.
shz_complex_t shz_catanf(shz_complex_t c) SHZ_NOEXCEPT
Calculates and returns the complex arctangent of c.
shz_complex_t shz_casinf(shz_complex_t c) SHZ_NOEXCEPT
Calculates and returns the complex arcsine of c.
shz_complex_t shz_csinf(shz_complex_t c) SHZ_NOEXCEPT
Calculates and returns the complex sine of c.
shz_complex_t shz_csechf(shz_complex_t c) SHZ_NOEXCEPT
Calculates and returns the complex hyperbolic secant of c.
shz_complex_t shz_ccschf(shz_complex_t c) SHZ_NOEXCEPT
Calculates and returns the complex hyperbolic cosecant of c.
shz_complex_t shz_crecipf(shz_complex_t c) SHZ_NOEXCEPT
Returns the multiplicative inverse or reciprocal of the complex number, c.
float shz_cabsf(shz_complex_t c) SHZ_NOEXCEPT
Returns the absolute value (or magnitude) of the complex number, c.
shz_complex_t shz_cacothf(shz_complex_t c) SHZ_NOEXCEPT
Calculates and returns the complex hyperbolic arccotangent of c.
shz_complex_t shz_ctanf(shz_complex_t c) SHZ_NOEXCEPT
Calculates and returns the complex tangent of c.
#define SHZ_CMPLXF32(x, y)
Initializes a complex number.
Definition shz_complex.h:39
shz_complex_t shz_cscalef(shz_complex_t c, float v) SHZ_NOEXCEPT
Scales both componets of the complex number, c, by the value, v, returning the result.
shz_complex_t shz_cprojf(shz_complex_t c) SHZ_NOEXCEPT
Calculates the projection of the complex number, c, onto the Riemann sphere.
shz_complex_t shz_cdivf(shz_complex_t lhs, shz_complex_t rhs) SHZ_NOEXCEPT
Divides lhs by rhs, returning the complex result.
shz_complex_t shz_csubf(shz_complex_t lhs, shz_complex_t rhs) SHZ_NOEXCEPT
Subtracts rhs from lhs and returns the complex result.
shz_complex_t shz_casechf(shz_complex_t c) SHZ_NOEXCEPT
Calculates and returns the complex hyperbolic arcsecant of c.
shz_complex_t shz_cacotf(shz_complex_t c) SHZ_NOEXCEPT
Calculates and returns the complex arccotangent of c.
shz_complex_t shz_csqrtf(shz_complex_t c) SHZ_NOEXCEPT
Computes and returns the complex square root of c.
shz_complex_t shz_casinhf(shz_complex_t c) SHZ_NOEXCEPT
Calculates and returns the complex hyperbolic arcsine of c.
bool shz_cequalf(shz_complex_t lhs, shz_complex_t rhs) SHZ_NOEXCEPT
Checks for equality between two complex numbers with either an absolute or relative tolerance.
float shz_cnormf(shz_complex_t c) SHZ_NOEXCEPT
Returns the squared magnitude of the complex number, c.
shz_complex_t shz_caddf(shz_complex_t lhs, shz_complex_t rhs) SHZ_NOEXCEPT
Adds two complex numbers together and returns the result.
shz_complex_t shz_casecf(shz_complex_t c) SHZ_NOEXCEPT
Calculates and returns the complex arcsecant of c.
shz_complex_t shz_clogf(shz_complex_t c) SHZ_NOEXCEPT
Computes and returns the complex natural logarithm of c.
float shz_crealf(shz_complex_t c) SHZ_NOEXCEPT
Returns the real component of a complex number.
shz_complex_t shz_csecf(shz_complex_t c) SHZ_NOEXCEPT
Calculates and returns the complex secant of c.
shz_complex_t shz_cpolarf(float r, float theta) SHZ_NOEXCEPT
Returns a complex number with a magnitude of r, and a phase angle of theta (in radians).
shz_complex_t shz_ccoshf(shz_complex_t c) SHZ_NOEXCEPT
Calculates and returns the complex hyperbolic cosine of c.
shz_complex_t shz_complex
shz_complex_t alias for those who don't like POSIX-style.
Definition shz_complex.h:53
shz_complex_t shz_cexpf(shz_complex_t c) SHZ_NOEXCEPT
Returns the complex base e exponential of c.
shz_complex_t shz_cinitf(float real, float imag) SHZ_NOEXCEPT
Returns a complex number which has been initialized to contain the given component values.
shz_complex_t shz_conjf(shz_complex_t c) SHZ_NOEXCEPT
Returns the complex conjugate of c.
shz_complex_t shz_catanhf(shz_complex_t c) SHZ_NOEXCEPT
Calculates and returns the complex hyperbolic arctangent of c.
shz_complex_t shz_cpowf(shz_complex_t base, shz_complex_t exp) SHZ_NOEXCEPT
Raises the complex base to the complex exp power, returning a complex result.
shz_complex_t shz_cacschf(shz_complex_t c) SHZ_NOEXCEPT
Calculates and returns the complex hyperbolic arccosecant of c.
float shz_cargf(shz_complex_t c) SHZ_NOEXCEPT
Returns the phase angle of the complex number, c.
shz_complex_t shz_ccosf(shz_complex_t c) SHZ_NOEXCEPT
Calculates and returns the complex cosine of c.
shz_complex_t shz_ctanhf(shz_complex_t c) SHZ_NOEXCEPT
Calculates and returns the complex hyperbolic tangent of c.
shz_complex_t shz_cacosf(shz_complex_t c) SHZ_NOEXCEPT
Calculates and returns the complex arccosine of c.
shz_complex_t shz_csinhf(shz_complex_t c) SHZ_NOEXCEPT
Calculates and returns the complex hyperbolic sine of c.
shz_complex_t shz_clog10f(shz_complex_t c) SHZ_NOEXCEPT
Computes and returns the complex base 10 logarithm of c.
shz_complex_t shz_ccothf(shz_complex_t c) SHZ_NOEXCEPT
Calculates and returns the complex hyperbolic cotangent of c.
shz_complex_t shz_cacscf(shz_complex_t c) SHZ_NOEXCEPT
Calculates and returns the complex arccosecant of c.
shz_complex_t shz_ccscf(shz_complex_t c) SHZ_NOEXCEPT
Calculates and returns the complex cosecant of c.
Represents a floating-point complex number real/imaginary pair.
Definition shz_complex.h:47
float real
The real portion of the complex number.
Definition shz_complex.h:48
float imag
The imaginary portion of the complex number.
Definition shz_complex.h:49