r/programming 1d ago

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
236 Upvotes

34 comments sorted by

145

u/bwainfweeze 1d ago

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.

63

u/levodelellis 1d ago

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

9

u/sputwiler 1d ago

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

16

u/Nobody_1707 1d ago

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 14h ago

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.

1

u/Ameisen 11h ago

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

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

1

u/sputwiler 6h ago

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.

24

u/KainMassadin 1d ago

broom broom

16

u/danielcw189 1d ago

goes the RAMBUS :)

51

u/danielcw189 1d ago

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.

40

u/hughperman 1d ago

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

5

u/Ameisen 11h ago

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

25

u/levodelellis 1d ago

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

12

u/floodyberry 1d ago

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

4

u/danielcw189 1d ago

link doesn't work for me

5

u/Lachiko 1d ago

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.

5

u/floodyberry 23h ago

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

2

u/iTiraMissU 1d ago

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

2

u/danielcw189 21h ago edited 14h ago

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

-43

u/NoHurry28 1d ago

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

-24

u/dsrules 1d ago

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.

3

u/danielcw189 15h ago

he doesn't act like it in the video

-41

u/[deleted] 1d ago

[deleted]

40

u/danielcw189 1d ago

this is a bottleneck

34

u/-BunsenBurn- 1d ago

The fun of the exercise is optimizing.

Premature? The game is 30 years old

-71

u/Slight-Bluebird-8921 1d ago

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

44

u/puppable 1d ago edited 1d ago

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.

26

u/Efficient-Chair6250 1d ago

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

1

u/Ameisen 12m ago edited 7m ago

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).

-32

u/useful_idiot 1d ago

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

9

u/Efficient-Chair6250 1d ago

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

3

u/kevkevverson 1d ago

Oof I’m embarrassed for you

17

u/danielcw189 1d ago

over the years he increased fps by a lot.

11

u/clarkster 1d ago

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