Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use Kaze's folded polynomials #64

Open
wants to merge 7 commits into
base: develop/1.0.0
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions include/config/config_game.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@
// Disables AA (Improves console performance but causes visible seams between unconnected geometry).
//#define DISABLE_AA

// Use a folded polynomial instead of a lookup table
#define DISABLE_LOOKUP_TABLE
Yanis002 marked this conversation as resolved.
Show resolved Hide resolved

// Fix annoying glitches (crashes and softlocks)
#define FIX_ANNOYING_GLITCH

Expand Down
3 changes: 3 additions & 0 deletions include/functions.h
Original file line number Diff line number Diff line change
Expand Up @@ -1966,6 +1966,9 @@ s32 osPfsDeleteFile(OSPfs* pfs, u16 companyCode, u32 gameCode, u8* gameName, u8*
s32 __osPfsReleasePages(OSPfs* pfs, __OSInode* inode, u8 initialPage, u8 bank, __OSInodeUnit* finalPage);
void guOrthoF(f32[4][4], f32, f32, f32, f32, f32, f32, f32);
void guOrtho(Mtx*, f32, f32, f32, f32, f32, f32, f32);
#ifdef DISABLE_LOOKUP_TABLE
f32x2 sincos(s16 angle);
#endif
f32 cosf(f32 angle);
s16 coss(u16 angle);
void osViSetEvent(OSMesgQueue* mq, OSMesg msg, u32 retraceCount);
Expand Down
8 changes: 8 additions & 0 deletions include/macros.h
Original file line number Diff line number Diff line change
Expand Up @@ -214,3 +214,11 @@ extern struct GraphicsContext* __gfxCtx;
} while (0)

#endif

#ifdef DISABLE_LOOKUP_TABLE

#define ONE 1.0f
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why using a macro for this? it looks weird to me 🤔 (the ONE one)

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yeah i thought i would need to do the function twice and forgot

#define SECOND_ORDER_COEFFICIENT 0.0000000010911122665310369f
Yanis002 marked this conversation as resolved.
Show resolved Hide resolved
#define quasi_cos_2(x) (ONE - SECOND_ORDER_COEFFICIENT * x * x)

#endif
8 changes: 8 additions & 0 deletions include/ultra64/ultratypes.h
Original file line number Diff line number Diff line change
@@ -1,8 +1,16 @@
#include "config.h"

#ifndef ULTRA64_ULTRATYPES_H
#define ULTRA64_ULTRATYPES_H

#ifdef _LANGUAGE_C

#ifdef DISABLE_LOOKUP_TABLE
typedef _Complex float f32x2;
#define F32X2_NEW(x, y) __builtin_complex((float) (x), (float) (y))
#define F32X2_AT(pair, idx) \
__builtin_choose_expr(idx / (_idx == 0 || idx == 1), __real__(pair), __imag__(pair))
#endif
typedef signed char s8;
typedef unsigned char u8;
typedef signed short int s16;
Expand Down
38 changes: 38 additions & 0 deletions src/code/z_lib.c
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
#include "global.h"

#include "config.h"

/**
* memset: sets `len` bytes to `val` starting at address `dest`.
*
Expand All @@ -23,20 +25,56 @@ void Lib_MemSet(u8* dest, size_t len, u8 val) {
// clang-format on
}

#ifdef DISABLE_LOOKUP_TABLE
/**
* @param angle binang
* @return sinecos(sine, cosine)
*/

Yanis002 marked this conversation as resolved.
Show resolved Hide resolved
f32x2 sincos(s16 angle) {
s32 shifter = (angle ^ (angle << 1)) & 0xC000;
f32 x = (f32) (((angle + shifter) << 17) >> 16);
float cosx = quasi_cos_2(x);
float sinx = sqrtf(ONE - cosx * cosx);

if (shifter & 0x4000) {
float temp = cosx;
cosx = sinx;
sinx = temp;
}
if (angle < 0) {
sinx = -sinx;
}
if (shifter & 0x8000) {
cosx = -cosx;
}
return F32X2_NEW(sinx, cosx);

}
#endif

/**
* @param angle binang
* @return cos(angle)
*/
f32 Math_CosS(s16 angle) {
#ifdef DISABLE_LOOKUP_TABLE
return __imag__(sincos(angle));
#else
return coss(angle) * SHT_MINV;
#endif
}

/**
* @param angle binang
* @return sin(angle)
*/
f32 Math_SinS(s16 angle) {
#ifdef DISABLE_LOOKUP_TABLE
return __real__(sincos(angle));
#else
return sins(angle) * SHT_MINV;
#endif
}

/**
Expand Down