Fixed point math
Fixed point math
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
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
Re: Fixed point math
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?
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?
Re: Fixed point math
I did look at math16 and it lacks a bit. I am coding now my version of 16.16 but without using int64_t.
Re: Fixed point math
Are you looking for a full implementation or just the basic algebra stuff?
Re: Fixed point math
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.
Re: Fixed point math
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?
If it's not spoiling your fun, can you share the part of your code requiring a boost?
Re: Fixed point math
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.
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.
Re: Fixed point math
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!

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!
Re: Fixed point math
int64_t is available btw!
Re: Fixed point math
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?
Re: Fixed point math
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.
Re: Fixed point math
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.
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.
Re: Fixed point math
Thanks! Will give it a go.