Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

looking for implementation of sqrt

Status
Not open for further replies.
May 16, 2007
4
GB
Hi,

Probably a silly question, but where do i find function implementations (for stdlib functions). To be precise I'm looking for the implementation of sqrt. I had a look in /usr/include and found math.h but it does not contain an implementation.

The cmath library contains the below;
sqrt( __x)
{ return __builtin_sqrt(__x); }
But does anybody know where do I find the code that satisfies __builtin_sqrt?

Thanks for your help in advance.
/--kjs

 
Will any implementation for any OS do or are you looking for a specific one?
 
I'm looking for any OS implementation, pref Unix or Linux but anything will do.

Googling sqrt.c gave me a few versions of sqrt that various people wrote. Most of them don't even compile using gcc.

But I'm looking for the implementation the standard library uses. Or something very close to that. I couldn't find that on Google.

Any advice/help will be appreciated.

Thanks
kjs
 
What is it:for the implementation the standard library uses? The language standard does not define standard implementations; it defines standard specifications of math functions (sqrt, for example).

The language implementation has its own sqrt (for example, it uses FPU instruction fsqrt (as VC++), or calculates sqrt via some kind polynomial approximation+more precise iterative process, or uses more exotic fixed point arith approach for special embedded platforms).

It's waste of time to search canonical sqrt (or any other math function)implementation. Of course, one day you may invent the best (all over the Word) implementation (then next day someone will invent much more better sqrt and so on).
 
Are you looking for the code for a reason or just to see how it's done?
 
I 'm looking for it for a reason.

Basically I'm trying to implement a program in C that is meant to run on an embedded device with an MSP430 micro-controller (10KB RAM, 48KB Flash). I don't really want to include the whole cmath library -- I only need the sqrt. Since resources are scarce, every byte counts.

Thanks for your help
 
Code:
#include <stdio.h>
#include <stdlib.h>
#include <assert.h>
#include <errno.h>

double
mYsqrt(arg)
double arg;
{
	double x, temp;
	int exp;
	int i;

	if(arg <= 0.) {
		if(arg < 0.)
			errno = EDOM;
		return(0.);
	}
	x = frexp(arg,&exp);
	while(x < 0.5) {
		x *= 2;
		exp--;
	}
	/*
	 * NOTE
	 * this wont work on 1's comp
	 */
	if(exp & 1) {
		x *= 2;
		exp--;
	}
	temp = 0.5*(1.0+x);

	while(exp > 60) {
		temp *= (1L<<30);
		exp -= 60;
	}
	while(exp < -60) {
		temp /= (1L<<30);
		exp += 60;
	}
	if(exp >= 0)
		temp *= 1L << (exp/2);
	else
		temp /= 1L << (-exp/2);
	for(i=0; i<=4; i++)
		temp = 0.5*(temp + arg/temp);
	return(temp);
}

int main ( ) {
    double a = mYsqrt(1234.56);
    double b = sqrt(1234.56);
    printf( "%f %f\n", a, b );
    return 0;
}
Code found here with very little effort.

I only changed the name to allow comparison with the built-in version.
[tt]$ gcc foo.c -lm
$ ./a.exe
35.136306 35.136306[/tt]

If you want to make it ANSI-C, then change
[tt]double
sqrt(arg)
double arg;
[/tt]
Into
[tt]
double sqrt( double arg )
[/tt]


> Most of them don't even compile using gcc.
Posting actual code, references and error messages would help.


--
If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top