CPC Maths working again
CPC Maths working again
It looks like the CPC maths library rotted over the past 20 years of no one using it.
I've now repaired it, so it should be working again. Whilst doing so I ran across a disassembly of it here: https://github.com/Bread80/CPC6128-Firm ... PMaths.asm which makes me interested in creating a standalone library and running through our fp benchmarks.
I've now repaired it, so it should be working again. Whilst doing so I ran across a disassembly of it here: https://github.com/Bread80/CPC6128-Firm ... PMaths.asm which makes me interested in creating a standalone library and running through our fp benchmarks.
Re: CPC Maths working again
wonderful, I suspected it was failing
nice find, too
nice find, too
Re: CPC Maths working again
I've now added a generic version of the library that can be used on z80 classic targets with the flag --math-cpc
Given that it uses both index registers the machines it can be used on will be limited but it's another curio.
I've run the n-body and spectral-norm tests on it and update the benchmark page: https://github.com/z88dk/z88dk/wiki/Cla ... benchmarks
Given that it uses both index registers the machines it can be used on will be limited but it's another curio.
I've run the n-body and spectral-norm tests on it and update the benchmark page: https://github.com/z88dk/z88dk/wiki/Cla ... benchmarks
Re: CPC Maths working again
It had a good balance between speed and accuracy, it's an interesting option
Re: CPC Maths working again
I think it's great that it's finally working!
(No I have no idea what it's for or how to work with it. Perhaps I just don't know where to find the documentation, or perhaps I never needed a math library myself (because all the maths in my games were mostly just additions and subtractions.))
(No I have no idea what it's for or how to work with it. Perhaps I just don't know where to find the documentation, or perhaps I never needed a math library myself (because all the maths in my games were mostly just additions and subtractions.))
Re: CPC Maths working again
Quite, I think most people don't use the floating point libraries at all - when writing games there's not much need for them and usually much better ways to achieve the result (eg there's lib3d about the place which provides integer trig functions).
However, we've built up quite a collection of them over the years, and from a software archaeology perspective it's interesting to see the approaches that were taken, the compromises that result and how design decisions affect performance. We've also got our home-grown 16 and 32 bit IEEE libraries which @feilipu had a lot of fun writing.
Switching between the various implementations shouldn't involve a change to C code - they all implement the compiler primitives and <math.h> functions, so it's a case of just changing the compiler options to change the library. Details on the flags are on the wiki here: https://github.com/z88dk/z88dk/wiki/Cla ... -Libraries
As I said it's a curio area of the codebase, but I find it quite interesting. One of the things we've yet to do is to support any of the BCD based maths libraries (eg MSX or Computers Lynx) which I've been eyeing up for a few years. Again the usefulness is debatable, but I have to admit I'm curious as to why the MSX does have BCD math: all the Microsoft BASIC ancestors that I've seen use an MBF format (32, 48, 64 bit).
However, we've built up quite a collection of them over the years, and from a software archaeology perspective it's interesting to see the approaches that were taken, the compromises that result and how design decisions affect performance. We've also got our home-grown 16 and 32 bit IEEE libraries which @feilipu had a lot of fun writing.
Switching between the various implementations shouldn't involve a change to C code - they all implement the compiler primitives and <math.h> functions, so it's a case of just changing the compiler options to change the library. Details on the flags are on the wiki here: https://github.com/z88dk/z88dk/wiki/Cla ... -Libraries
As I said it's a curio area of the codebase, but I find it quite interesting. One of the things we've yet to do is to support any of the BCD based maths libraries (eg MSX or Computers Lynx) which I've been eyeing up for a few years. Again the usefulness is debatable, but I have to admit I'm curious as to why the MSX does have BCD math: all the Microsoft BASIC ancestors that I've seen use an MBF format (32, 48, 64 bit).
-
- Member
- Posts: 71
- Joined: Sun Apr 01, 2018 4:02 pm
Re: CPC Maths working again
I you ask me IMHO a Fixed Point library would be a great addition. Very used for games and in format Q8.8 fits nicely in the Z80.
You operate with a vector Q8.8 for iteration movement and then “apply” the integer part to your (unsigned) integer vector x,y for the integer positions of elements, preserving the decimal part for forward accumulation.
You operate with a vector Q8.8 for iteration movement and then “apply” the integer part to your (unsigned) integer vector x,y for the integer positions of elements, preserving the decimal part for forward accumulation.
Re: CPC Maths working again
I think there is already a 8.8 library available somewhere, even if I have never used it.
I do think it would be useful though, and I am guessing that Churrera is using something similar, because they have accerleration and therefore fixed point coordinates.
It would depends on how good it is before I use it, but I can see other people using it if we can find this library again.
I do think it would be useful though, and I am guessing that Churrera is using something similar, because they have accerleration and therefore fixed point coordinates.
It would depends on how good it is before I use it, but I can see other people using it if we can find this library again.
Re: CPC Maths working again
I am one of the few who is using the floating point library in my Elite Planet Browser.
I'm testing it with my little floatcalctest.c program:
Compiling it with...
zcc +gb -create-app --math-cpc -o fctgb.gb floatcalctest.c
...brings this error:
../cimpl/ftoe.c::ftoe::0::0:19: error: undefined symbol: l_gintspsp
^---- l_gintspsp
The Gameboy seems to be a classic target, is there a chance to get it working there as well?
I'm testing it with my little floatcalctest.c program:
Code: Select all
#include <stdio.h>
void main()
{
// Clear the screen
printf("%c",12);
printf("\x0c"); // the "\x0c" (0x0c character) resets the cursor position to the top left corner
printf("\nhello world\n");
printf("\npress any key to calculate 5*0.1...\n");
fgetc_cons(); // wait for keypress
printf("\n5*0.1 is %.1f\n",5*0.1);
printf("\npress any key to exit...\n");
fgetc_cons(); // wait for keypress
}
zcc +gb -create-app --math-cpc -o fctgb.gb floatcalctest.c
...brings this error:
../cimpl/ftoe.c::ftoe::0::0:19: error: undefined symbol: l_gintspsp
^---- l_gintspsp
Re: CPC Maths working again
The cpcmath library unfortunately uses both index registers so can't work on the Gameboy.
The only maths library available for +gb is still MBF32 (--math-mbf32_gbz80)
The only maths library available for +gb is still MBF32 (--math-mbf32_gbz80)
-
- Member
- Posts: 71
- Joined: Sun Apr 01, 2018 4:02 pm
Re: CPC Maths working again
Looked at but cannot find.Timmy wrote: ↑Thu Apr 27, 2023 10:01 pm I think there is already a 8.8 library available somewhere, even if I have never used it.
I do think it would be useful though, and I am guessing that Churrera is using something similar, because they have accerleration and therefore fixed point coordinates.
It would depends on how good it is before I use it, but I can see other people using it if we can find this library again.
If interested, I can share my old fixed-point code, made at 2009 (a long time ago) with the purpose of making something for NDS (was never ported), so it is oriented to a 32-bit processor without FPU, and uses C++. It includes a full pack of Q16 (Q16.16), Vector3D and Matrix for fully operational computations. In fact I did (must be somewhere) a full functional example running (on PC) using only fixed-point, with element movement, transformations, collisions and etc. based on that lib.
Maybe I'd take it back, make a good clean to take only the required, adapting it to Q8.8 and C language.
Re: CPC Maths working again
I've collated most of the parts of a Q8.8 from cemetech/bug fixes on various forums, it just needs a fair bit of cleanup to make it coherent as a whole.
Prior to that I took a look at libfixmath which provided a Q16.16 implementation, unfortunately it doesn't compile down that compactly, so an assembler rewrite would be needed.
Prior to that I took a look at libfixmath which provided a Q16.16 implementation, unfortunately it doesn't compile down that compactly, so an assembler rewrite would be needed.
Re: CPC Maths working again
The Q8.8 work is currently in a branch here: https://github.com/z88dk/z88dk/tree/feature/fix88
The current state is that the following are working: multiply, divide, inverse, sqrt, log, log2, sin, cos. I need to create an exp() function to complete the set and then fill out the other trig functions.
Hopefully I'll be able to finish it off the code (if not the tests) this weekend
The current state is that the following are working: multiply, divide, inverse, sqrt, log, log2, sin, cos. I need to create an exp() function to complete the set and then fill out the other trig functions.
Hopefully I'll be able to finish it off the code (if not the tests) this weekend
Re: CPC Maths working again
I've now merged Q8.8 after manual testing, I still want to automate the testing.
Have fun with it.
Have fun with it.
-
- Member
- Posts: 71
- Joined: Sun Apr 01, 2018 4:02 pm
Re: CPC Maths working again
You are my hero 
I am at mobile I’ll take it a look tomorrow. Some reference to documentation or is integrated as native type?

I am at mobile I’ll take it a look tomorrow. Some reference to documentation or is integrated as native type?
Re: CPC Maths working again
It's not a native type - I kept it library only.
Usage should be fairly obvious (eg expfix88(), mulfix88() etc) and can be trivially deduced from the header file -https://github.com/z88dk/z88dk/blob/mas ... th_fix88.h
As a result, there's only some overview information here: https://github.com/z88dk/z88dk/wiki/Cla ... z80nrabbit
Usage should be fairly obvious (eg expfix88(), mulfix88() etc) and can be trivially deduced from the header file -https://github.com/z88dk/z88dk/blob/mas ... th_fix88.h
As a result, there's only some overview information here: https://github.com/z88dk/z88dk/wiki/Cla ... z80nrabbit
Re: CPC Maths working again
it is very nice to have something different added to the libraries
Re: CPC Maths working again
Thanks, the MBF32 library does everything that I need - it wasn't working yet on the Gameboy the last time that I tried it (long time ago).
What other targets do not support the standard -lm library? As far as I know the PMD85?
Re: CPC Maths working again
It's the 8080, 8085, gbz80 and possibly z180 machines that don't support genmath.
There's a csv file here with cpu details: https://github.com/z88dk/z88dk/blob/mas ... atures.csv