Skip to content

Commit

Permalink
types.h and math.h rewrite
Browse files Browse the repository at this point in the history
  • Loading branch information
intns committed Jan 2, 2024
1 parent 2513d73 commit 2b4b2b1
Show file tree
Hide file tree
Showing 3 changed files with 107 additions and 13 deletions.
17 changes: 16 additions & 1 deletion include/math.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,19 @@

#include "types.h"

#define LONG_TAU 6.2831854820251465
#define TAU 6.2831855f
#define PI 3.1415927f
#define HALF_PI 1.5707964f
#define THIRD_PI 1.0471976f
#define QUARTER_PI 0.7853982f

#define SIN_2_5 0.43633234f
#define M_SQRT3 1.73205f

#define DEG2RAD (1.0f / 180.0f)
#define TORADIANS(val) (PI * (DEG2RAD * val))

f64 __fabs(f64);
f32 __fabsf(f32);
f64 __fnabs(f64);
Expand Down Expand Up @@ -31,12 +44,14 @@ void __mtfsb0(int);
void __mtfsb1(int);
f64 __setflm(f64);

inline f32 sqrtf(f32 x)
static inline f32 sqrtf(f32 x)
{
static const f64 _half = .5;
static const f64 _three = 3.0;

vf32 y;
if (x > 0.0f) {

f64 guess = __frsqrte((f64)x); // returns an approximation to
guess = _half * guess * (_three - guess * guess * x); // now have 12 sig bits
guess = _half * guess * (_three - guess * guess * x); // now have 24 sig bits
Expand Down
64 changes: 55 additions & 9 deletions include/types.h
Original file line number Diff line number Diff line change
@@ -1,6 +1,12 @@
#ifndef _TYPES_H
#define _TYPES_H

// r2 is 803F0200
// r13 is 803E4D20

typedef int BOOL;
typedef unsigned int uint;

typedef signed char s8;
typedef signed short s16;
typedef signed long s32;
Expand All @@ -26,12 +32,12 @@ typedef volatile f32 vf32;
typedef volatile f64 vf64;
typedef volatile f128 vf128;

typedef int BOOL;
typedef u32 size_t;
typedef u32 unknown;

/* int<x>_t */
typedef signed char int8_t;
typedef short int16_t;
typedef int int32_t;
#ifndef __cplusplus
typedef u16 wchar_t;
#endif

/* uint<x>_t */
typedef unsigned char uint8_t;
Expand All @@ -49,13 +55,53 @@ typedef unsigned long size_t;
#define NULL ((void*)0)
#define nullptr 0

#define BUMP_REGISTER(reg) \
{ \
asm { mr reg, reg } \
}
// Sets specific flag to 1
#define SET_FLAG(x, val) (x |= (val))

// Resets specific flag from (val) back to 0
#define RESET_FLAG(x, val) (x &= ~(val))

// Return 1 if flag is set, 0 if flag is not set
#define IS_FLAG(x, val) (x & val)

// Array size define
#define ARRAY_SIZE(o) (sizeof((o)) / sizeof(*(o)))

// Align X to the previous N bytes (N must be power of two)
#define ALIGN_PREV(X, N) ((X) & ~((N)-1))

// Align X to the next N bytes (N must be power of two)
#define ALIGN_NEXT(X, N) ALIGN_PREV(((X) + (N)-1), N)

// True if X is aligned to N bytes, else false
#define IS_ALIGNED(X, N) ((X & ((N)-1)) == 0)

// True if X is not aligned to N bytes, else false
#define IS_NOT_ALIGNED(X, N) (((X) & ((N)-1)) != 0)

// Align object to num bytes (num should be power of two)
#define ATTRIBUTE_ALIGN(num) __attribute__((aligned(num)))

// Checks if a flag is set in a bitfield
#define IS_FLAG_SET(flags, bitsFromLSB) (((flags) >> (bitsFromLSB) & 1))

#define ASSERT_HANG(cond) \
if (!(cond)) { \
while (true) { } \
}

// Get the maximum of two values
#define MAX(a, b) (((a) > (b)) ? (a) : (b))

// Get the minimum of two values
#define MIN(a, b) (((a) < (b)) ? (a) : (b))

// Rounds a float to a u8
#define ROUND_F32_TO_U8(a) a >= 0.0f ? a + 0.5f : a - 0.5f

// Number of bytes in a kilobyte
#define KILOBYTE_BYTECOUNT 1024

#ifdef __MWERKS__
#define WEAKFUNC __declspec(weak)
#define DECL_SECT(name) __declspec(section name)
Expand Down
39 changes: 36 additions & 3 deletions src/sysCommon/sysMath.cpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#include "types.h"
#include "math.h"

/*
* --INFO--
Expand Down Expand Up @@ -1398,8 +1399,17 @@ void Quat::fromEuler(Vector3f&)
* Address: 80038588
* Size: 00002C
*/
void roundAng(f32)
float roundAng(float x)
{
if (x < 0.0f) {
x += TAU;
}

if (x >= TAU) {
x -= TAU;
}

return x;
/*
.loc_0x0:
lfs f0, -0x7C60(r2)
Expand Down Expand Up @@ -1472,8 +1482,17 @@ void angDist(f32, f32)
* Address: 80038628
* Size: 000050
*/
void qdist2(f32, f32, f32, f32)
void qdist2(float, float, float, float)
{
float differenceX = point2X - point1X;
float absoluteDifferenceX = (differenceX < 0.0f) ? -differenceX : differenceX;

float differenceY = point2Y - point1Y;
float absoluteDifferenceY = (differenceY < 0.0f) ? -differenceY : differenceY;

float minimumDifference = (absoluteDifferenceX <= absoluteDifferenceY) ? absoluteDifferenceX : absoluteDifferenceY;

return absoluteDifferenceX + absoluteDifferenceY - minimumDifference * 0.5f;
/*
.loc_0x0:
fsubs f3, f3, f1
Expand Down Expand Up @@ -1522,8 +1541,22 @@ void qdist3(f32, f32, f32, f32, f32, f32)
* Address: 80038678
* Size: 0001BC
*/
void CollTriInfo::init(RoomInfo*, Vector3f*)
void CollTriInfo::init(RoomInfo* info, Vector3f* pos)
{
for (int i = 0; i < 3; ++i) {
Vector3f* pos1;
Vector3f* pos2;
pos2 = &pos[this->_04[(i + 1) % 3]];
pos1 = &pos[this->_04[i % 3]];

Vector3f tempVector;
tempVector.sub2(pos2, pos1);
tempVector.normalise();
tempVector.CP(&this->field_18);
this->field_28[i] = tempVector;
double dpResult = tempVector.DP(pos2);
this->field_34[i] = dpResult;
}
/*
.loc_0x0:
mflr r0
Expand Down

0 comments on commit 2b4b2b1

Please sign in to comment.