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 7 commits
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. This has a speed boots and makes the calculation more precise.
Copy link
Collaborator

Choose a reason for hiding this comment

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

boots

#define DISABLE_SIN_COS_LOOKUP_TABLE

// 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 @@ -1978,6 +1978,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_SIN_COS_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 @@ -218,3 +218,11 @@ extern struct GraphicsContext* __gfxCtx;
} while (0)

#endif

#ifdef DISABLE_SIN_COS_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_SIN_COS_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
36 changes: 36 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,54 @@ void Lib_MemSet(u8* dest, size_t len, u8 val) {
// clang-format on
}

#ifdef DISABLE_SIN_COS_LOOKUP_TABLE
/**
* @param angle binang
* @return sinecos(sine, cosine)
*/
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(1.0f - 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_SIN_COS_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_SIN_COS_LOOKUP_TABLE
return __real__(sincos(angle));
#else
return sins(angle) * SHT_MINV;
#endif
}

/**
Expand Down
5 changes: 5 additions & 0 deletions src/libultra/gu/coss.c
Original file line number Diff line number Diff line change
@@ -1,9 +1,14 @@
#include "global.h"

#include "config.h"
/**
* @param angle binang
* @return cos(angle)*0x7FFF
*/
s16 coss(u16 angle) {
#ifdef DISABLE_SIN_COS_LOOKUP_TABLE
return Math_CosS(angle) * 0x7FFF;
#else
return sins(angle + 0x4000);
#endif
}
8 changes: 7 additions & 1 deletion src/libultra/gu/sins.c
Original file line number Diff line number Diff line change
@@ -1,12 +1,17 @@
#include "ultra64.h"

#include "config.h"
#ifndef DISABLE_SIN_COS_LOOKUP_TABLE
#include "sintable.inc.c"

#endif
/**
* @param angle binang
* @return sin(angle)*0x7FFF
*/
s16 sins(u16 angle) {
#ifdef DISABLE_SIN_COS_LOOKUP_TABLE
return Math_SinS(angle) * 0x7FFF;
Copy link
Collaborator

Choose a reason for hiding this comment

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

this function isn't declared (just add #include "functions.h") and it returns a f32, not s16, I feel like we should add a cast here (same comment for coss for the cast)

#else
s16 value;

angle >>= 4;
Expand All @@ -22,4 +27,5 @@ s16 sins(u16 angle) {
} else {
return value;
}
#endif
}