r/programming • • Sep 08 '26

video on improving performance of arctan for Nintendo 64 by Kaze Emanuar "spent weeks to save 100 Microseconds"

https://www.youtube.com/watch?v=U2K44nC8c8g
259 Upvotes

43 comments sorted by

59

u/danielcw189 Sep 08 '26

to clarify: the full title of the video is "He spent weeks to save 100 Microseconds". The video is by Kaze Emunar, but he didn't do the lion-share of the work here.

42

u/hughperman Sep 08 '26

If that's 100 microseconds out of 101 microseconds, that's pretty great

7

u/Ameisen Sep 09 '26

When you have a frametime target, 100us is a pretty big win. Even on modern hardware.

164

u/bwainfweeze Sep 08 '26

TLDR: Found an arctan approximation that saves a kilobyte of cache, which reduces memory contention for the rest of the game.

Mario 64 uses a lookup table for arctan, trading time for space, but the processor is almost never saturated while the memory bus often is.

28

u/KainMassadin Sep 08 '26

broom broom

16

u/danielcw189 Sep 09 '26

goes the RAMBUS :)

68

u/levodelellis Sep 08 '26

You missed that it was more accurate too, and they had to dodge gcc adding a NOP (a fix to a bug) which makes it need an extra cache line

10

u/sputwiler Sep 09 '26

had to dodge gcc adding a NOP (a fix to a bug)

Is that GCC just trying to fill the infamous MIPS delay slot or an actual bug

23

u/Nobody_1707 Sep 09 '26

It was a bug with floating point multiplication on early revisions of the hardware, but since the compiler can't know which revision of the N64 is going to run the code, it has to be pessimistic. The algorithm can't actually hit the bug, but GCC had been hotfixed to just always put two NOPs after the mul instruction instead of analyzing whether the bug could actually occur.

https://n64brew.dev/wiki/VR4300#Multiplication_Bug

6

u/Uristqwerty Sep 09 '26

Though if I understand the video correctly, they're still including the workaround. It's just that plenty of non-NOP instructions work just as well for avoiding the bug, yet the compiler wasn't automatically interleaving other code between the multiplies; they had to rewrite the function in a presumably-less-idiomatic way to ensure no potentially-buggy multiplies generated immediately adjacent, and thus GCC would never waste space with a NOP.

4

u/danielcw189 Sep 10 '26

I understood it as them writing the assembler of the function's body directly.

4

u/Uristqwerty Sep 12 '26

Two different parts of the video. It shows only 5 inline assembler instructions around 16:30 to solve a nonsense jump and inefficient immediate value, but then at 17:30 it shows changes to C code to avoid the floating point multiply NOP.

3

u/Nobody_1707 Sep 11 '26

Yes, they needed to rewrite the function in mostly inline assembly to fix this bug, and to avoid an instance of GGC just not optimizing well.

2

u/Uristqwerty Sep 12 '26

At the part they talk about inline assembly, they show a 5-instruction asm block, out of a 40-instruction-after-workarounds function. And a minute later, they show C code when talking about workarounds for the multiply bug. It's both, in different places!

5

u/Ameisen Sep 09 '26

I hate that delay slot. It complicates binary translation of MIPS immensely.

I fully understand why it exists... I just despise it.

2

u/sputwiler Sep 10 '26

I love the delay slot. It feels like a final word or a fade shot.

But then my first exposure to MIPS was writing assembly for the thing.

5

u/Ameisen Sep 10 '26

It just complicates emulation. Particularly recompilation.

When you hit a non-compact branch, it basically acts like an instruction pair. However, if you jump to the subsequent instruction, it doesn't. This causes it to be more complex.

My LLVM toolchain for my emulator prefers compact branches for this reason - the code runs faster.

Also, it causes issues if you have a chunked recompiler, because if a non-compact branch is the last instruction in a chunk, when you invalidate a chunk, you also have to invalidate the subsequent chunk.

3

u/sputwiler Sep 10 '26

Yeah real. I was just saying the only time I dealt with them was writing MIPS code, which wasn't much of a pain at all.

2

u/Ameisen Sep 10 '26

It is sometimes difficult to actually have something for the slot to do: it executes after the branch condition is resolved, and also cannot contain a control instruction. This can make things like jump tables inefficient.

17

u/floodyberry Sep 08 '26

looks like unreal's custom atan2 was for compiler/library bug reasons, not performance, it just happened to be faster

also weird that unreal hasn't switched to using (sin|cos|tan)pi

6

u/danielcw189 Sep 09 '26

link doesn't work for me

6

u/Lachiko Sep 09 '26

I had a look and it seems it requires an epic account

To access our repositories, sign up for an Epic account at UnrealEngine.com and register your GitHub ID using these instructions.

6

u/floodyberry Sep 09 '26

oops

// atan2f occasionally returns NaN with perfectly valid input (possibly due to a compiler or library bug).
// We are replacing it with a minimax approximation with a max relative error of 7.15255737e-007 compared to the C library function.
// On PC this has been measured to be 2x faster than the std C version.

the coefficients come from david eberly's geometric tools library

25

u/levodelellis Sep 08 '26

I see kaze, I know I'm in for a good time

1

u/iTiraMissU Sep 09 '26

Did I always misread his YouTube name as “Kaze Emulator” or did he change his channel name recently? I feel so dumb right now.

5

u/danielcw189 Sep 09 '26 edited Sep 09 '26

I have been watching his channel for at least 3 years now, and as far as I remember it has been like "Emunar" (actually: Emanuar). Before your comment the thought, that his username could be about Emulators, never crossed my mind. And his work usually aims for real hardware, and not emulators

1

u/PTMurasaki Oct 26 '26

I'm pretty sure it's either his actual name, or something he came up with that works like a real name does, so Emanuar would be the surname here

-22

u/dsrules Sep 09 '26

This guy is obviously smart and knows ultra64 in and out but he shouldn’t act like HE did it when it’s the work of others mostly.

6

u/danielcw189 Sep 09 '26

he doesn't act like it in the video

-46

u/NoHurry28 Sep 09 '26

I wish people used AI to do shit like this and make all our code better instead of vibing out slop making all of our code worse

-69

u/Slight-Bluebird-8921 Sep 08 '26

this guy is pure boy who cried wolf

every 3 days he releases a video where he says he improved the performance of mario64 by 300% and then the game runs exactly the same

shut up already geezus

18

u/danielcw189 Sep 09 '26

over the years he increased fps by a lot.

4

u/danielcw189 Sep 10 '26

every 3 days he releases a video

on his main channel he doesn't even release 1 video per month.

43

u/puppable Sep 09 '26 edited Sep 09 '26

What...? He's very prominently showcased in basically every video the mod he's been using these optimizations in. Do you say things just to say them?
Edit: Even beyond that, Mario 64 is practically a model species for studying the history of video game programming patterns. It's an interesting subject in its own right.

28

u/Efficient-Chair6250 Sep 09 '26

In practically every video he barely achieves one extra FPS. It's a veeery slow process. I think you should actually try to pay attention and not be jealous

5

u/Ameisen Sep 10 '26 edited Sep 10 '26

I should note that we don't measure in FPS but in the reciprocal: MSPF. Framerate is non-linear: 29fps to 30fps isn't the same as 9fps to 10fps (1.15ms improvement as opposed to 11.1ms).

-34

u/useful_idiot Sep 09 '26

I wish I had the time and crayons to post like this

11

u/Efficient-Chair6250 Sep 09 '26

Nobody said you have to like micro optimizations, but plenty of us do. And at the end of the day, it seems he nearly achieved his goal already.

Sometimes things just take relentless dedication. And sometimes people like you just lack it

6

u/kevkevverson Sep 09 '26

Oof I’m embarrassed for you

13

u/clarkster Sep 09 '26

Just because you don't understand it, doesn't mean he's wrong. Confident ignorance is hilarious

0

u/[deleted] Sep 09 '26

[deleted] — view deleted comment

42

u/danielcw189 Sep 09 '26

this is a bottleneck

36

u/-BunsenBurn- Sep 09 '26

The fun of the exercise is optimizing.

Premature? The game is 30 years old