programming the x87 for math.h etc.
- NickJohnson
- Member
- Posts: 1249
- Joined: Tue Mar 24, 2009 8:11 pm
- Location: Sunnyvale, California
programming the x87 for math.h etc.
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?
Where is a good place to start on this sort of stuff?
- Combuster
- 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.
- NickJohnson
- Member
- Posts: 1249
- Joined: Tue Mar 24, 2009 8:11 pm
- Location: Sunnyvale, California
Re: programming the x87 for math.h etc.
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.
- Owen
- Member
- Posts: 1700
- Joined: Fri Jun 13, 2008 3:21 pm
- Location: Cambridge, United Kingdom
- Contact:
Re: programming the x87 for math.h etc.
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.
- NickJohnson
- Member
- Posts: 1249
- Joined: Tue Mar 24, 2009 8:11 pm
- Location: Sunnyvale, California
Re: programming the x87 for math.h etc.
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?
- Combuster
- 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.
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.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.
Re: programming the x87 for math.h etc.
Thank god I was never taught that "basic calculus" in hischool. I almost flunked my IT studies because of that ****...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.
JAL
Re: programming the x87 for math.h etc.
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.
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.
Re: programming the x87 for math.h etc.
Didn't I send you one? If anybody wants a copy I still have my own copy of it. Problem with it isSolar 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.
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.
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.Candy wrote:Didn't I send you one?
Every good solution is obvious once you've found it.
- Combuster
- 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.
Wow, amazon lists 375 US dollars for 288 pages. Even manually printing is more than 10x cheaperCandy wrote:Didn't I send you one? If anybody wants a copy I still have my own copy of it. Problem with it isSolar 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.
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.
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
- Owen
- Member
- Posts: 1700
- Joined: Fri Jun 13, 2008 3:21 pm
- Location: Cambridge, United Kingdom
- Contact:
Re: programming the x87 for math.h etc.
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:Solar wrote: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.Candy wrote:Didn't I send you one?
- 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
Re: programming the x87 for math.h etc.
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.Combuster wrote:Wow, amazon lists 375 US dollars for 288 pages. Even manually printing is more than 10x cheaper
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
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.
- Combuster
- 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.
I happened to have tried that half a dozen times before, with no success, as Solar will probably explain shortlyBy the way - when you get to it, I could contribute the multibyte/wide character functions if you like.
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.
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.NickJohnson wrote:Where is a good place to start on this sort of stuff?