Fixed point math

Other misc things
Post Reply
vagant
Member
Posts: 25
Joined: Mon Jun 30, 2025 9:33 am

Fixed point math

Post by vagant »

Hi,

I’d like to speed up the calculations by replacing floating-point math with fixed-point arithmetic. I’m aware of the existing math_fix16.h library, which uses Q8.8 format, but its precision isn’t sufficient for my needs. Has anyone tried other fixed-point math libraries that offer better accuracy?



Cheers
Michal
User avatar
dom
Well known member
Posts: 2319
Joined: Sun Jul 15, 2007 10:01 pm

Re: Fixed point math

Post by dom »

What range of numbers are you looking for? What sort of decimal precision?

To my knowledge there's no other complete fixed point library for the z80. There's fragments of a 16.16 but I'm not wholly convinced that the balance of range/decimal precision is quite right.

Have you taken a look at math16? Does that have enough range/precision?
vagant
Member
Posts: 25
Joined: Mon Jun 30, 2025 9:33 am

Re: Fixed point math

Post by vagant »

I did look at math16 and it lacks a bit. I am coding now my version of 16.16 but without using int64_t.
stefano
Well known member
Posts: 2328
Joined: Mon Jul 16, 2007 7:39 pm

Re: Fixed point math

Post by stefano »

Are you looking for a full implementation or just the basic algebra stuff?
vagant
Member
Posts: 25
Joined: Mon Jun 30, 2025 9:33 am

Re: Fixed point math

Post by vagant »

Just basic. I have coded simple raycaster on PCW and was interested if I can make it any faster by using fixed math. I need to convert from flotat/int into fixed and back, do the normal math - nor much more.
stefano
Well known member
Posts: 2328
Joined: Mon Jul 16, 2007 7:39 pm

Re: Fixed point math

Post by stefano »

IMHO the real boost comes with pre-computed vectors or just using integer values.
If it's not spoiling your fun, can you share the part of your code requiring a boost?
vagant
Member
Posts: 25
Joined: Mon Jun 30, 2025 9:33 am

Re: Fixed point math

Post by vagant »

I am following the standard Raycaster model published by Lode here: https://lodev.org/cgtutor/raycasting.html
There is a source code included and is using double numbers for any calculations - I would like to check how much faster the same algorithm would run on Z80 machine i.e. PCW using fixed point math. I know this is not optimal algorithm plus there are faster ways of doing raycaster but just wanted to try and learn Z88dk as a result.
Plus it was always on my mind to learn fixed point math in C and in asm.
vagant
Member
Posts: 25
Joined: Mon Jun 30, 2025 9:33 am

Re: Fixed point math

Post by vagant »

So I have done it and the result is slower than using float :)

I guess this is down to pushing for Q16.16 accuracy without having int64_t available and working around it with int32_t. Anyway quite a bit of learning here for me. Thanks to everyone for suggestions and help!
User avatar
dom
Well known member
Posts: 2319
Joined: Sun Jul 15, 2007 10:01 pm

Re: Fixed point math

Post by dom »

int64_t is available btw!
vagant
Member
Posts: 25
Joined: Mon Jun 30, 2025 9:33 am

Re: Fixed point math

Post by vagant »

dom wrote: Tue Jul 01, 2025 11:11 am int64_t is available btw!
I can see this being defined in stdint.h but any operations using int64_t is resulting in the following error being generated by the ZCC: error: undefined symbol: l_i64_s64_toi32. From what I understood the best you can get is 48 bit using math48?
User avatar
dom
Well known member
Posts: 2319
Joined: Sun Jul 15, 2007 10:01 pm

Re: Fixed point math

Post by dom »

vagant wrote: Tue Jul 01, 2025 11:59 am I can see this being defined in stdint.h but any operations using int64_t is resulting in the following error being generated by the ZCC: error: undefined symbol: l_i64_s64_toi32. From what I understood the best you can get is 48 bit using math48?
That's curious, it's defined and a down conversion from int64_t to int32_t works for me. I'll investigate more later on - can I assume you're on a nightly?

There is support for an 8 byte float - mbf64 but that relies on the code being in Microsoft BASIC so is only on Laser500, Primo, TRS80 and MC1000. Only the basic operations are 64 bit, trig etc are down-converted to mbf32 to use the ROM routines.
vagant
Member
Posts: 25
Joined: Mon Jun 30, 2025 9:33 am

Re: Fixed point math

Post by vagant »

dom wrote: Tue Jul 01, 2025 1:51 pm ... can I assume you're on a nightly?
And this is where the problem was! I pulled the nightly, built and run again and now it worked. It is now definitely faster but still not as fast as the standard --math32 and float,
User avatar
feilipu
Member
Posts: 59
Joined: Tue Nov 15, 2016 5:02 am

Re: Fixed point math

Post by feilipu »

You might find the core of math16 useful for what you need.

https://github.com/z88dk/z88dk/blob/mas ... /README.md

I used a “_f24” format with 8-bit exponent (same as a single float) and 16-bit mantissa for internal calculations. This format is no slower than the half float _Float16 format on the z80, and actually faster for chained calculations (like the division, square root, and polynomial functions), because the conversion between the two formats is a bit ugly.

You might find raycaster written in _f24 is a good compromise solution.
vagant
Member
Posts: 25
Joined: Mon Jun 30, 2025 9:33 am

Re: Fixed point math

Post by vagant »

Thanks! Will give it a go.
Post Reply