Steal some math funcs

This commit is contained in:
bumbread 2022-06-11 15:49:07 +11:00
parent 6f57efcaa9
commit 2320a22706
29 changed files with 1616 additions and 1508 deletions

View File

@ -51,4 +51,5 @@ del build\*.obj
:skip_crt_compilation :skip_crt_compilation
echo Compiling test.. echo Compiling test..
clang test\test6.c ciabatta.lib -std=c11 -lkernel32 -luser32 -lshell32 -nostdlib %CIABATTA_OPTIONS% clang -fno-builtin test\test_math.c ciabatta.lib -std=c11 -lkernel32 -luser32 -lshell32 -nostdlib %CIABATTA_OPTIONS%
::cl test\test_math.c /Iinc -D_CRT_SECURE_NO_WARNINGS /Z7 /link ciabatta.lib kernel32.lib user32.lib shell32.lib -nostdlib -nodefaultlibs

View File

View File

264
code/math/ieee754.c Normal file
View File

@ -0,0 +1,264 @@
#include <math.h>
#include <fenv.h>
#include <stdint.h>
#include <float.h>
#include <_compiler.h>
#if defined(_compiler_clang) || defined(_compiler_gnu)
#define just_do_it(t) __attribute__((unused)) volatile t
#endif
int _fpclassify(double x) {
union {double f; uint64_t i;} u = {x};
int e = u.i>>52 & 0x7ff;
if (!e) return u.i<<1 ? FP_SUBNORMAL : FP_ZERO;
if (e==0x7ff) return u.i<<12 ? FP_NAN : FP_INFINITE;
return FP_NORMAL;
}
int _fpclassifyf(float x) {
union {float f; uint32_t i;} u = {x};
int e = u.i>>23 & 0xff;
if (!e) return u.i<<1 ? FP_SUBNORMAL : FP_ZERO;
if (e==0xff) return u.i<<9 ? FP_NAN : FP_INFINITE;
return FP_NORMAL;
}
int _fpclassifyl(long double x) {
return _fpclassify(x);
}
int _signbit(double x) {
union {
double d;
uint64_t i;
} y = { x };
return y.i>>63;
}
int _signbitf(float x) {
union {
float f;
uint32_t i;
} y = { x };
return y.i>>31;
}
int _signbitl(long double x) {
return _signbit(x);
}
double copysign(double x, double y) {
union {double f; uint64_t i;} ux={x}, uy={y};
ux.i &= ~(1ULL<<63);
ux.i |= uy.i & (1ULL<<63);
return ux.f;
}
float copysignf(float x, float y) {
union {float f; uint32_t i;} ux={x}, uy={y};
ux.i &= 0x7fffffff;
ux.i |= uy.i & 0x80000000;
return ux.f;
}
long double copysignl(long double x, long double y) {
return copysign(x, y);
}
double nan(const char *s) {
return NAN;
}
float nanf(const char *s) {
return NAN;
}
long double nanl(const char *s) {
return NAN;
}
double rint(double x) {
static const double_t toint = 1/DBL_EPSILON;
union {double f; uint64_t i;} u = {x};
int e = u.i>>52 & 0x7ff;
int s = u.i>>63;
double y;
if (e >= 0x3ff+52) return x;
if (s) y = x - toint + toint;
else y = x + toint - toint;
if (y == 0) return s ? -0.0 : +0.0;
return y;
}
float rintf(float x) {
static const float toint = 1/FLT_EPSILON;
union {float f; uint32_t i;} u = {x};
int e = u.i>>23 & 0xff;
int s = u.i>>31;
float y;
if (e >= 0x7f+23) return x;
if (s) y = x - toint + toint;
else y = x + toint - toint;
if (y == 0) return s ? -0.0f : 0.0f;
return y;
}
long double rintl(long double x) {
return rint(x);
}
double nearbyint(double x) {
#pragma STDC FENV_ACCESS ON
int e = fetestexcept(FE_INEXACT);
x = rint(x);
if (!e) feclearexcept(FE_INEXACT);
return x;
}
float nearbyintf(float x) {
#pragma STDC FENV_ACCESS ON
int e = fetestexcept(FE_INEXACT);
x = rintf(x);
if (!e) feclearexcept(FE_INEXACT);
return x;
}
long double nearbyintl(long double x) {
return nearbyint(x);
}
double nextafter(double x, double y) {
union {double f; uint64_t i;} ux={x}, uy={y};
uint64_t ax, ay;
int e;
if (isnan(x) || isnan(y)) return x + y;
if (ux.i == uy.i) return y;
ax = ux.i & -1ULL/2;
ay = uy.i & -1ULL/2;
if (ax == 0) {
if (ay == 0) return y;
ux.i = (uy.i & 1ULL<<63) | 1;
} else if (ax > ay || ((ux.i ^ uy.i) & 1ULL<<63)) {
ux.i--;
}
else {
ux.i++;
}
e = ux.i >> 52 & 0x7ff;
/* raise overflow if ux.f is infinite and x is finite */
if (e == 0x7ff) just_do_it(float) _x = x+x;
/* raise underflow if ux.f is subnormal or zero */
if (e == 0) just_do_it(float) _x = x*x + ux.f*ux.f;
return ux.f;
}
float nextafterf(float x, float y) {
union {float f; uint32_t i;} ux={x}, uy={y};
uint32_t ax, ay, e;
if (isnan(x) || isnan(y)) return x + y;
if (ux.i == uy.i) return y;
ax = ux.i & 0x7fffffff;
ay = uy.i & 0x7fffffff;
if (ax == 0) {
if (ay == 0) return y;
ux.i = (uy.i & 0x80000000) | 1;
} else if (ax > ay || ((ux.i ^ uy.i) & 0x80000000)) {
ux.i--;
}
else {
ux.i++;
}
e = ux.i & 0x7f800000;
/* raise overflow if ux.f is infinite and x is finite */
if (e == 0x7f800000) just_do_it(float) _x = x+x;
/* raise underflow if ux.f is subnormal or zero */
if (e == 0) just_do_it(float) _x = x*x + ux.f*ux.f;
return ux.f;
}
long double nextafterl(long double x, long double y) {
return nextafter(x, y);
}
double nexttoward(double x, long double y) {
return nextafter(x, y);
}
float nexttowardf(float x, long double y) {
union {float f; uint32_t i;} ux = {x};
uint32_t e;
if (isnan(x) || isnan(y)) return x + y;
if (x == y) return y;
if (x == 0) {
ux.i = 1;
if (signbit(y)) ux.i |= 0x80000000;
} else if (x < y) {
if (signbit(x)) ux.i--;
else ux.i++;
} else {
if (signbit(x)) ux.i++;
else ux.i--;
}
e = ux.i & 0x7f800000;
/* raise overflow if ux.f is infinite and x is finite */
if (e == 0x7f800000) just_do_it(float) _x = x+x;
/* raise underflow if ux.f is subnormal or zero */
if (e == 0) just_do_it(float) _x = x*x + ux.f*ux.f;
return ux.f;
}
long double nexttowardl(long double x, long double y) {
return nextafterl(x, y);
}
double round(double x) {
static const double_t toint = 1/DBL_EPSILON;
union {double f; uint64_t i;} u = {x};
int e = u.i >> 52 & 0x7ff;
double_t y;
if (e >= 0x3ff+52) return x;
if (u.i >> 63) x = -x;
if (e < 0x3ff-1) {
/* raise inexact if x!=0 */
just_do_it(float) _x = x + toint;
return 0*u.f;
}
y = x + toint - toint - x;
if (y > 0.5) y = y + x - 1;
else if (y <= -0.5) y = y + x + 1;
else y = y + x;
if (u.i >> 63) y = -y;
return y;
}
float roundf(float x) {
static const double_t toint = 1/FLT_EPSILON;
union {float f; uint32_t i;} u = {x};
int e = u.i >> 23 & 0xff;
float_t y;
if (e >= 0x7f+23) return x;
if (u.i >> 31) x = -x;
if (e < 0x7f-1) {
just_do_it(float) _x = x + toint;
return 0*u.f;
}
y = x + toint - toint - x;
if (y > 0.5f) y = y + x - 1;
else if (y <= -0.5f) y = y + x + 1;
else y = y + x;
if (u.i >> 31) y = -y;
return y;
}
long double roundl(long double x) {
return round(x);
}

View File

@ -1,139 +0,0 @@
// Instantiates 'template' macros for floating-point values
// When including expecting the following parameters to be defined:
// ftype: Floating-point type to consider
// itype: Signed integer type corresponding to ftype's bitwidth
// f_ebits: Number of bits in the exponent
// f_mbits: Number of bits in the mantissa
// suffix(name): appends corresponding suffix to the given name,
// e.g. f for floats
#define f_nbits (1+f_ebits+f_mbits)
#define f_emax ((1ULL << (f_ebits-1)) - 1)
#define f_emin (1 - f_emax)
#define f_ebias f_emax
// Extracting fields from the float
#define f_eoffs (f_mbits)
#define f_soffs (f_mbits+f_ebits)
#define f_emask ((1ULL << f_ebits) - 1)
#define f_mmask ((1ULL << f_mbits) - 1)
#define f_smask 1ULL
#define f_eval(b) (((b) >> f_eoffs) & f_emask)
#define f_sval(b) (((b) >> f_soffs) & f_smask)
#define f_mval(b) (((b) >> 0) & f_mmask)
#define f_abs(b) ((b) & ~(f_smask << f_soffs))
#define f_exp(b) (f_eval(b) - f_ebias)
#define f_qexp(b) (f_eval(b) - f_ebias - f_mbits)
#define f_qman(b) (((b) & f_mmask) | (f_mmask+1))
#define b_cons(s,e,m) (((itype)s << f_soffs) | ((itype)e << f_eoffs) | (itype)(m))
// Converting float to integer bits
static inline itype suffix(f_bits)(ftype f) {
union _u {
ftype f;
itype b;
} u;
u.f = f;
return u.b;
}
static inline ftype suffix(f_frombits)(itype b) {
union _u {
ftype f;
itype b;
} u;
u.b = b;
return u.f;
}
// Floating-point classification
int suffix(_fpclassify)(ftype f) {
itype bits = suffix(f_bits)(f);
itype exp = f_eval(bits);
itype man = f_mval(bits);
if(exp == f_emask) {
if(man == 0) return FP_INFINITE;
else return FP_NAN;
}
else if(exp == 0) {
if(man == 0) return FP_ZERO;
else return FP_SUBNORMAL;
}
else return FP_NORMAL;
}
int suffix(_signbit)(ftype f) {
itype bits = suffix(f_bits)(f);
itype sign = f_sval(bits);
return sign;
}
ftype suffix(copysign)(ftype x, ftype y) {
itype xbits = suffix(f_bits)(x);
itype ybits = suffix(f_bits)(y);
itype exp = f_eval(xbits);
itype man = f_mval(xbits);
itype sgn = f_sval(ybits);
itype rbits = b_cons(sgn, exp, man);
return suffix(f_frombits)(rbits);
}
// Floating-point non-signaling comparison
static Ordering suffix(_ordering)(ftype x, ftype y) {
itype xclass = suffix(_fpclassify)(x);
itype yclass = suffix(_fpclassify)(y);
if(xclass == FP_NAN || yclass == FP_NAN) {
return UN;
}
itype xbits = suffix(f_bits)(x);
itype ybits = suffix(f_bits)(y);
itype xsgn = f_sval(xbits);
itype ysgn = f_sval(ybits);
itype xabs = f_abs(xbits);
itype yabs = f_abs(ybits);
if(xsgn == ysgn) {
if(xabs > yabs) return GR;
if(xabs < yabs) return LS;
return EQ;
}
else {
if(xabs == 0 && yabs == 0) return EQ;
if(xsgn) return LS;
if(ysgn) return GR;
}
return UN; // I may be stupid
}
int suffix(_isgrt)(ftype x, ftype y) {
int ord = suffix(_ordering)(x, y);
return ord == GR;
}
int suffix(_isgeq)(ftype x, ftype y) {
int ord = suffix(_ordering)(x, y);
return ord == GR || ord == EQ;
}
int suffix(_isles)(ftype x, ftype y) {
int ord = suffix(_ordering)(x, y);
return ord == LS;
}
int suffix(_isleq)(ftype x, ftype y) {
int ord = suffix(_ordering)(x, y);
return ord == LS || ord == EQ;
}
int suffix(_isleg)(ftype x, ftype y) {
int ord = suffix(_ordering)(x, y);
return ord == LS || ord == GR;
}
int suffix(_isuno)(ftype x, ftype y) {
int ord = suffix(_ordering)(x, y);
return ord == UN;
}

View File

@ -1,53 +0,0 @@
#include <math.h>
#include <stdint.h>
// Used for float comparisons in ieee754.h
enum Ordering {
LS,
EQ,
GR,
UN,
} typedef Ordering;
#define ftype float
#define itype int32_t
#define f_ebits 8
#define f_mbits 23
#define suffix(n) n ## f
#include "ieee754.h"
#include "pow.h"
#undef suffix
#undef f_mbits
#undef f_ebits
#undef itype
#undef ftype
#define ftype double
#define itype int64_t
#define f_ebits 11
#define f_mbits 52
#define suffix(n) n
#include "ieee754.h"
#include "pow.h"
#undef suffix
#undef f_mbits
#undef f_ebits
#undef itype
#undef ftype
_Static_assert(sizeof(long double) == sizeof(double),
"Are these 'long doubles' in the same room with us right now?");
#define ftype long double
#define itype int64_t
#define f_ebits 11
#define f_mbits 52
#define suffix(n) n ## l
#include "ieee754.h"
#include "pow.h"
#undef suffix
#undef f_mbits
#undef f_ebits
#undef itype
#undef ftype

View File

View File

@ -1,103 +0,0 @@
#include <errno.h>
#if !defined(_isqrt_defined)
#define _isqrt_defined
static uint64_t _isqrt(uint64_t num, uint64_t *remp) {
// To find a square root of a number
// We get rid of zero
if(num == 0) {
*remp = 0;
return 0;
}
// Then, starting from the bottom, split num into 2-digit pairs
// and find the top-most non-zero pair
uint64_t i = 0;
while(i != (sizeof(uint64_t)*8) && (num >> i) != 0) {
i += 2;
}
// Then we start taking guesses such that at each step
// sqrt^2 <= number made of consequent pairs of exausted integers
uint64_t sqrt = 0;
uint64_t rem = 0;
// Repeat until remainder is equal to zero:
do {
i -= 2;
// Bring the next two digits of the number to our remainder
rem = (rem << 2) | ((num >> i) & 0x3);
// Find d such that d(2sqrt+d) <= rem
// Since d could be either 0 or 1 we simply check 1, otherwise its 0
uint64_t d = 1;
uint64_t t = ((sqrt<<2)|1);
if(t <= rem) {
rem -= t;
}
else {
d = 0;
}
// Append the digit to sqrt from the right
sqrt = (sqrt<<1)|d;
} while(i != 0);
*remp = rem;
return sqrt;
}
#endif
// For all it's worth this shit is simply equivalent to
// _isqrt((uint64)x)
// I hate porgaming.
ftype suffix(sqrt)(ftype x) {
if(x < 0) {
#if math_errhandling & MATH_ERRNO
errno = EDOM;
#endif
return NAN;
}
if(x == 0 || isinf(x)) {
return x;
}
if(isnan(x)) {
return NAN;
}
itype bits = suffix(f_bits)(x);
itype exp = f_qexp(bits);
itype man = f_qman(bits);
// Get lots of precision by shifting man right by max bits
// and subtracting this from the exponent
itype bit = 0; // highest set-bit of man
while((man >> (bit+1)) != 0) ++bit;
itype prec_shift_n = f_nbits - bit - 3;
man <<= prec_shift_n;
exp -= prec_shift_n;
// Now do the sqrt of 2^exp * man
// If exp is odd then 2^{2k+1}*sqrt(man) = 2^{2k}*sqrt{2*man}
if((2 + (exp % 2)) % 2 != 0) {
man <<= 1;
}
// Take exp sqrt
exp >>= 1;
// Take sqrt of mantissa
uint64_t rem;
man = (itype)_isqrt(man, &rem);
// Now sqrt(x) = 2^exp * man
// we need to normalize this shit
bit = 0; // highest set-bit of man
while((man >> (bit+1)) != 0) ++bit;
exp += bit;
man <<= f_nbits-bit;
exp += f_ebias;
man >>= f_nbits-f_mbits;
man &= f_mmask;
// Cons it back
bits = b_cons(0, exp, man);
return suffix(f_frombits)(bits);
}
ftype suffix(hypot)(ftype x, ftype y)
{
if(isinf(x) || isinf(y)) {
return INFINITY;
}
return suffix(sqrt)(x*x + y*y);
}

View File

View File

View File

View File

@ -1,4 +1,6 @@
#include <math.h>
//TODO: verify printf("%d", 0). From the code it looked like it would print //TODO: verify printf("%d", 0). From the code it looked like it would print
// an empty string. // an empty string.
@ -101,6 +103,11 @@ inline static int FMT_FUNC_NAME (void *ctx, OutputFunc out, const FMT_CHAR_TYPE
case 'L': { case 'L': {
double d = va_arg(args, double); double d = va_arg(args, double);
if(signbit(d)) { // TODO: negative zero
out(ctx, 1, "-");
d = -d;
}
if(isinf(d)) { if(isinf(d)) {
out(ctx, sizeof"inf"-1, "inf"); out(ctx, sizeof"inf"-1, "inf");
break; break;
@ -110,11 +117,6 @@ inline static int FMT_FUNC_NAME (void *ctx, OutputFunc out, const FMT_CHAR_TYPE
break; break;
} }
if(d < 0) { // TODO: negative zero
out(ctx, 1, "-");
d = -d;
}
uint64_t w = (uint64_t)d; uint64_t w = (uint64_t)d;
d -= w; d -= w;
FMT_CHAR_TYPE buffer[20]; FMT_CHAR_TYPE buffer[20];

View File

@ -14,8 +14,8 @@ typedef double double_t;
#define NAN (-(float)(INFINITY * 0.0F)) #define NAN (-(float)(INFINITY * 0.0F))
// FP_ILOGB0 #define FP_ILOGBNAN (-1-0x7fffffff)
// FP_ILOGBNAN #define FP_ILOGB0 FP_ILOGBNAN
#define MATH_ERRNO 1 #define MATH_ERRNO 1
#define MATH_ERREXCEPT 2 #define MATH_ERREXCEPT 2
@ -28,193 +28,275 @@ typedef double double_t;
#define FP_NORMAL 2 #define FP_NORMAL 2
#define FP_SUBNORMAL 3 #define FP_SUBNORMAL 3
#define FP_ZERO 4 #define FP_ZERO 4
int _fpclassify(double f); int _fpclassify(double);
int _fpclassifyf(float f); int _fpclassifyf(float);
#define fpclassify(x) (sizeof(x)==4?_fpclassifyf(x):_fpclassify(x)) int _fpclassifyl(long double);
#define fpclassify(x) (sizeof(x)==4?_fpclassifyf(x)\
:sizeof(x)==8?_fpclassify(x) \
: _fpclassifyl(x))
#define isfinite(x) (fpclassify(x) != FP_INFINITE && fpclassify(x) != FP_NAN) #define isfinite(x) (fpclassify(x) != FP_INFINITE && fpclassify(x) != FP_NAN)
#define isinf(x) (fpclassify(x) == FP_INFINITE) #define isinf(x) (fpclassify(x) == FP_INFINITE)
#define isnan(x) (fpclassify(x) == FP_NAN) #define isnan(x) (fpclassify(x) == FP_NAN)
#define isnormal(x) (fpclassify(x) == FP_NORMAL) #define isnormal(x) (fpclassify(x) == FP_NORMAL)
// Sign bit shit // signbit shit
int _signbit(double f); int _signbit(double);
int _signbitf(float f); int _signbitf(float);
#define signbit(x) (sizeof(x)==4?_signbitf(x):signbit(x)) int _signbitl(long double);
float copysignf(float x, float y); #define signbit(x) (sizeof(x) == sizeof(float) ? _signbitf(x) \
:sizeof(x) == sizeof(double) ? _signbit(x) \
: _signbitl(x))
// Ordering // Ordering
#define isgreater(x) (sizeof(x)==4?_isgrtf(x):_isgrt(x)) #define isunordered(x,y) (isnan((x)) ? ((void)(y),1) : isnan((y)))
#define isgreaterequal(x) (sizeof(x)==4?_isgeqf(x):_isgeq(x)) #define isgreater(x,y) (!isunordered(x,y) && ((x) > (y)))
#define isless(x) (sizeof(x)==4?_islesf(x):_isles(x)) #define isgreaterequal(x,y) (!isunordered(x,y) && ((x) >= (y)))
#define islessequal(x) (sizeof(x)==4?_isleqf(x):_isleq(x)) #define isless(x,y) (!isunordered(x,y) && ((x) < (y)))
#define islessgreater(x) (sizeof(x)==4?_islegf(x):_isleg(x)) #define islessequal(x,y) (!isunordered(x,y) && ((x) <= (y)))
#define isunordered(x) (sizeof(x)==4?_isunof(x):_isuno(x)) #define islessgreater(x,y) (!isunordered(x,y) && ((x) != (y)))
double acos(double x); #if defined(_USE_MATH_DEFINES)
float acosf(float x); #define M_E 2.7182818284590452354
#define M_LOG2E 1.4426950408889634074
#define M_LOG10E 0.43429448190325182765
#define M_LN2 0.69314718055994530942
#define M_LN10 2.30258509299404568402
#define M_PI 3.14159265358979323846
#define M_PI_2 1.57079632679489661923
#define M_PI_4 0.78539816339744830962
#define M_1_PI 0.31830988618379067154
#define M_2_PI 0.63661977236758134308
#define M_2_SQRTPI 1.12837916709551257390
#define M_SQRT2 1.41421356237309504880
#define M_SQRT1_2 0.70710678118654752440
#endif
double asin(double x); // Floating-point function prototypes
float asinf(float x); double acos(double);
float acosf(float);
long double acosl(long double);
double atan(double x); double acosh(double);
float atanf(float x); float acoshf(float);
long double acoshl(long double);
double atan2(double y, double x); double asin(double);
float atan2f(float y, float x); float asinf(float);
long double asinl(long double);
double cos(double x); double asinh(double);
float cosf(float x); float asinhf(float);
long double asinhl(long double);
double sin(double x); double atan(double);
float sinf(float x); float atanf(float);
long double atanl(long double);
double tan(double x); double atan2(double, double);
float tanf(float x); float atan2f(float, float);
long double atan2l(long double, long double);
double acosh(double x); double atanh(double);
float acoshf(float x); float atanhf(float);
long double atanhl(long double);
double asinh(double x); double cbrt(double);
float asinhf(float x); float cbrtf(float);
long double cbrtl(long double);
double atanh(double x); double ceil(double);
float atanhf(float x); float ceilf(float);
long double ceill(long double);
double cosh(double x); double copysign(double, double);
float coshf(float x); float copysignf(float, float);
long double copysignl(long double, long double);
double sinh(double x); double cos(double);
float sinhf(float x); float cosf(float);
long double cosl(long double);
double tanh(double x); double cosh(double);
float tanhf(float x); float coshf(float);
long double coshl(long double);
double exp(double x); double erf(double);
float expf(float x); float erff(float);
long double erfl(long double);
double exp2(double x); double erfc(double);
float exp2f(float x); float erfcf(float);
long double erfcl(long double);
double expm1(double x); double exp(double);
float expm1f(float x); float expf(float);
long double expl(long double);
double frexp(double value, int *exp); double exp2(double);
float frexpf(float value, int *exp); float exp2f(float);
long double exp2l(long double);
int ilogb(double x); double expm1(double);
int ilogbf(float x); float expm1f(float);
long double expm1l(long double);
double ldexp(double x, int exp); double fabs(double);
float ldexpf(float x, int exp); float fabsf(float);
long double fabsl(long double);
double log(double x); double fdim(double, double);
float logf(float x); float fdimf(float, float);
long double fdiml(long double, long double);
double log10(double x); double floor(double);
float log10f(float x); float floorf(float);
long double floorl(long double);
double log1p(double x); double fma(double, double, double);
float log1pf(float x); float fmaf(float, float, float);
long double fmal(long double, long double, long double);
double log2(double x); double fmax(double, double);
float log2f(float x); float fmaxf(float, float);
long double fmaxl(long double, long double);
double logb(double x); double fmin(double, double);
float logbf(float x); float fminf(float, float);
long double fminl(long double, long double);
double modf(double value, double *iptr); double fmod(double, double);
float modff(float value, float *iptr); float fmodf(float, float);
long double fmodl(long double, long double);
double scalbn(double x, int n); double frexp(double, int *);
float scalbnf(float x, int n); float frexpf(float, int *);
long double frexpl(long double, int *);
double scalbln(double x, long int n); double hypot(double, double);
float scalblnf(float x, long int n); float hypotf(float, float);
long double hypotl(long double, long double);
double cbrt(double x); int ilogb(double);
float cbrtf(float x); int ilogbf(float);
int ilogbl(long double);
double fabs(double x); double ldexp(double, int);
float fabsf(float x); float ldexpf(float, int);
long double ldexpl(long double, int);
double hypot(double x, double y); double lgamma(double);
float hypotf(float x, float y); float lgammaf(float);
long double lgammal(long double);
double pow(double x, double y); long long llrint(double);
float powf(float x, float y); long long llrintf(float);
long long llrintl(long double);
double sqrt(double x); long long llround(double);
float sqrtf(float x); long long llroundf(float);
long long llroundl(long double);
double erf(double x); double log(double);
float erff(float x); float logf(float);
long double logl(long double);
double erfc(double x); double log10(double);
float erfcf(float x); float log10f(float);
long double log10l(long double);
double lgamma(double x); double log1p(double);
float lgammaf(float x); float log1pf(float);
long double log1pl(long double);
double tgamma(double x); double log2(double);
float tgammaf(float x); float log2f(float);
long double log2l(long double);
double ceil(double x); double logb(double);
float ceilf(float x); float logbf(float);
long double logbl(long double);
double floor(double x); long lrint(double);
float floorf(float x); long lrintf(float);
long lrintl(long double);
double nearbyint(double x); long lround(double);
float nearbyintf(float x); long lroundf(float);
long lroundl(long double);
double rint(double x); double modf(double, double *);
float rintf(float x); float modff(float, float *);
long double modfl(long double, long double *);
long int lrint(double x); double nan(const char *);
long int lrintf(float x); float nanf(const char *);
long double nanl(const char *);
long long int llrint(double x); double nearbyint(double);
long long int llrintf(float x); float nearbyintf(float);
long double nearbyintl(long double);
double round(double x); double nextafter(double, double);
float roundf(float x); float nextafterf(float, float);
long double nextafterl(long double, long double);
long int lround(double x); double nexttoward(double, long double);
long int lroundf(float x); float nexttowardf(float, long double);
long double nexttowardl(long double, long double);
long long int llround(double x); double pow(double, double);
long long int llroundf(float x); float powf(float, float);
long double powl(long double, long double);
double trunc(double x); double remainder(double, double);
float truncf(float x); float remainderf(float, float);
long double remainderl(long double, long double);
double fmod(double x, double y); double remquo(double, double, int *);
float fmodf(float x, float y); float remquof(float, float, int *);
long double remquol(long double, long double, int *);
double remainder(double x, double y); double rint(double);
float remainderf(float x, float y); float rintf(float);
long double rintl(long double);
double remquo(double x, double y, int *quo); double round(double);
float remquof(float x, float y, int *quo); float roundf(float);
long double roundl(long double);
double copysign(double x, double y); double scalbln(double, long);
float copysignf(float x, float y); float scalblnf(float, long);
long double scalblnl(long double, long);
double nan(const char *tagp); double scalbn(double, int);
float nanf(const char *tagp); float scalbnf(float, int);
long double scalbnl(long double, int);
double nextafter(double x, double y); double sin(double);
float nextafterf(float x, float y); float sinf(float);
long double sinl(long double);
double fdim(double x, double y); double sinh(double);
float fdimf(float x, float y); float sinhf(float);
long double sinhl(long double);
double fmax(double x, double y); double sqrt(double);
float fmaxf(float x, float y); float sqrtf(float);
long double sqrtl(long double);
double fmin(double x, double y); double tan(double);
float fminf(float x, float y); float tanf(float);
long double tanl(long double);
double fma(double x, double y, double z); double tanh(double);
float fmaf(float x, float y, float z); float tanhf(float);
long double tanhl(long double);
double tgamma(double);
float tgammaf(float);
long double tgammal(long double);
double trunc(double);
float truncf(float);
long double truncl(long double);

View File

@ -1,22 +0,0 @@
#include <stdio.h>
#include <math.h>
void test_sqrt(float f) {
float s = sqrtf(f);
printf("sqrt of %f is %f\n", f, s);
}
int main() {
test_sqrt(0.0f);
test_sqrt(1.0f);
test_sqrt(2.0f);
test_sqrt(3.0f);
test_sqrt(4.0f);
test_sqrt(7.0f);
test_sqrt(9.0f);
test_sqrt(16.0f);
test_sqrt(256.0f);
test_sqrt(257.0f);
return 0;
}

76
test/test_math.c Normal file
View File

@ -0,0 +1,76 @@
#include <stdio.h>
#include <math.h>
#include <float.h>
#include <fenv.h>
const char *show_classification(double x) {
switch(fpclassify(x)) {
case FP_INFINITE: return "Inf";
case FP_NAN: return "NaN";
case FP_NORMAL: return "normal";
case FP_SUBNORMAL: return "subnormal";
case FP_ZERO: return "zero";
default: return "unknown";
}
}
int main() {
printf("\n=== fpclassify === \n");
double zero = 0.0; // fucken msvc
printf("1.0/0.0 is %s\n", show_classification(1.0/zero));
printf("0.0/0.0 is %s\n", show_classification(0.0/zero));
printf("DBL_MIN/2 is %s\n", show_classification(DBL_MIN/2));
printf("-0.0 is %s\n", show_classification(-0.0));
printf("1.0 is %s\n", show_classification(1.0));
printf("\n\n=== signbit === \n");
printf("signbit(+0.0) = %d\n", signbit(+0.0));
printf("signbit(-0.0) = %d\n", signbit(-0.0));
printf("\n\n=== copysign === \n");
printf("copysign(1.0,+2.0) = %f\n", copysign(1.0,+2.0));
printf("copysign(1.0,-2.0) = %f\n", copysign(1.0,-2.0));
printf("copysign(INFINITY,-2.0) = %f\n", copysign(INFINITY,-2.0));
printf("copysign(NAN,-2.0) = %f\n", copysign(NAN,-2.0));
printf("\n\n=== rint === \n");
fesetround(FE_TONEAREST);
printf("rounding to nearest (halfway cases to even):\n"
"rint(+2.3) = %f ", rint(2.3));
printf("rint(+2.5) = %f ", rint(2.5));
printf("rint(+3.5) = %f\n", rint(3.5));
printf("rint(-2.3) = %f ", rint(-2.3));
printf("rint(-2.5) = %f ", rint(-2.5));
printf("rint(-3.5) = %f\n", rint(-3.5));
fesetround(FE_DOWNWARD);
printf("rounding down: \nrint(+2.3) = %f ", rint(2.3));
printf("rint(+2.5) = %f ", rint(2.5));
printf("rint(+3.5) = %f\n", rint(3.5));
printf("rint(-2.3) = %f ", rint(-2.3));
printf("rint(-2.5) = %f ", rint(-2.5));
printf("rint(-3.5) = %f\n", rint(-3.5));
feclearexcept(FE_ALL_EXCEPT);
printf("rint(1.1) = %f\n", rint(1.1));
if(fetestexcept(FE_INEXACT)) printf(" FE_INEXACT was raised\n");
float from1 = 0, to1 = nextafterf(from1, 1);
printf("The next representable float after %f is %f\n", from1, to1);
float from2 = 1, to2 = nextafterf(from2, 2);
printf("The next representable float after %f is %f\n", from2, to2);
{
#pragma STDC FENV_ACCESS ON
feclearexcept(FE_ALL_EXCEPT);
double from4 = DBL_MAX, to4 = nextafter(from4, INFINITY);
printf("The next representable double after %f is %f\n",
from4, to4);
if(fetestexcept(FE_OVERFLOW)) printf(" raised FE_OVERFLOW\n");
if(fetestexcept(FE_INEXACT)) printf(" raised FE_INEXACT\n");
}
float from5 = 0.0, to5 = nextafter(from5, -0.0);
printf("nextafter(+0.0, -0.0) gives %f (%f)\n", to5, to5);
return 0;
}