programming the x87 for math.h etc.

Question about which tools to use, bugs, the best way to implement a function, etc should go here. Don't forget to see if your question is answered in the wiki first! When in doubt post here.
User avatar
NickJohnson
Member
Member
Posts: 1249
Joined: Tue Mar 24, 2009 8:11 pm
Location: Sunnyvale, California

programming the x87 for math.h etc.

Post by NickJohnson »

Well, I finally got FPU support working (thanks, Brendan), but because I'm writing my libc from scratch, I now have to write ton of math headers and functions. I don't know how the different floating point types are even represented, much less how to handle exceptions and how to define constants like NaN, +/-inf, etc. It seems like the answers are stretched somewhere between the IEEE 754 standard and some part of the Intel docs I haven't found (FPU instructions are listed, but not much beyond that, as far as I could find).

Where is a good place to start on this sort of stuff?
User avatar
Combuster
Member
Member
Posts: 9301
Joined: Wed Oct 18, 2006 3:45 am
Libera.chat IRC: [com]buster
Location: On the balcony, where I can actually keep 1½m distance
Contact:

Re: programming the x87 for math.h etc.

Post by Combuster »

To be able to implement math.h from scratch, you need university grade mathematics.

For the actual formats: 1. Intel, Chapter 8 Verse 2 and 1. Intel, Chapter 4 Verse 8 (the single and double precision ones are standard IEEE floats)
"Certainly avoid yourself. He is a newbie and might not realize it. You'll hate his code deeply a few years down the road." - Sortie
[ My OS ] [ VDisk/SFS ]
User avatar
NickJohnson
Member
Member
Posts: 1249
Joined: Tue Mar 24, 2009 8:11 pm
Location: Sunnyvale, California

Re: programming the x87 for math.h etc.

Post by NickJohnson »

Thanks - those seem like the things I was looking for. What kind of "university grade" mathematics are you talking about? It seems like any functions that aren't built in to the FPU would simply require a series approximation, which is just basic calculus. I just need functionality - nothing super-optimized.
User avatar
Owen
Member
Member
Posts: 1700
Joined: Fri Jun 13, 2008 3:21 pm
Location: Cambridge, United Kingdom
Contact:

Re: programming the x87 for math.h etc.

Post by Owen »

If you're implementing things like sin/cos using the FPU, you're doing it wrong. It is in fact faster to implement it using discrete instructions.
User avatar
NickJohnson
Member
Member
Posts: 1249
Joined: Tue Mar 24, 2009 8:11 pm
Location: Sunnyvale, California

Re: programming the x87 for math.h etc.

Post by NickJohnson »

That's interesting... then why does the FPU have so many functions like that (sin, cos, etc.)? Are they orders of magnitude slower, or is it okay to use the builtin functions to start?
User avatar
Combuster
Member
Member
Posts: 9301
Joined: Wed Oct 18, 2006 3:45 am
Libera.chat IRC: [com]buster
Location: On the balcony, where I can actually keep 1½m distance
Contact:

Re: programming the x87 for math.h etc.

Post by Combuster »

NickJohnson wrote:Thanks - those seem like the things I was looking for. What kind of "university grade" mathematics are you talking about? It seems like any functions that aren't built in to the FPU would simply require a series approximation, which is just basic calculus. I just need functionality - nothing super-optimized.
While Taylor-MacLaurin series work for sines and cosines and base-e exponents, and you can get away with Newton-Rhapson for square roots, things end about there. Taylor series have a domain where things work (which is not infinite, like with powers of e), or where large inputs require insane amount of expansion (large inputs to sin/cos) - it's not anything trivial. And beyond those functions, things get progressively worse.
"Certainly avoid yourself. He is a newbie and might not realize it. You'll hate his code deeply a few years down the road." - Sortie
[ My OS ] [ VDisk/SFS ]
jal
Member
Member
Posts: 1385
Joined: Wed Oct 31, 2007 9:09 am

Re: programming the x87 for math.h etc.

Post by jal »

NickJohnson wrote:It seems like any functions that aren't built in to the FPU would simply require a series approximation, which is just basic calculus.
Thank god I was never taught that "basic calculus" in hischool. I almost flunked my IT studies because of that ****...


JAL
User avatar
Solar
Member
Member
Posts: 7615
Joined: Thu Nov 16, 2006 12:01 pm
Location: Germany
Contact:

Re: programming the x87 for math.h etc.

Post by Solar »

I found P.J. Plaugher, "The C Standard Library", to be a must-have when you're working on implementing a C standard library.

Just a hint: <math.h> is tricky. Not only are you expected to get the results right within limits specified by the standard (which might or might not coincede with those of the FPU opcodes you used), you are also expected to handle INF and NaN values, denormalized values, underflows, and overflows correctly, which almost always rules out "trivial" solutions. You're also expected to have errno updated properly - which usually requires reading the FPU status, which greatly reduces the speed of your FPU when done frequently.

In the end, yes, there's a chance that an integer implementation of <math.h> will be more effective. Cody & Waite, "Software Manual for the Elementary Functions", is a very good guide to how this can be done. Not easily (or cheaply) obtained, though.

Oh, and looking at <fenv.h> will open another can of worms, as will <tgmath.h>.

This is only what I can tell after doing no more than brushing the surface of <math.h> myself. I wish you good luck, but don't be offended when I still hope I'll get PDCLib done first. ;-)
Every good solution is obvious once you've found it.
User avatar
Candy
Member
Member
Posts: 3882
Joined: Tue Oct 17, 2006 11:33 pm
Location: Eindhoven

Re: programming the x87 for math.h etc.

Post by Candy »

Solar wrote:Cody & Waite, "Software Manual for the Elementary Functions", is a very good guide to how this can be done. Not easily (or cheaply) obtained, though.
Didn't I send you one? If anybody wants a copy I still have my own copy of it. Problem with it is
1. Very unpopular (so no reprints)
2. Very old (so no second-hand bookstores have it anymore)
3. Collectors' value. If you do find one, it's between 2.5 and 15 times original price - which is absurdly expensive, considering that we're not really interested in collecting it but just the contents.

I did a cross-library borrow a few years back (university of Nijmegen has a copy) & made 2 copies.
User avatar
Solar
Member
Member
Posts: 7615
Joined: Thu Nov 16, 2006 12:01 pm
Location: Germany
Contact:

Re: programming the x87 for math.h etc.

Post by Solar »

Candy wrote:Didn't I send you one?
Indeed you did, thank you for that, and I'm actually looking forward at sharpening my skills at something I haven't done before. Once I get that thrice-damned <stdio.h> release done. 99% there, but currently I'm doing 50+ hours a week and ain't in the mood to continue coding in the evening or weekends after that.
Every good solution is obvious once you've found it.
User avatar
Combuster
Member
Member
Posts: 9301
Joined: Wed Oct 18, 2006 3:45 am
Libera.chat IRC: [com]buster
Location: On the balcony, where I can actually keep 1½m distance
Contact:

Re: programming the x87 for math.h etc.

Post by Combuster »

Candy wrote:
Solar wrote:Cody & Waite, "Software Manual for the Elementary Functions", is a very good guide to how this can be done. Not easily (or cheaply) obtained, though.
Didn't I send you one? If anybody wants a copy I still have my own copy of it. Problem with it is
1. Very unpopular (so no reprints)
2. Very old (so no second-hand bookstores have it anymore)
3. Collectors' value. If you do find one, it's between 2.5 and 15 times original price - which is absurdly expensive, considering that we're not really interested in collecting it but just the contents.

I did a cross-library borrow a few years back (university of Nijmegen has a copy) & made 2 copies.
Wow, amazon lists 375 US dollars for 288 pages. Even manually printing is more than 10x cheaper :shock:

I really ought to abuse copyright law here, borrow a copy (which is legally allowed), then make a copy for personal use (which is also legally allowed) and return the original :twisted:
"Certainly avoid yourself. He is a newbie and might not realize it. You'll hate his code deeply a few years down the road." - Sortie
[ My OS ] [ VDisk/SFS ]
User avatar
Owen
Member
Member
Posts: 1700
Joined: Fri Jun 13, 2008 3:21 pm
Location: Cambridge, United Kingdom
Contact:

Re: programming the x87 for math.h etc.

Post by Owen »

Solar wrote:
Candy wrote:Didn't I send you one?
Indeed you did, thank you for that, and I'm actually looking forward at sharpening my skills at something I haven't done before. Once I get that thrice-damned <stdio.h> release done. 99% there, but currently I'm doing 50+ hours a week and ain't in the mood to continue coding in the evening or weekends after that.
By the way - when you get to it, I could contribute the multibyte/wide character functions if you like. I think it would mostly break down like this:
  • wchar_t is 4 byte UCS-4, as required for strict compliance with the standard
  • PDCLib provides built-in support for conversions to/from ASCII, ISO/IEC 8859-1 (Latin-1), UTF-8, UTF-16, UCS-4 and CESU-8, which can be done algorithmically
  • A mechanism is provided for delegating support for other character sets to the operating system
(I suppose char16_t/char32_t/C1X support will be a post-1.0 thing?)
User avatar
Candy
Member
Member
Posts: 3882
Joined: Tue Oct 17, 2006 11:33 pm
Location: Eindhoven

Re: programming the x87 for math.h etc.

Post by Candy »

Combuster wrote:Wow, amazon lists 375 US dollars for 288 pages. Even manually printing is more than 10x cheaper :shock:
When I checked it was 150 dollars for the cheapest (which was visibly in a bad state) and about 800 for the most expensive. It's most likely gone up, as with so many "collector's items". We're not trying to "collect" it though.
I really ought to abuse copyright law here, borrow a copy (which is legally allowed), then make a copy for personal use (which is also legally allowed) and return the original :twisted:

I figured, since I want to use PDClib, that's personal use for me too :-). That's what I did by the way, it's just a big fat photocopy.
User avatar
Combuster
Member
Member
Posts: 9301
Joined: Wed Oct 18, 2006 3:45 am
Libera.chat IRC: [com]buster
Location: On the balcony, where I can actually keep 1½m distance
Contact:

Re: programming the x87 for math.h etc.

Post by Combuster »

By the way - when you get to it, I could contribute the multibyte/wide character functions if you like.
I happened to have tried that half a dozen times before, with no success, as Solar will probably explain shortly :wink:

As for the price reference, http://www.amazon.com/Elementary-Functi ... 0138220646 (Noticed a read error btw, Its actually 395)
"Certainly avoid yourself. He is a newbie and might not realize it. You'll hate his code deeply a few years down the road." - Sortie
[ My OS ] [ VDisk/SFS ]
User avatar
qw
Member
Member
Posts: 792
Joined: Mon Jan 26, 2009 2:48 am

Re: programming the x87 for math.h etc.

Post by qw »

NickJohnson wrote:Where is a good place to start on this sort of stuff?
I recommend the source code of DJGPP's C library. Two math libraries are included: one from D.J. Delorie, written in assembly, and one from Cygnus Support, written in C.
Post Reply