itofix(3)

πŸ“¦ version 4.4.3.1, in fedora 43, fedora 44

πŸ’– itofix(3) β€” the Allegro manual, but make it make sense πŸ’–

okay bestie, so this is a page from the Allegro manual, the game programming library, and it's about one tiny little function that does so much heavy lifting. itofix β€” int to fixed. say it with me. int. to. fixed. ✨


✨ SYNOPSIS ✨

#include <allegro.h>

fixed itofix(int x);

like. that's it. that's the whole signature. one int goes in, one fixed comes out. we love a low-effort king πŸ‘‘


πŸ¦‹ DESCRIPTION πŸ¦‹

okay so what it does: it converts an integer to fixed point. literally. that's the job.

and if you're sitting there like "bestie what does that even mean" β€” it's the same thing as x<<16. that's the whole trick. shift left sixteen bits and boom, you're in fixed-point land. no cap. πŸ’…

⚠️ THE PART YOU NEED TO HEAR, FR FR: overflows and underflows are not detected. not even in debug builds. like, the debug build is not gonna save you here. it's not gonna warn you. it's not gonna hold your hand. the values simply "wrap around". trying to convert an integer greater than 32767? overflow. trying to convert an integer lesser than -32768? underflow. both of them just wrap. silently. iconic behaviour honestly but also terrifying 😭

here's the example, and pls look at it because it is literally demonstrating the crime:

fixed number;
/* This conversion is OK. */
number = itofix(100);
ASSERT(fixtoi(number) == 100);
number = itofix(64000);
/* This check will fail in debug builds. */
ASSERT(fixtoi(number) == 64000);

see how itofix(100) is fine and ASSERT(fixtoi(number) == 100) passes? cute. and then itofix(64000) happens and the assert below it will fail in debug builds. the conversion is "OK" in the sense that it compiles and runs, but 64000 is bigger than 32767, so. yeah. wrap city. πŸ™ˆ


πŸŽ€ RETURN VALUE πŸŽ€

it returns the value of the integer converted to fixed point, ignoring overflows. ignoring them!! like they didn't even happen!! the overflow is not being handled, it's being ignored, which is a whole mood but also a whole footgun. 🫢


🌸 SEE ALSO 🌸

go read these next, bestie, they're the whole friend group:


and that's the page!! sending u off with all the love in the world from Allegro version 4.4.3 πŸ’œβœ¨ stay safe out there and maybe don't pass 64000 to itofix unless u want chaos. mwah. πŸŽ€πŸ¦‹πŸ’–

πŸ“¦ where this page comes from