简体   繁体   中英

How to check code inside math library function sqrt() using Dev C++?

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

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;
    }
}

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

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM