CPC Maths working again

Amstrad CPC and NC systems
Post Reply
User avatar
dom
Well known member
Posts: 2076
Joined: Sun Jul 15, 2007 10:01 pm

CPC Maths working again

Post by dom »

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.
stefano
Well known member
Posts: 2137
Joined: Mon Jul 16, 2007 7:39 pm

Re: CPC Maths working again

Post by stefano »

wonderful, I suspected it was failing
nice find, too
User avatar
dom
Well known member
Posts: 2076
Joined: Sun Jul 15, 2007 10:01 pm

Re: CPC Maths working again

Post by dom »

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
stefano
Well known member
Posts: 2137
Joined: Mon Jul 16, 2007 7:39 pm

Re: CPC Maths working again

Post by stefano »

It had a good balance between speed and accuracy, it's an interesting option
Timmy
Well known member
Posts: 392
Joined: Sat Mar 10, 2012 4:18 pm

Re: CPC Maths working again

Post by Timmy »

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.))
User avatar
dom
Well known member
Posts: 2076
Joined: Sun Jul 15, 2007 10:01 pm

Re: CPC Maths working again

Post by dom »

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).
DarkSchneider
Member
Posts: 71
Joined: Sun Apr 01, 2018 4:02 pm

Re: CPC Maths working again

Post by DarkSchneider »

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.
Timmy
Well known member
Posts: 392
Joined: Sat Mar 10, 2012 4:18 pm

Re: CPC Maths working again

Post by Timmy »

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.
User avatar
RobertK
Well known member
Posts: 347
Joined: Mon Feb 26, 2018 12:58 pm

Re: CPC Maths working again

Post by RobertK »

I am one of the few who is using the floating point library in my Elite Planet Browser.
dom wrote: Tue Apr 25, 2023 6:08 pm 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.
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	  
}
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
User avatar
dom
Well known member
Posts: 2076
Joined: Sun Jul 15, 2007 10:01 pm

Re: CPC Maths working again

Post by dom »

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)
DarkSchneider
Member
Posts: 71
Joined: Sun Apr 01, 2018 4:02 pm

Re: CPC Maths working again

Post by DarkSchneider »

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.
Looked at but cannot find.

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.
User avatar
dom
Well known member
Posts: 2076
Joined: Sun Jul 15, 2007 10:01 pm

Re: CPC Maths working again

Post by dom »

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.
User avatar
dom
Well known member
Posts: 2076
Joined: Sun Jul 15, 2007 10:01 pm

Re: CPC Maths working again

Post by dom »

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
User avatar
dom
Well known member
Posts: 2076
Joined: Sun Jul 15, 2007 10:01 pm

Re: CPC Maths working again

Post by dom »

I've now merged Q8.8 after manual testing, I still want to automate the testing.

Have fun with it.
DarkSchneider
Member
Posts: 71
Joined: Sun Apr 01, 2018 4:02 pm

Re: CPC Maths working again

Post by DarkSchneider »

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?
User avatar
dom
Well known member
Posts: 2076
Joined: Sun Jul 15, 2007 10:01 pm

Re: CPC Maths working again

Post by dom »

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
stefano
Well known member
Posts: 2137
Joined: Mon Jul 16, 2007 7:39 pm

Re: CPC Maths working again

Post by stefano »

it is very nice to have something different added to the libraries
User avatar
RobertK
Well known member
Posts: 347
Joined: Mon Feb 26, 2018 12:58 pm

Re: CPC Maths working again

Post by RobertK »

dom wrote: Fri Apr 28, 2023 7:31 am 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)
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?
User avatar
dom
Well known member
Posts: 2076
Joined: Sun Jul 15, 2007 10:01 pm

Re: CPC Maths working again

Post by dom »

RobertK wrote: Tue May 09, 2023 6:57 amWhat other targets do not support the standard -lm library? As far as I know the PMD85?
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
stefano
Well known member
Posts: 2137
Joined: Mon Jul 16, 2007 7:39 pm

Re: CPC Maths working again

Post by stefano »

RobertK wrote: Tue May 09, 2023 6:57 amWhat other targets do not support the standard -lm library? As far as I know the PMD85?
Regarding the library limits, I'm proud I adapted qsort() to the 8080,it should allow the othello game to run :D
Post Reply