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

1

u/EpochVanquisher Aug 18 '26

You have it backwards—people used to implement these directly in the ISA, back in the days of the x87 FPU and some others. Modern hardware mostly has basic arithmetic, and the functions in libm are software.

There are a couple exceptions (sqrt is often part of the ISA).

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.

→ More replies

3

u/mikeblas Aug 18 '26

Really? The Intel processors got rid of all their higher level floating point opcodes?

3

u/EpochVanquisher Aug 18 '26

They’re still there for backwards compatibility, but modern compilers don’t use them unless you go out of your way to make the compiler use them.

1

u/mikeblas Aug 18 '26

Interesting. Why did it shift?

4

u/EpochVanquisher Aug 18 '26

In general these functions are a good fit for software. There are a bunch of steps: range reduction, polynomial or rational approximation, table lookups, handling for exceptional cases. When you have an algorithm with all these steps to it, software is probably your first and best choice (most of the time).

If you implement them in hardware, maybe it will be microcoded, which basically means it’s still software, but the software is running on the microcode sequencer in the CPU. And then you have to dedicate a bunch of silicon to that, and you are stuck to a single implementation.

Software is really good.

The difference with sqrt is that it happens to have a really nice hardware implementation. That implementation is a nice low-latency circuit which gives the correctly rounded result.

1

u/max123246 Aug 19 '26

Cisc was a mistake lol

1

u/EpochVanquisher Aug 19 '26

I don’t think that’s a good way to put it.

Designers never chose to make CISC processors. But around 1980, we got a bunch of design philosophies about CPUs, called the new stuff “RISC”, and retroactively called the old way “CISC”.

2

u/DawnOnTheEdge Aug 18 '26 edited Aug 18 '26

A major reason is that all real-world numerical computation wants to work on arrays of numbers, and the 8087 stack-based instruction set would not work with SIMD.

1

u/mikeblas Aug 18 '26

Yep, that's a compelling argument.

1

u/FUZxxl Aug 18 '26

The transcendental instructions are somewhat slow and of varying quality depending on FPU manufacturer. If you do it in software, the result is consistent across FPUs and usually faster.