[ruamoko] Add sincos and sincosh functions

sincos is just a wrapper around the GNU libc sincos. sincosh is the
equivalent for sinh and cosh, but there doesn't seem to be any such
function, so it's just the two wrapped. They both return their results
in a vec2/vec2d as (sih[h], cos[h]).
This commit is contained in:
Bill Currie 2023-09-10 14:13:56 +09:00
parent b35f4c82a4
commit bce2b7d767
3 changed files with 59 additions and 0 deletions

View file

@ -31,6 +31,7 @@
# include "config.h" # include "config.h"
#endif #endif
#define _GNU_SOURCE
#include <stdlib.h> #include <stdlib.h>
#include <math.h> #include <math.h>
@ -47,6 +48,14 @@ bi_sinf (progs_t *pr, void *data)
R_FLOAT (pr) = sinf (P_FLOAT (pr, 0)); R_FLOAT (pr) = sinf (P_FLOAT (pr, 0));
} }
static void
bi_sincosf (progs_t *pr, void *data)
{
float *vec = &R_var (pr, float);
float x = P_FLOAT (pr, 0);
sincosf (x, &vec[0], &vec[1]);
}
static void static void
bi_cosf (progs_t *pr, void *data) bi_cosf (progs_t *pr, void *data)
{ {
@ -141,6 +150,15 @@ bi_sinhf (progs_t *pr, void *data)
R_FLOAT (pr) = sinhf (P_FLOAT (pr, 0)); R_FLOAT (pr) = sinhf (P_FLOAT (pr, 0));
} }
static void
bi_sincoshf (progs_t *pr, void *data)
{
float *vec = &R_var (pr, float);
float x = P_FLOAT (pr, 0);
vec[0] = sinhf (x);
vec[1] = coshf (x);
}
static void static void
bi_coshf (progs_t *pr, void *data) bi_coshf (progs_t *pr, void *data)
{ {
@ -198,6 +216,14 @@ bi_sin (progs_t *pr, void *data)
R_DOUBLE (pr) = sin (P_DOUBLE (pr, 0)); R_DOUBLE (pr) = sin (P_DOUBLE (pr, 0));
} }
static void
bi_sincos (progs_t *pr, void *data)
{
double *vec = &R_var (pr, double);
float x = P_FLOAT (pr, 0);
sincos (x, &vec[0], &vec[1]);
}
static void static void
bi_cos (progs_t *pr, void *data) bi_cos (progs_t *pr, void *data)
{ {
@ -288,6 +314,15 @@ bi_sinh (progs_t *pr, void *data)
R_DOUBLE (pr) = sinh (P_DOUBLE (pr, 0)); R_DOUBLE (pr) = sinh (P_DOUBLE (pr, 0));
} }
static void
bi_sincosh (progs_t *pr, void *data)
{
float *vec = &R_var (pr, float);
float x = P_FLOAT (pr, 0);
vec[0] = sinh (x);
vec[1] = cosh (x);
}
static void static void
bi_cosh (progs_t *pr, void *data) bi_cosh (progs_t *pr, void *data)
{ {
@ -325,6 +360,7 @@ bi_atanh (progs_t *pr, void *data)
#define p(type) PR_PARAM(type) #define p(type) PR_PARAM(type)
static builtin_t builtins[] = { static builtin_t builtins[] = {
{"sin|f", bi_sinf, -1, 1, {p(float)}}, {"sin|f", bi_sinf, -1, 1, {p(float)}},
{"sincos|f",bi_sincosf, -1, 1, {p(float)}},
{"cos|f", bi_cosf, -1, 1, {p(float)}}, {"cos|f", bi_cosf, -1, 1, {p(float)}},
{"tan|f", bi_tanf, -1, 1, {p(float)}}, {"tan|f", bi_tanf, -1, 1, {p(float)}},
{"asin|f", bi_asinf, -1, 1, {p(float)}}, {"asin|f", bi_asinf, -1, 1, {p(float)}},
@ -340,6 +376,7 @@ static builtin_t builtins[] = {
{"cbrt|f", bi_cbrtf, -1, 1, {p(float)}}, {"cbrt|f", bi_cbrtf, -1, 1, {p(float)}},
{"hypot|ff",bi_hypotf, -1, 2, {p(float), p(float)}}, {"hypot|ff",bi_hypotf, -1, 2, {p(float), p(float)}},
{"sinh|f", bi_sinhf, -1, 1, {p(float)}}, {"sinh|f", bi_sinhf, -1, 1, {p(float)}},
{"sincosh|f",bi_sincoshf,-1, 1, {p(float)}},
{"cosh|f", bi_coshf, -1, 1, {p(float)}}, {"cosh|f", bi_coshf, -1, 1, {p(float)}},
{"tanh|f", bi_tanhf, -1, 1, {p(float)}}, {"tanh|f", bi_tanhf, -1, 1, {p(float)}},
{"asinh|f", bi_asinhf, -1, 1, {p(float)}}, {"asinh|f", bi_asinhf, -1, 1, {p(float)}},
@ -349,6 +386,7 @@ static builtin_t builtins[] = {
{"ceil|d", bi_ceil, -1, 1, {p(double)}}, // float version in pr_cmds {"ceil|d", bi_ceil, -1, 1, {p(double)}}, // float version in pr_cmds
{"fabs|d", bi_fabs, -1, 1, {p(double)}}, // float version in pr_cmds {"fabs|d", bi_fabs, -1, 1, {p(double)}}, // float version in pr_cmds
{"sin|d", bi_sin, -1, 1, {p(double)}}, {"sin|d", bi_sin, -1, 1, {p(double)}},
{"sincos|d",bi_sincos, -1, 1, {p(double)}},
{"cos|d", bi_cos, -1, 1, {p(double)}}, {"cos|d", bi_cos, -1, 1, {p(double)}},
{"tan|d", bi_tan, -1, 1, {p(double)}}, {"tan|d", bi_tan, -1, 1, {p(double)}},
{"asin|d", bi_asin, -1, 1, {p(double)}}, {"asin|d", bi_asin, -1, 1, {p(double)}},
@ -364,6 +402,7 @@ static builtin_t builtins[] = {
{"cbrt|d", bi_cbrt, -1, 1, {p(double)}}, {"cbrt|d", bi_cbrt, -1, 1, {p(double)}},
{"hypot|dd",bi_hypot, -1, 2, {p(double), p(double)}}, {"hypot|dd",bi_hypot, -1, 2, {p(double), p(double)}},
{"sinh|d", bi_sinh, -1, 1, {p(double)}}, {"sinh|d", bi_sinh, -1, 1, {p(double)}},
{"sincosh|d",bi_sincosh,-1, 1, {p(double)}},
{"cosh|d", bi_cosh, -1, 1, {p(double)}}, {"cosh|d", bi_cosh, -1, 1, {p(double)}},
{"tanh|d", bi_tanh, -1, 1, {p(double)}}, {"tanh|d", bi_tanh, -1, 1, {p(double)}},
{"asinh|d", bi_asinh, -1, 1, {p(double)}}, {"asinh|d", bi_asinh, -1, 1, {p(double)}},

View file

@ -130,6 +130,14 @@
@extern @overload float sin (float x); @extern @overload float sin (float x);
@extern @overload double sin (double x); @extern @overload double sin (double x);
/**
Returns the sine and cosine of \a x.
The return value is [sin, cos].
*/
@extern @overload vec2 sincos (float x);
@extern @overload dvec2 sincos (double x);
/** /**
Returns the cosine of \a x. Returns the cosine of \a x.
*/ */
@ -183,6 +191,14 @@
@extern @overload float sinh (float x); @extern @overload float sinh (float x);
@extern @overload double sinh (double x); @extern @overload double sinh (double x);
/**
Returns the hyperbolic sine and cosine of \a x.
The return value is [sinh, cosh].
*/
@extern @overload vec2 sincosh (float x);
@extern @overload dvec2 sincosh (double x);
/** /**
Returns the hyperbolic cosine of \a x Returns the hyperbolic cosine of \a x
*/ */

View file

@ -1,6 +1,7 @@
#include <math.h> #include <math.h>
float (float x) sin = #0; float (float x) sin = #0;
vec2 sincos (float x) = #0;
float (float x) cos = #0; float (float x) cos = #0;
float (float x) tan = #0; float (float x) tan = #0;
float (float x) asin = #0; float (float x) asin = #0;
@ -13,6 +14,7 @@ float (float x) log2 = #0;
float (float x) log10 = #0; float (float x) log10 = #0;
float (float x, float y) pow = #0; float (float x, float y) pow = #0;
float (float x) sinh = #0; float (float x) sinh = #0;
vec2 sincosh (float x) = #0;
float (float x) cosh = #0; float (float x) cosh = #0;
float (float x) tanh = #0; float (float x) tanh = #0;
float (float x) asinh = #0; float (float x) asinh = #0;
@ -26,6 +28,7 @@ double (double v) floor = #0;
double (double v) ceil = #0; double (double v) ceil = #0;
double (double f) fabs = #0; double (double f) fabs = #0;
double (double x) sin = #0; double (double x) sin = #0;
dvec2 sincos (double x) = #0;
double (double x) cos = #0; double (double x) cos = #0;
double (double x) tan = #0; double (double x) tan = #0;
double (double x) asin = #0; double (double x) asin = #0;
@ -38,6 +41,7 @@ double (double x) log2 = #0;
double (double x) log10 = #0; double (double x) log10 = #0;
double (double x, double y) pow = #0; double (double x, double y) pow = #0;
double (double x) sinh = #0; double (double x) sinh = #0;
dvec2 sincosh (double x) = #0;
double (double x) cosh = #0; double (double x) cosh = #0;
double (double x) tanh = #0; double (double x) tanh = #0;
double (double x) asinh = #0; double (double x) asinh = #0;