Author Topic: [z80] Floating Point Routines  (Read 51695 times)

0 Members and 4 Guests are viewing this topic.

Offline Xeda112358

  • they/them
  • Moderator
  • LV12 Extreme Poster (Next: 5000)
  • ************
  • Posts: 4704
  • Rating: +719/-6
  • Calc-u-lator, do doo doo do do do.
    • View Profile
[z80] Floating Point Routines
« on: August 13, 2013, 12:09:18 pm »
EDIT: I'm still not good with GitHub, so I'm going to try to get this to work, but I make no promises. z80float


Hopefully, this can evolve into a collection of floating point routines, with this first post edited to keep them in one easy place. I am working on some floating point routines for use in things like Grammer and I hope it will some day make its way into a community made OS, so I hope that people find these useful enough to use (and feel free to do so). I am still working on these routines, trying to optimise them, too.

edit: I removed the code because it was too much. Instead, I have made a public folder available here to keep these organised.

Tari, from Cemetech posted a link to some routines received from Scott Dial for BCD format (the same as the TI-OS). They can be found here.

Examples of use might be:
Code: [Select]
   ld hl,FP1
   ld de,FP2
   call FloatDiv
   <<code>>
FP1:
  .db 0,2,  $c9,$0f,$da,$a2,$21,$68,$c2,$34    ;pi, not rounded up
FP2:
  .db 0,2,  $ad,$f8,$54,$58,$a2,$bb,$4a,$9A    ;e, not rounded up
The 80-bit division and multiplication routines do not handle overflow yet. On the to-do list:

  • 80-bit Addition
  • 80-bit Subtraction
  • 80-bit Exponents
  • 80-bit Logarithms
  • 80-bit sin/cos/tan
  • 80-bit arcsin/arccos/arctan
  • 80-bit Complex Addition, Subtraction, Multiplication, Division, Exponents, Logs, Trig
  • BCD Division
  • BCD Exponents
  • BCD Logarithms
  • BCD sin/cos/tan
  • BCD arcsin/arccos/arctan
  • BCD Complex Addition, Subtraction, Multiplication, Division, Exponents, Logs, Trig



From these, routines such as those dealing with probability, statistics, and other similar routines should be easier to make. The advantage to using BCD is for displaying numbers or reading them during parsing. With the 80-bit format, most non-integers don't have an easy representation (like .1) and in order to parse a base-10 string of digits, you would have to do some very time consuming processes (probably a hundred times slower than with BCD). However, for compiled languages and where text output isn't necessary, 80-bit floats offer a much higher range of numbers, greater accuracy, and all for a decent speed.

Offline Xeda112358

  • they/them
  • Moderator
  • LV12 Extreme Poster (Next: 5000)
  • ************
  • Posts: 4704
  • Rating: +719/-6
  • Calc-u-lator, do doo doo do do do.
    • View Profile
Re: [z80] Floating Point Routines
« Reply #1 on: August 14, 2013, 12:13:29 pm »
I added the 80-bit Floating Point Addition routine which was friendlier than I expected. Also, for current timings, Division is a little under 60 000 t-states, multiplication is a little over 30 000 t-states, and addition is just under 1400 t-states. I need to make all of these faster x.x

EDIT: Oh, also, The floating point addition code includes a routine for setting a number to infinity, keeping its sign, among other things. It also has rounding implemented.

EDIT: In my test app, here are the sizes so far:
FloatAdd : 244 bytes (this includes the code for handling infinity and a few other subroutines that will be shared by others)
FloatDiv : 244 bytes
FloatMul: 205 (this includes a subroutine routine for 64-bit integer multiplication)

Offline Xeda112358

  • they/them
  • Moderator
  • LV12 Extreme Poster (Next: 5000)
  • ************
  • Posts: 4704
  • Rating: +719/-6
  • Calc-u-lator, do doo doo do do do.
    • View Profile
Re: [z80] Floating Point Routines
« Reply #2 on: August 16, 2013, 04:28:26 pm »
I modified the addition routine (I rewrote it from scratch) and it now handles adding negative numbers and whatnot. This means the subtraction routine only needed to toggle the sign of the second input, so multiplication, division, addition, and subtraction are working :)

I also put the code here because the first post was getting too cluttered for my liking. If you check it out, you will notice the yet-to-be-tested code for the CORDIC algorithm that should compute sine and cosine at the same time.

Offline Xeda112358

  • they/them
  • Moderator
  • LV12 Extreme Poster (Next: 5000)
  • ************
  • Posts: 4704
  • Rating: +719/-6
  • Calc-u-lator, do doo doo do do do.
    • View Profile
Re: [z80] Floating Point Routines
« Reply #3 on: August 18, 2013, 11:45:16 pm »
I added a few 24-bit floating point routines including a log2 routine. The advantage to the 24-bit floats is that it can all be done in the registers, giving them even better speed. The obvious downside is a much more limited range for accuracy (about 4 to 5 decimal digits), but it does provide a fast alternative to integer math. It could be used in languages like Axe or Grammer.

I haven't fully tested the log2 routine, but I did test a few key values that were more likely to have issues with accuracy. I am pleased to say, that the worst I got was about .0000763 away from the actual value (but I won't be surprised if there are values that yield as much as an error of 5 times that). The main issue is that it doesn't handle negative inputs.

By the way, for timing, the log2 routine is around 1200 to 3500 t-states, so it can do 1700 to 5000 computations per second at 6MHz. Graphing a log() equation should be pretty fast ;D

Offline Eiyeron

  • Urist McEiyolobster
  • LV10 31337 u53r (Next: 2000)
  • **********
  • Posts: 1430
  • Rating: +130/-10
  • (-_(//));
    • View Profile
    • Rétro-Actif : Rétro/Prog/Blog
Re: [z80] Floating Point Routines
« Reply #4 on: August 19, 2013, 05:24:08 am »
That's neat. i won't certainly use them, but keep up the good work!

Offline Xeda112358

  • they/them
  • Moderator
  • LV12 Extreme Poster (Next: 5000)
  • ************
  • Posts: 4704
  • Rating: +719/-6
  • Calc-u-lator, do doo doo do do do.
    • View Profile
Re: [z80] Floating Point Routines
« Reply #5 on: October 21, 2013, 08:11:39 am »
I updated the 80-bit floating point multiplication routine. It is a bit larger now, but I used a modified approach from the last version and it is now much faster. From ~32000 t-states, I brought it down to ~24000 2 days ago, and last night, I brought it down to <16000 t-states. Now it is more accurate and provides a wider range of numbers and it is faster than the OS. Division on the other hand is still pretty slow (the OS does it faster).

As well, I have a squareroot routine for the 24-bit floats that works, but I want to make it faster. I haven't added that to the routines list yet.

Offline Matrefeytontias

  • Axe roxxor (kinda)
  • LV10 31337 u53r (Next: 2000)
  • **********
  • Posts: 1982
  • Rating: +310/-12
  • Axe roxxor
    • View Profile
    • RMV Pixel Engineers
Re: [z80] Floating Point Routines
« Reply #6 on: October 21, 2013, 08:58:57 am »
You could pretty much write an OS out of that.

Offline Sorunome

  • Fox Fox Fox Fox Fox Fox Fox!
  • Support Staff
  • LV13 Extreme Addict (Next: 9001)
  • *************
  • Posts: 7920
  • Rating: +374/-13
  • Derpy Hooves
    • View Profile
    • My website! (You might lose the game)
Re: [z80] Floating Point Routines
« Reply #7 on: October 21, 2013, 09:15:50 am »
* Sorunome waits on xeda to write a custom OS which tops all of TI, even has mathprint and supports the normal basic progs - all only far better :P

THE GAME
Also, check out my website
If OmnomIRC is screwed up, blame me!
Click here to give me an internet!

Offline Matrefeytontias

  • Axe roxxor (kinda)
  • LV10 31337 u53r (Next: 2000)
  • **********
  • Posts: 1982
  • Rating: +310/-12
  • Axe roxxor
    • View Profile
    • RMV Pixel Engineers
Re: [z80] Floating Point Routines
« Reply #8 on: October 21, 2013, 11:35:44 am »
*cough* *cough* Join KnightOS dev team *cough* *cough* add float routines *cough* *cough* write a homescreen app *cough*

Offline DJ Omnimaga

  • Clacualters are teh gr33t
  • CoT Emeritus
  • LV15 Omnimagician (Next: --)
  • *
  • Posts: 55941
  • Rating: +3154/-232
  • CodeWalrus founder & retired Omnimaga founder
    • View Profile
    • Dream of Omnimaga Music
Re: Re: [z80] Floating Point Routines
« Reply #9 on: October 21, 2013, 12:18:19 pm »
Given what SirCmpwn did to AssemblyBandit when he tried to contribute to KnightOS CSE, I would be careful about contributing for it.
Dream of Omnimaga

Offline Xeda112358

  • they/them
  • Moderator
  • LV12 Extreme Poster (Next: 5000)
  • ************
  • Posts: 4704
  • Rating: +719/-6
  • Calc-u-lator, do doo doo do do do.
    • View Profile
Re: [z80] Floating Point Routines
« Reply #10 on: October 21, 2013, 03:04:15 pm »
Yeah, I dunno about joining that team, but you are free to use any of these routines. They haven't yet been rigorously optimised (obviously, if I managed to make a routine twice as fast there is huge room for improvement). My hope was that these would actually be used for an OS, whether it is an OS I write or an OS others write. I also planned to use these routines if I ever finish Grammer 3, or Grammer 4 (an OS).

I might make a concept programming language using these 80-bit and 24-bit floats, or I might incorporate them into FileSyst somehow.

Offline Sorunome

  • Fox Fox Fox Fox Fox Fox Fox!
  • Support Staff
  • LV13 Extreme Addict (Next: 9001)
  • *************
  • Posts: 7920
  • Rating: +374/-13
  • Derpy Hooves
    • View Profile
    • My website! (You might lose the game)
Re: [z80] Floating Point Routines
« Reply #11 on: October 21, 2013, 03:08:48 pm »
Given what SirCmpwn did to AssemblyBandit when he tried to contribute to KnightOS CSE, I would be careful about contributing for it.
What happend to AssemblyBandit O.O

THE GAME
Also, check out my website
If OmnomIRC is screwed up, blame me!
Click here to give me an internet!

Offline DJ Omnimaga

  • Clacualters are teh gr33t
  • CoT Emeritus
  • LV15 Omnimagician (Next: --)
  • *
  • Posts: 55941
  • Rating: +3154/-232
  • CodeWalrus founder & retired Omnimaga founder
    • View Profile
    • Dream of Omnimaga Music
Re: Re: [z80] Floating Point Routines
« Reply #12 on: October 22, 2013, 02:58:58 pm »
He was falsely accused of stealing KnightOS code after he provided sircmpwn with a fixed version of the 84+CSE remake. Sir needed help to get KOS to run on the color calc, hence why AsmBandit wanted to help.
Dream of Omnimaga

Offline Streetwalrus

  • LV12 Extreme Poster (Next: 5000)
  • ************
  • Posts: 3821
  • Rating: +80/-8
    • View Profile
Re: [z80] Floating Point Routines
« Reply #13 on: October 22, 2013, 03:15:48 pm »
Wtf ? O.O I he thanked him though at least. Also with an open source project stealing would be claiming the work as yours and not releasing the source. Knowing AsmBandit, this couldn't happen.

Offline DJ Omnimaga

  • Clacualters are teh gr33t
  • CoT Emeritus
  • LV15 Omnimagician (Next: --)
  • *
  • Posts: 55941
  • Rating: +3154/-232
  • CodeWalrus founder & retired Omnimaga founder
    • View Profile
    • Dream of Omnimaga Music
Re: Re: [z80] Floating Point Routines
« Reply #14 on: October 22, 2013, 07:31:00 pm »
True. He just published a fixed copy on Sir's repository anyway. Oh well
Dream of Omnimaga