Skip to content

Instantly share code, notes, and snippets.

@slembcke
Last active October 30, 2019 04:28
Show Gist options
  • Save slembcke/f86b6bd2524dbaebe65e422ff3db0f8b to your computer and use it in GitHub Desktop.
Save slembcke/f86b6bd2524dbaebe65e422ff3db0f8b to your computer and use it in GitHub Desktop.
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