r/C_Programming • u/New_Telephone926 • Aug 18 '26
Question How are functions in libm actually implemented ?
I'm trying to gather information for a school project about how elementary functions (exp, log, cos, sin, etc.) are implemented on computers, for maximum accuracy and efficiency.
I've already found some information about techniques that can be used to implement these functions, such as range reduction, Taylor series, polynomial approximations or minimax approximations. However, most of what I've found remains fairly theoretical, and I have no idea of what is actually used in the real world.
I tried to reverse-engineer the exp implementation in libm, but my knowledge of C and its intricacies is still fairly limited (though I'm working on improving it).
I'm particularly interested in understanding which approximation methods are actually used, how polynomial coefficients are chosen (Taylor, minimax/Remez, or other), and how implementations balance speed and accuracy.
But really, if you have any information whatsoever, it would be great.
If anyone has documentation, papers, or personal experience with this kind of subject, it would be really helpful.
Thanks in advance!
Edit : Thanks a lot to everyone ! You gave me exactly what I was looking for
View all comments
Show parent comment
0
u/FUZxxl Aug 19 '26 edited Aug 19 '26
I don't think your definitions are particularly useful. You basically define things such that everything that is like an 8087 is an FPU and everything else is not, despite the 8087 being a bit of an outlier as far as FPU designs are concerned. And even if we call an 8087-like design “modern,” you are drawing the line at something that is barely a microarchitectural difference from “pre-modern” FPUs. Maybe it would be useful to call FPUs “modern” if the basic operations are no longer microcoded and they are integrated with a CPU into one die.
This disregards all FPUs made before the 8086, of which there are a lot. IEEE 754 is the 8087's floating point representation. Of course everything that came before was different in various ways.
Correct rounding they usually have, binary radix not necessarily (e.g. S/360 has decimal/hexadecimal floats), infinities you usually do get, denormals were basically not a thing before the 8087 (I do not know of any older FPU supporting them). So again, this excludes almost all FPUs that came before the 8087.
That excludes many popular FPUs, including the Weitek FPUs (memory mapped), ARM's early FPUs (receive a dedicated instruction stream from the CPU through special coprocessor instructions), and makes the 8087 an FPU or not depending on how its used (non-8086 systems frequently hooked it up in weird ways, all of which have a separate instruction stream for the FPU). In particular this also excludes the 68881 you mentioned earlier, as it receives its own dedicated instruction stream (usually from the CPU, but you can hook it up however you like).
I seriously doubt that. The only thing that changed at that point was that most of the proprietary floating point formats went away in favour of IEEE 754 and denormal support was (often begrudgingly) added. The microarchitecture and systems integration stayed the same.
Curiously, in most ways the Z3 is an FPU:
If you look at an architectural block diagram, it looks basically the same as other FPUs up until VLSI really picked up (so maybe ~1985)