vn/code/core/core_math.h

379 lines
10 KiB
C
Raw Permalink Normal View History

2023-06-21 16:59:36 +00:00
/* date = June 19th 2023 10:28 pm */
#ifndef CORE_MATH_H
#define CORE_MATH_H
#include <math.h>
2024-01-20 11:18:57 +00:00
#include <float.h>
2023-06-21 16:59:36 +00:00
//- sixten: Constants
read_only global r32 InfinityR32 = INFINITY;
read_only global r32 PiR32 = 3.1415926535897f;
read_only global r32 EulersNumberR32 = 2.7182818284590452353602874713527f;
//- sixten: Vector types
union v2_r32
{
struct
{
r32 x, y;
};
r32 E[2];
};
union v2_s32
{
struct
{
s32 x, y;
};
s32 E[2];
};
union v2_s64
{
struct
{
s64 x, y;
};
s64 E[2];
};
union v3_r32
{
struct
{
r32 x, y, z;
};
struct
{
r32 r, g, b;
};
r32 E[3];
};
union v3_s32
{
struct
{
s32 x, y, z;
};
struct
{
s32 r, g, b;
};
s32 E[3];
};
union v3_s64
{
struct
{
s64 x, y, z;
};
struct
{
s64 r, g, b;
};
s64 E[3];
};
union v4_r32
{
struct
{
r32 x, y, z, w;
};
struct
{
r32 r, g, b, a;
};
r32 E[4];
};
union v4_s32
{
struct
{
s32 x, y, z, w;
};
struct
{
s32 r, g, b, a;
};
s32 E[4];
};
union v4_s64
{
struct
{
s64 x, y, z, w;
};
struct
{
s64 r, g, b, a;
};
s64 E[4];
};
#define ConvertV2ToR32(V) V2R32((r32)(V).x, (r32)(V).y)
#define ConvertV2ToS32(V) V2S32((s32)(V).x, (s32)(V).y)
#define ConvertV2ToS64(V) V2S64((s64)(V).x, (s64)(V).y)
#define ConvertV3ToR32(V) V3R32((r32)(V).x, (r32)(V).y, (r32)(V).z)
#define ConvertV3ToS32(V) V3S32((s32)(V).x, (s32)(V).y, (s32)(V).z)
#define ConvertV3ToS64(V) V3S64((s64)(V).x, (s64)(V).y, (s64)(V).z)
#define ConvertV4ToR32(V) V4R32((r32)(V).x, (r32)(V).y, (r32)(V).z, (r32)(V).w)
#define ConvertV4ToS32(V) V4S32((s32)(V).x, (s32)(V).y, (s32)(V).z, (s32)(V).w)
#define ConvertV4ToS64(V) V4S64((s64)(V).x, (s64)(V).y, (s64)(V).z, (s64)(V).w)
//- sixten: Matrix types
// sixten(NOTE): Matrices are treated as column-major.
struct m3x3
{
r32 E[3][3];
};
struct m4x4
{
r32 E[4][4];
};
//- sixten: Range types
struct range1_r32
{
r32 Min, Max;
};
struct range1_s32
{
s32 Min, Max;
};
struct range1_s64
{
s64 Min, Max;
};
struct range2_r32
{
v2_r32 Min, Max;
};
struct range2_s32
{
v2_s32 Min, Max;
};
struct range2_s64
{
v2_s64 Min, Max;
};
//- sixten: Scalar functions
inline r32 Floor(r32 x) { r32 Result = floorf(x); return(Result); }
inline r32 Ceil(r32 x) { r32 Result = ceilf(x); return(Result); }
inline r32 Round(r32 x) { r32 Result = roundf(x); return(Result); }
inline r32 Mod(r32 x, r32 y) { r32 Result = fmodf(x, y); return(Result); }
inline r32 SquareRoot(r32 x) { r32 Result = sqrtf(x); return(Result); }
inline r32 AbsoluteValue(r32 x) { r32 Result = fabsf(x); return(Result); }
inline r32 Sin(r32 x) { r32 Result = sinf(x); return(Result); }
inline r32 Cos(r32 x) { r32 Result = cosf(x); return(Result); }
inline r32 Tan(r32 x) { r32 Result = tanf(x); return(Result); }
inline r32 Pow(r32 Base, r32 Exponent) { r32 Result = powf(Base, Exponent); return(Result); }
inline r32 Log10(r32 x) { r32 Result = log10f(x); return(Result); }
inline r32 Log(r32 x) { r32 Result = logf(x); return(Result); }
inline r32 LinearBlend(r32 a, r32 b, r32 x) { r32 Result = a + (b-a)*x; return(Result); }
inline r32 Min(r32 A, r32 B) { r32 Result = Minimum(A, B); return(Result); }
inline r32 Max(r32 A, r32 B) { r32 Result = Maximum(A, B); return(Result); }
2023-06-27 14:14:28 +00:00
inline s32 Min(s32 A, s32 B) { s32 Result = Minimum(A, B); return(Result); }
inline s32 Max(s32 A, s32 B) { s32 Result = Maximum(A, B); return(Result); }
inline s64 Min(s64 A, s64 B) { s64 Result = Minimum(A, B); return(Result); }
inline s64 Max(s64 A, s64 B) { s64 Result = Maximum(A, B); return(Result); }
2023-06-21 16:59:36 +00:00
//- sixten: Vector functions
inline v2_r32 V2R32(r32 x, r32 y);
inline v2_r32 operator+(v2_r32 A, v2_r32 B);
inline v2_r32 operator-(v2_r32 A, v2_r32 B);
inline v2_r32 operator*(v2_r32 A, v2_r32 B);
inline v2_r32 operator/(v2_r32 A, v2_r32 B);
inline v2_r32 operator*(v2_r32 A, r32 B);
inline v2_r32 operator+=(v2_r32 &A, v2_r32 B);
inline v2_r32 operator-=(v2_r32 &A, v2_r32 B);
inline v2_r32 operator*=(v2_r32 &A, v2_r32 B);
inline v2_r32 operator/=(v2_r32 &A, v2_r32 B);
inline v2_r32 operator*=(v2_r32 &A, r32 B);
inline b32 operator==(v2_r32 A, v2_r32 B);
inline b32 operator!=(v2_r32 A, v2_r32 B);
2023-06-21 16:59:36 +00:00
inline r32 Inner(v2_r32 A, v2_r32 B);
inline r32 LengthSquared(v2_r32 V);
inline r32 Length(v2_r32 V);
inline v2_r32 NOZ(v2_r32 V);
inline v2_r32 Hadamard(v2_r32 A, v2_r32 B);
2023-06-21 16:59:36 +00:00
inline v2_r32 LinearBlend(v2_r32 A, v2_r32 B, r32 x);
inline v2_r32 Min(v2_r32 A, v2_r32 B);
inline v2_r32 Max(v2_r32 A, v2_r32 B);
inline v2_s32 V2S32(s32 x, s32 y);
inline v2_s32 operator+(v2_s32 A, v2_s32 B);
inline v2_s32 operator-(v2_s32 A, v2_s32 B);
inline v2_s32 operator+=(v2_s32 &A, v2_s32 B);
inline v2_s32 operator-=(v2_s32 &A, v2_s32 B);
inline v2_s32 Min(v2_s32 A, v2_s32 B);
inline v2_s32 Max(v2_s32 A, v2_s32 B);
inline v2_s64 V2S64(s64 x, s64 y);
inline v2_s64 operator+(v2_s64 A, v2_s64 B);
inline v2_s64 operator-(v2_s64 A, v2_s64 B);
inline v2_s64 operator+=(v2_s64 &A, v2_s64 B);
inline v2_s64 operator-=(v2_s64 &A, v2_s64 B);
inline v2_s64 Min(v2_s64 A, v2_s64 B);
inline v2_s64 Max(v2_s64 A, v2_s64 B);
inline v3_r32 V3R32(r32 x, r32 y, r32 z);
inline v3_r32 operator+(v3_r32 A, v3_r32 B);
inline v3_r32 operator-(v3_r32 A, v3_r32 B);
inline v3_r32 operator*(v3_r32 A, v3_r32 B);
inline v3_r32 operator/(v3_r32 A, v3_r32 B);
inline v3_r32 operator*(v3_r32 A, r32 B);
inline v3_r32 operator+=(v3_r32 &A, v3_r32 B);
inline v3_r32 operator-=(v3_r32 &A, v3_r32 B);
inline v3_r32 operator*=(v3_r32 &A, v3_r32 B);
inline v3_r32 operator/=(v3_r32 &A, v3_r32 B);
inline v3_r32 operator*=(v3_r32 &A, r32 B);
inline r32 Inner(v3_r32 A, v3_r32 B);
inline r32 LengthSquared(v3_r32 V);
inline r32 Length(v3_r32 V);
inline v3_r32 NOZ(v3_r32 V);
inline v3_r32 LinearBlend(v3_r32 A, v3_r32 B, r32 x);
inline v3_r32 Cross(v3_r32 A, v3_r32 B);
inline v3_r32 Min(v3_r32 A, v3_r32 B);
inline v3_r32 Max(v3_r32 A, v3_r32 B);
inline v3_s32 V3S32(s32 x, s32 y, s32 z);
inline v3_s32 operator+(v3_s32 A, v3_s32 B);
inline v3_s32 operator-(v3_s32 A, v3_s32 B);
inline v3_s32 operator+=(v3_s32 &A, v3_s32 B);
inline v3_s32 operator-=(v3_s32 &A, v3_s32 B);
inline v3_s32 Min(v3_s32 A, v3_s32 B);
inline v3_s32 Max(v3_s32 A, v3_s32 B);
inline v3_s64 V3S64(s64 x, s64 y, s64 z);
inline v3_s64 operator+(v3_s64 A, v3_s64 B);
inline v3_s64 operator-(v3_s64 A, v3_s64 B);
inline v3_s64 operator+=(v3_s64 &A, v3_s64 B);
inline v3_s64 operator-=(v3_s64 &A, v3_s64 B);
inline v3_s64 Min(v3_s64 A, v3_s64 B);
inline v3_s64 Max(v3_s64 A, v3_s64 B);
inline v4_r32 V4R32(r32 x, r32 y, r32 z, r32 w);
inline v4_r32 operator+(v4_r32 A, v4_r32 B);
inline v4_r32 operator-(v4_r32 A, v4_r32 B);
inline v4_r32 operator*(v4_r32 A, v4_r32 B);
inline v4_r32 operator/(v4_r32 A, v4_r32 B);
inline v4_r32 operator*(v4_r32 A, r32 B);
inline v4_r32 operator+=(v4_r32 &A, v4_r32 B);
inline v4_r32 operator-=(v4_r32 &A, v4_r32 B);
inline v4_r32 operator*=(v4_r32 &A, v4_r32 B);
inline v4_r32 operator/=(v4_r32 &A, v4_r32 B);
inline v4_r32 operator*=(v4_r32 &A, r32 B);
inline r32 Inner(v4_r32 A, v4_r32 B);
inline r32 LengthSquared(v4_r32 V);
inline r32 Length(v4_r32 V);
inline v4_r32 NOZ(v4_r32 V);
inline v4_r32 LinearBlend(v4_r32 A, v4_r32 B, r32 x);
inline v4_r32 Min(v4_r32 A, v4_r32 B);
inline v4_r32 Max(v4_r32 A, v4_r32 B);
inline v4_s32 V4S32(s32 x, s32 y, s32 z, s32 w);
inline v4_s32 operator+(v4_s32 A, v4_s32 B);
inline v4_s32 operator-(v4_s32 A, v4_s32 B);
inline v4_s32 operator+=(v4_s32 &A, v4_s32 B);
inline v4_s32 operator-=(v4_s32 &A, v4_s32 B);
inline v4_s32 Min(v4_s32 A, v4_s32 B);
inline v4_s32 Max(v4_s32 A, v4_s32 B);
inline v4_s64 V4S64(s64 x, s64 y, s64 z, s64 w);
inline v4_s64 operator+(v4_s64 A, v4_s64 B);
inline v4_s64 operator-(v4_s64 A, v4_s64 B);
inline v4_s64 operator+=(v4_s64 &A, v4_s64 B);
inline v4_s64 operator-=(v4_s64 &A, v4_s64 B);
inline v4_s64 Min(v4_s64 A, v4_s64 B);
inline v4_s64 Max(v4_s64 A, v4_s64 B);
//- sixten: Range functions
2023-07-19 15:09:41 +00:00
// sixten(NOTE): InRange means that the value is in [Min, Max) and
// Contains means that the value is in [Min, Max]
inline range1_r32 Range1R32(r32 A, r32 B);
2023-06-21 16:59:36 +00:00
inline b32 InRange(range1_r32 Range, r32 Value);
2023-07-19 15:09:41 +00:00
inline b32 Contains(range1_r32 Range, r32 Value);
2023-06-21 16:59:36 +00:00
inline r32 DimOfRange(range1_r32 Range);
2023-06-27 14:14:28 +00:00
inline range1_r32 Intersection(range1_r32 A, range1_r32 B);
inline range1_r32 Pad(range1_r32 Range, r32 Value);
2023-06-21 16:59:36 +00:00
2023-07-19 15:09:41 +00:00
inline range1_s32 Range1S32(s32 A, s32 B);
2023-06-21 16:59:36 +00:00
inline b32 InRange(range1_s32 Range, s32 Value);
2023-07-19 15:09:41 +00:00
inline b32 Contains(range1_s32 Range, s32 Value);
2023-06-21 16:59:36 +00:00
inline s32 DimOfRange(range1_s32 Range);
2023-06-27 14:14:28 +00:00
inline range1_s32 Intersection(range1_s32 A, range1_s32 B);
inline range1_s32 Pad(range1_s32 Range, s32 Value);
2023-06-21 16:59:36 +00:00
2023-07-19 15:09:41 +00:00
inline range1_s64 Range1S64(s64 A, s64 B);
2023-06-21 16:59:36 +00:00
inline b32 InRange(range1_s64 Range, s64 Value);
2023-07-19 15:09:41 +00:00
inline b32 Contains(range1_s64 Range, s64 Value);
2023-06-21 16:59:36 +00:00
inline s64 DimOfRange(range1_s64 Range);
2023-06-27 14:14:28 +00:00
inline range1_s64 Intersection(range1_s64 A, range1_s64 B);
inline range1_s64 Pad(range1_s64 Range, s64 Value);
2023-06-21 16:59:36 +00:00
2023-07-19 15:09:41 +00:00
inline range2_r32 Range2R32(v2_r32 A, v2_r32 B);
2023-06-21 16:59:36 +00:00
inline b32 InRange(range2_r32 Range, v2_r32 Value);
2023-07-19 15:09:41 +00:00
inline b32 Contains(range2_r32 Range, v2_r32 Value);
2023-06-21 16:59:36 +00:00
inline v2_r32 DimOfRange(range2_r32 Range);
2023-06-27 14:14:28 +00:00
inline range2_r32 Intersection(range2_r32 A, range2_r32 B);
2023-07-19 15:09:41 +00:00
inline v2_r32 CornerFromRange(range2_r32 Range, corner Corner);
inline range2_r32 Pad(range2_r32 Range, v2_r32 Value);
2023-06-21 16:59:36 +00:00
2023-07-19 15:09:41 +00:00
inline range2_s32 Range2S32(v2_s32 A, v2_s32 B);
2023-06-21 16:59:36 +00:00
inline b32 InRange(range2_s32 Range, v2_s32 Value);
2023-07-19 15:09:41 +00:00
inline b32 Contains(range2_s32 Range, v2_s32 Value);
2023-06-21 16:59:36 +00:00
inline v2_s32 DimOfRange(range2_s32 Range);
2023-06-27 14:14:28 +00:00
inline range2_s32 Intersection(range2_s32 A, range2_s32 B);
2023-07-19 15:09:41 +00:00
inline v2_s32 CornerFromRange(range2_s32 Range, corner Corner);
2023-06-21 16:59:36 +00:00
2023-07-19 15:09:41 +00:00
inline range2_s64 Range2S64(v2_s64 A, v2_s64 B);
2023-06-21 16:59:36 +00:00
inline b32 InRange(range2_s64 Range, v2_s64 Value);
2023-07-19 15:09:41 +00:00
inline b32 Contains(range2_s64 Range, v2_s64 Value);
2023-06-21 16:59:36 +00:00
inline v2_s64 DimOfRange(range2_s64 Range);
2023-06-27 14:14:28 +00:00
inline range2_s64 Intersection(range2_s64 A, range2_s64 B);
2023-07-19 15:09:41 +00:00
inline v2_s64 CornerFromRange(range2_s64 Range, corner Corner);
2023-06-21 16:59:36 +00:00
#endif //CORE_MATH_H