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

23 Upvotes

60 comments sorted by

View all comments

Show parent comment

4

u/FUZxxl Aug 18 '26

The x87 FPU was a big exception. Most floating point units back then and today did not implement transcendental operations. Typically they could only do addition, subtraction, multiplication, division, and square roots, and usually comparisons and such.

2

u/EpochVanquisher Aug 18 '26

I was thinking of the 68881 and the x87, which appeared around the same time, and then people decided that transcendental functions probably don’t belong in the FPU.

2

u/FUZxxl Aug 18 '26

You were thinking of two of the three FPUs with transcendental function support I am aware of. The very slow Am9511 is another one.

2

u/EpochVanquisher Aug 18 '26

Yeah, two extremely popular models, from the early history of the FPU. More modern stuff dropped the idea of these complicated instructions being implemented in hardware.

Earlier floating-point hardware (prior to the 8087) I think is not really recognizable as an FPU. Depends on where you draw the line, though.

Part of a broader trend of moving complicated or sequenced operations to software, like the MIPS R2010, which looks a lot more like a modern FPU, in a lot of ways.

2

u/FUZxxl Aug 18 '26

The first FPU was built by Konrad Zuse in 1942 (Zuse Z3). It could do addition, subtraction, multiplication, division, square-root, and conversion from and to decimal on normalised binary floating point numbers. It had support for trapping on overflow, division by zero, and square-root of negative numbers.

Subsequently many different floating point units were built, slowly converging on a normalised binary representation. Kahan than introduced a fast way to do denormal numbers in the 8087 and agreed to share how its done with the competition in exchange for that becoming the standard. Many of the earlier FPUs are otherwise very similar to modern ones, just using a different floating-point representation. For example, S/360 famously used decimal or hexadecimal floating-point.

2

u/EpochVanquisher Aug 18 '26

Sure; if you focus on a different part of history you can get a different story.

I would never call the Zuse Z3 an FPU, that choice seems pretty odd to me. Did it have an FPU? I don’t think I would describe it that way. It certainly wasn’t like modern FPUs.

1

u/FUZxxl Aug 19 '26

The entire thing is an FPU and it's extremely similar to a modern FPU.

2

u/EpochVanquisher Aug 19 '26

Sure, and if you understand that I don’t call the Zuse Z3 an FPU, and I don’t think it’s modern, then you can figure out what my comments are getting at.

0

u/FUZxxl Aug 19 '26

Ok. What makes you think it's not an FPU? What makes you think it's not modern? I would like to understand where you draw the distinction between FPU and non-FPU and modern/non-modern FPU so I can understand your point better.

1

u/EpochVanquisher Aug 19 '26

In my mind, if it’s an FPU, it’s a part of a larger system, like how a CPU or an ALU are.

If it’s “modern”, then it’s IEEE 754, or close to it. If you want to strip away the IEEE 754 requirement then I’d say a “modern” design has correct rounding, binary radix, infinities, and denormals, at the very least. It would also execute instructions in a stream mixed with non-FPU instructions, rather than a separate stream of floating-point instructions.

By this sense, 1980 is the rough demarcation between modern FPU designs and older designs. By coincidence, this coincides with the design trend to limit what processors do per instruction, so the newer FPU designs don’t have transcendental functions, but some old ones do.

→ More replies