r/C_Programming • • 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

24 Upvotes

60 comments sorted by

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.

If it’s “modern”, then it’s IEEE 754, or close to it.

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.

has correct rounding, binary radix, infinities, and denormals

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.

It would also execute instructions in a stream mixed with non-FPU instructions, rather than a separate stream of floating-point instructions.

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).

By this sense, 1980 is the rough demarcation between modern FPU designs and older designs.

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:

  • normalised binary representation
  • correct rounding
  • binary radix
  • five species (addition, subtraction, multiplication, division, square root)
  • infinities and NaN trap, but cannot be represented (no denormals)
  • dedicated instruction stream fed in by punched tape
  • a two element register stack, similar to the 8087
  • 64 words of memory
  • microcoded pipelined design

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)

1

u/EpochVanquisher Aug 19 '26

I don’t want to see comments like this