Page 1 of 2

programming the x87 for math.h etc.

Posted: Mon Aug 16, 2010 1:24 pm
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?

Re: programming the x87 for math.h etc.

Posted: Mon Aug 16, 2010 2:24 pm
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)

Re: programming the x87 for math.h etc.

Posted: Mon Aug 16, 2010 3:04 pm
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.

Re: programming the x87 for math.h etc.

Posted: Mon Aug 16, 2010 6:09 pm
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.

Re: programming the x87 for math.h etc.

Posted: Mon Aug 16, 2010 7:38 pm
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?

Re: programming the x87 for math.h etc.

Posted: Mon Aug 16, 2010 11:14 pm
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.

Re: programming the x87 for math.h etc.

Posted: Tue Aug 17, 2010 4:23 am
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

Re: programming the x87 for math.h etc.

Posted: Tue Aug 17, 2010 5:08 am
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. ;-)

Re: programming the x87 for math.h etc.

Posted: Tue Aug 17, 2010 8:29 am
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.

Re: programming the x87 for math.h etc.

Posted: Tue Aug 17, 2010 8:33 am
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.

Re: programming the x87 for math.h etc.

Posted: Tue Aug 17, 2010 10:27 am
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:

Re: programming the x87 for math.h etc.

Posted: Tue Aug 17, 2010 10:39 am
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?)

Re: programming the x87 for math.h etc.

Posted: Tue Aug 17, 2010 10:43 am
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.

Re: programming the x87 for math.h etc.

Posted: Tue Aug 17, 2010 11:19 am
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)

Re: programming the x87 for math.h etc.

Posted: Wed Aug 18, 2010 3:29 am
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.