Just curious how are these implemented. I can’t see where I would start. Do they work directly on the float‘s/double‘s bits?
Also where can I find the source code of functions from math.h? All I find are either headers with prototypes or files with functions that call other functions from somewhere else.
EDIT: Part of the message was lost after editing the title. What I meant in particular were the ceil() and floor() functions.
If you’re interested in seeing source code for algorithms for this kind of thing, then fdlibm – the “Freely Distributable
libm“, originally from Sun, and the reference implementation for Java’s math libraries – might be a good place to start. (For casual browsing, it’s certainly a better place to start than GNU libc, where the pieces are scattered around various subdirectories –math/,sysdeps/ieee754/, etc.)fdlibm assumes that it’s working with an IEEE 754 format
double, and if you look at the implementations – for example, the core of the implementation of log() – you’ll see that they use all sorts of clever tricks, often using a mixture of both standarddoublearithmetic, and knowledge of the bit representation of adouble.(And if you’re interested in algorithms for supporting basic IEEE 754 floating point arithmetic, such as might be used for processors without hardware floating point support, take a look at John R. Hauser’s SoftFloat.)
As for your edit: in general,
ceil()andfloor()might well be implemented in hardware; for example, on x86, GCC (with optimisations enabled) generates code using thefrndintinstruction with appropriate fiddling of the FPU control word to set the rounding mode. But fdlibm’s pure software implementations (s_ceil.c,s_floor.c) do work using the bit representation directly.