1

I want to check code inside math library function sqrt() how is it possible?
I am using DEV C++ .

6

3 Answers 3

7

This stuff gets compiled into the toolchain runtime, but since GCC and its Windows port MinGW (which is what your Dev-C++ IDE invokes) are open-source, you can just take a look at the source.

Here it is for latest MinGW GCC; both versions appear to defer basically all of the work to the processor (which is not a great surprise, seeing as x86 — by way of the x87 part of the instruction set — supports square root calculations natively).

long double values

#include <math.h>
#include <errno.h>

extern long double  __QNANL;

long double
sqrtl (long double x)
{
  if (x < 0.0L )
    {
      errno = EDOM;
      return __QNANL;
    }
  else
    {
      long double res;
      asm ("fsqrt" : "=t" (res) : "0" (x));
      return res;
    }
}

float values

#include <math.h>
#include <errno.h>

extern float  __QNANF;

float
sqrtf (float x)
{
  if (x < 0.0F )
    {
      errno = EDOM;
      return __QNANF;
    }
  else
    {
      float res;
      asm ("fsqrt" : "=t" (res) : "0" (x));
      return res;
    }
}
0
1

Square roots are calculated by the floating point unit of the processor so there is not much C++ to learn there...

EDIT:

x86 instructions

http://en.wikipedia.org/wiki/X86_instruction_listings

http://en.wikipedia.org/wiki/X87

FSQRT - Square root

Even back in the day: en.wikipedia.org/wiki/8087

7
  • Now that's shocking news to me... Any reference to back up your brave claim? Mar 5, 2012 at 19:39
  • 1
    @JohanLundberg: Has that chip been used since the 80s? en.wikipedia.org/wiki/X87 is probably a better link. Mar 5, 2012 at 19:43
  • @LightnessRacesinOrbit All right. I was just trying to prove that it is not news. ;-) Mar 5, 2012 at 19:46
  • 1
    @karthikgorijavolu: I guess you're not paying attention :(] Mar 5, 2012 at 20:12
  • 3
    @LightnessRacesinOrbit now understood very well thanks for help Mar 5, 2012 at 20:41
0

If there's no source code for your sqrt(), you can always disassemble it. Inspecting the code would be one type of checking.

You can also write a test for sqrt(). That would be the other type of checking.

7
  • What sort of "test" would that be? Mar 5, 2012 at 20:12
  • @LightnessRacesinOrbit: There are many tests you can do: negative arguments, +/- 0, small positive arguments (normalized and denormalized), large positive arguments, exact squares, +/- infinity, NaNs, see that it's indeed calculating square roots (using predefined values or an alternative sqrt() function), see that it's accurate, see that it's monotonic, etc. You can throw in performance testing too. Is that good enough for a start? Mar 5, 2012 at 20:27
  • I suppose, though it'd be a rather haphazard way of reverse-engineering the code behind your implementation's sqrt. Mar 5, 2012 at 20:39
  • @LightnessRacesinOrbit: I didn't mean testing as a way of reverse-engineering, though it may reveal certain innards. Mar 5, 2012 at 20:44
  • What did you mean it as, then? Mar 5, 2012 at 20:47

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.