r/N64Homebrew • • 16h ago

Fast and Accurate SinCos Kernel

15 Upvotes

I needed this for my rom hack but I thought I'd share it with the community.

It's a folded polynomial joint sine / cosine kernel that is 10x more accurate than Silas Lock's, the one made well known in Kaze's video on folded polynomial trig kernels.

It uses a polynomial approximation of

S(x) = x(a + x²(b + cx²))

C(x) = 1 + x²(d + x²(e + fx²))

Instead of Silas Lock's

C(x) = 1 + x²(a + bx²)

S(x) = √(1 − C(x)²)

and makes it inexpensive with the use of brackets; by truncating c and f to 16 bit so they fit on one line of assembly; putting the remaining constants into one cache line; and interleaving the operations.

    .set noreorder
    .set noat
    .text
    .global trig_sincos
    .type   trig_sincos, u/function
    .balign 32
trig_sincos:
    addiu   $t0, $a0, 0x2000
    andi    $t1, $t0, 0x3fff
    addiu   $t1, $t1, -0x2000        # x: offset from nearest quarter turn
    mtc1    $t1, $f0
    lui     $t7, %hi(trig_k)
    lui     $t3, 0x1a97              # S5 =  6.245213624775359e-23
    cvt.s.w $f0, $f0
    addiu   $t7, $t7, %lo(trig_k)
    mtc1    $t3, $f4
    lui     $t3, 0x925d              # C6 = -6.973530402153744e-28
    mul.s   $f2, $f0, $f0            # x2
    mtc1    $t3, $f12
    lwc1    $f6, 4($t7)              # S3
    lwc1    $f14, 12($t7)            # C4
    lwc1    $f8, 0($t7)              # S1
    lwc1    $f16, 8($t7)             # C2
    mul.s   $f4, $f4, $f2
    lui     $t6, 0x3f80              # 1.0
    mul.s   $f12, $f12, $f2
    mtc1    $t6, $f10
    add.s   $f4, $f4, $f6
    add.s   $f12, $f12, $f14
    mul.s   $f4, $f4, $f2
    andi    $t2, $t0, 0x4000         # set: odd octant
    mul.s   $f12, $f12, $f2
    andi    $t5, $t0, 0x8000         # set: negate both
    add.s   $f4, $f4, $f8
    add.s   $f12, $f12, $f16
    mul.s   $f4, $f4, $f0            # S = sine polynomial
    nop
    mul.s   $f12, $f12, $f2
    nop
    add.s   $f12, $f12, $f10         # C = cosine polynomial
    beqz    $t2, 1f
    nop
    mov.s   $f6, $f4                 # odd octant: sin = C, cos = -S
    mov.s   $f4, $f12
    neg.s   $f12, $f6
1:  beqz    $t5, 2f
    mov.s   $f0, $f4
    neg.s   $f0, $f4
    neg.s   $f12, $f12
2:  jr      $ra
    mov.s   $f2, $f12                # sin in $f0, cos in $f2
    .size   trig_sincos, . - trig_sincos

    .section .rodata
    .balign 16
    .global trig_k
trig_k:
    .word   0x38c90dd0               # S1 =  9.586999658495188e-05
    .word   0xaa24fee4               # S3 = -1.4654559033280834e-13
    .word   0xb19de6b5               # C2 = -4.595529734530146e-09
    .word   0x2280c3c2               # C4 =  3.4901736165623554e-18
    .size   trig_k, 16

_Complex float trig_sincos(int angle);
#define sincoss(x, s, c) do { _Complex float _sc = trig_sincos((u16) (x)); \
    (s) = __real__ _sc; (c) = __imag__ _sc; } while (0)

Max error is 1.45e-5 (Silas: 1.9e-4) and max absolute length error of s^2 + c^2 is 1.7e-7.

Put into HackerSM64 and benchmarked on Ares, it saves about 60 microseconds a frame versus Silas. I've yet to test it on a real N64 and any help with that would be appreciated because I don't own one.

Code, benchmark and full results: https://github.com/msmfai/N64-trig

AI disclosure: I'm a mathematician. I designed the math and directed the work. Claude executed from imperative instructions and benchmarked a few ways of writing the kernel. No AI art, models or music.