Fixed point multiplication

I decided to experiment with @Pharap’s FixedPoints library in my jam entry.
The optimal format in my case is SQ23.8 and I need to do a lot of multiplication.

Basically, multiplying two 23.8 numbers looks something like this:
result = A * B >> 8;

Unfortunately, the right shift is done by dividing by 256 instead of simply discarding the lower byte.
In my game, this results in a hex file with 22370 bytes and a cpuLoad of over 110. Too big, too slow.

I added the following template specialization and the hex file dropped to 18118 bytes (4250 bytes smaller), with a cpuLoad of 80.

template<>
SFixed<23,8> operator *(const SFixed<23,8> & left, const SFixed<23,8> & right)
{
  int64_t r = left.getInternal() * right.getInternal();
  void *b = static_cast<void *>(&r);
  return SFixed<23, 8>::fromInternal( *static_cast<int32_t*>(b+1) );
}
2 Likes

2 posts were merged into an existing topic: A Fixed Point Arithmetic Library