[z88dk-dev] float math

Bridge to the z88dk-developers mailing list
Post Reply
alvin
Well known member
Posts: 1872
Joined: Mon Jul 16, 2007 7:39 pm

[z88dk-dev] float math

Post by alvin »

I was looking at doing float integration into the new lib. The genmath we have looks fairly fast but I also found another z80 implementation on the web written by Anders Hejlsberg (connected to Turbo Pascal and now C# at Microsoft). Has anyone looked at it before?

http://www.fam-bundgaard.dk/DOWNLOAD/DOWNLOAD.HTM

It's 48-bit (40 bit mantissa) which I think is the same as genmath. It also looks quite fast. It does not use any static data, instead operating on registers and the stack. A 48-bit float is held in BCDEHL and the EXX set can hold another one. I think genmath might be similar in performance but it might be interesting to try it out.



------------------------------------------------------------------------------
Dive into the World of Parallel Programming. The Go Parallel Website,
sponsored by Intel and developed in partnership with Slashdot Media, is your
hub for all things parallel software development, from weekly thought
leadership blogs to news, videos, case studies, tutorials and more. Take a
look and join the conversation now. http://goparallel.sourceforge.net/
stefano
Well known member
Posts: 2137
Joined: Mon Jul 16, 2007 7:39 pm

Post by stefano »

To me genmath looks the best compromise: In practice I think that the size optimization should be done by relying onto the existing ROM code and the speed by other kind of tricks (i.e. swtiching where possible to integers or using pre-calculated tables).



The only missing case is a super-high precision format-

I thought in the past to add an high precision implementation, basing on the TI calculators' format or onto a flexible size mantissa declaration, but then I fell in love to this cute genmath.

Do we really want to do that kind of work with a z80 ? ..well sure we want but how can we test it ?






Date: Sat, 7 Feb 2015 06:48:17 +0000
To: z88dk-developers@...
From: lists@...
Subject: [z88dk-dev] float math

I was looking at doing float integration into the new lib. The genmath we have looks fairly fast but I also found another z80 implementation on the web written by Anders Hejlsberg (connected to Turbo Pascal and now C# at Microsoft). Has anyone looked at it before?

http://www.fam-bundgaard.dk/DOWNLOAD/DOWNLOAD.HTM

It's 48-bit (40 bit mantissa) which I think is the same as genmath. It also looks quite fast. It does not use any static data, instead operating on registers and the stack. A 48-bit float is held in BCDEHL and the EXX set can hold another one. I think genmath might be similar in performance but it might be interesting to try it out.



------------------------------------------------------------------------------
Dive into the World of Parallel Programming. The Go Parallel Website,
sponsored by Intel and developed in partnership with Slashdot Media, is your
hub for all things parallel software development, from weekly thought
leadership blogs to news, videos, case studies, tutorials and more. Take a
look and join the conversation now. http://goparallel.sourceforge.net/
alvin
Well known member
Posts: 1872
Joined: Mon Jul 16, 2007 7:39 pm

Post by alvin »

I was looking at doing float integration into the new lib. The genmath we have looks fairly fast but I also found another z80 implementation on the web written by Anders Hejlsberg (connected to Turbo Pascal and now C# at Microsoft). Has anyone looked at it before?
I'm going to give math48 a try simply to figure out a clean way to integrate a selection of float packages into the new clib. Maybe sometime down the line it should be possible to support more than one float type (eg maybe the compiler will distinguish between float and double) and maybe some float types should optionally be implemented as fixed point instead.

I know that Stef likes genmath and I'm putting that in as well. Genmath has more math functions than math48 built-in, both are 48-bit (40 bit mantissa), both are written in assembly language and are relatively small. Math48 supports two floating point accumulators in registers: one is in BCDEHL and the other in BCDEHL'. The lib is completely re-entrant, does not require any static data and uses only one index register (ix). Genmath is using BCIXDE and uses the stack for a second operand; static data is used to store two fp accumulators although I am not sure if that's just a requirement for sccz80 yet? I think Stef removed use of both index registers and it's down to just ix.

The compilers currently have their own fixed ideas about what a float type is. sdcc's float is 32-bit and behaves like a long (passed by value on stack, etc); the binary format is built into the compiler (the compiler must be able to generate constants from source text). sccz80's float is 48-bit and is tied to genmath. floats can be passed on the stack or they may be stored in the static accumulators.

The idea to support any sort of float package is this and is a small extension of what Stef has been doing for the native math libraries:

The functions implemented by the library are included in the implementation directory. There may be minor modifications to pass through the clib's error_* subroutines to set errno on math errors.

A "z80" sub-directory contains an asm interface to implement all of C11's math.h functions. These are implemented in terms of the primitives supplied by the float package. Realistically only a subset of C11's functions will be defined and linking errors will occur if some math functions are not present.

The C interface is in subdirs "sccz80", "sdcc_ix", "sdcc_iy". This follows the pattern in the rest of the c library with functions here loading parameters from the stack into registers before jumping into the asm implementation in the "z80" directory. In this step, the compiler-side floating point format also has to be converted to the math package's floating point format and vice versa as control passes between the compiler and the math lib.

Many compiler-side required float primitives are implemented in the compiler primitive directories (l/sccz80, l/sdcc). Things like float compare, conversion to integers, etc. Both operands are in the compiler-defined float format and there is no need to carry out these functions in the float package as they are simple enough to implement in the compiler's float format and no loss of precision occurs (that's already happened at the compiler-mathlib interface). If the compilers should ever be modified to allow different float formats, these functions would have to be pushed into the math libraries.

The final step is the float library selection. The compilers will generate calls into the float package which will be defined at compile time using "defc" to point at a selected mathlib's implementation of those functions. A different prefix is attached to each mathlib's function name so that all math libs can coexist in a compiled library. From assembly language you would be able to call any math package and I may use that for testing to check computation correctness.



------------------------------------------------------------------------------
Dive into the World of Parallel Programming The Go Parallel Website, sponsored
by Intel and developed in partnership with Slashdot Media, is your hub for all
things parallel software development, from weekly thought leadership blogs to
news, videos, case studies, tutorials and more. Take a look and join the
conversation now. http://goparallel.sourceforge.net/
stefano
Well known member
Posts: 2137
Joined: Mon Jul 16, 2007 7:39 pm

Post by stefano »

>I know that Stef likes genmath (...)
:)

>I think Stef removed use of both index registers and it's down to just ix.
If I remember correctly I kept the few ZX81 fixes in separate files, but I don't think IY was much used in any case.



------------------------------------------------------------------------------
Dive into the World of Parallel Programming The Go Parallel Website, sponsored
by Intel and developed in partnership with Slashdot Media, is your hub for all
things parallel software development, from weekly thought leadership blogs to
news, videos, case studies, tutorials and more. Take a look and join the
conversation now. http://goparallel.sourceforge.net/
Post Reply