Skip to content

Instantly share code, notes, and snippets.

@slembcke slembcke/mulx.c
Last active Oct 30, 2019

Embed
What would you like to do?
Signed 16.16 fixed point multiply.
static inline fixed mulx_fast(fixed x, fixed y){return (fixed){(x.asint >> 8) * (y.asint >> 8)};}
static inline fixed mulx(fixed x, fixed y){
bool neg = (x.asint ^ y.asint) < 0;
union {
u32 asint;
struct {u16 hi, lo;};
} _x, _y;
if(x.asint >= 0) _x.asint = x.asint; else _x.asint = -x.asint;
if(y.asint >= 0) _y.asint = y.asint; else _y.asint = -y.asint;
u32 z = 0;
z += (_x.hi*_y.hi) << 16;
z += (_x.hi*_y.lo);
z += (_x.lo*_y.hi);
z += (_x.lo*_y.lo) >> 16;
return (fixed){neg ? -z : z};
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.