Skip to content
Snippets Groups Projects
Commit fda9dd22 authored by Fredrik Hübinette (Hubbe)'s avatar Fredrik Hübinette (Hubbe)
Browse files

sqrt optimized

Rev: src/ChangeLog:1.16
Rev: src/modules/math/math.c:1.2
Rev: src/modules/math/testsuite.in:1.2
parent afd91a0b
Branches
Tags
No related merge requests found
...@@ -6,6 +6,7 @@ Fri Nov 1 15:47:48 1996 Fredrik Hubinette <hubbe@tymin.blarg.com> ...@@ -6,6 +6,7 @@ Fri Nov 1 15:47:48 1996 Fredrik Hubinette <hubbe@tymin.blarg.com>
* some documentation about pike internals added * some documentation about pike internals added
* htmlify_docs.pike fixed to handle internal docs * htmlify_docs.pike fixed to handle internal docs
* mpz->powm fixed * mpz->powm fixed
* modules/math/math.c: sqrt optimized
Tue Oct 29 16:12:03 1996 Fredrik Hubinette <hubbe@tymin.blarg.com> Tue Oct 29 16:12:03 1996 Fredrik Hubinette <hubbe@tymin.blarg.com>
......
...@@ -68,14 +68,19 @@ void f_sqrt(INT32 args) ...@@ -68,14 +68,19 @@ void f_sqrt(INT32 args)
if(sp[-args].type==T_INT) if(sp[-args].type==T_INT)
{ {
INT32 a,b,c,q; unsigned INT32 n, b, s, y=0;
q=sp[-args].u.integer; unsigned INT16 x=0;
for(a=0,b=0x10000;a+1<b;)
n=sp[-args].u.integer;
for(b=1<<(sizeof(INT32)*8-2); b; b>>=2)
{
x<<=1; s=b+y; y>>=1;
if(n>=s)
{ {
c=(a+b)/2; x|=1; y|=b; n-=s;
if(c*c>q) b=c; else a=c; }
} }
sp[-args].u.integer=a; sp[-args].u.integer=x;
} }
else if(sp[-args].type==T_FLOAT) else if(sp[-args].type==T_FLOAT)
{ {
......
...@@ -26,7 +26,9 @@ test_true(cos(atan(1.0))<0.708) ...@@ -26,7 +26,9 @@ test_true(cos(atan(1.0))<0.708)
test_eq(4,sqrt(16)) test_eq(4,sqrt(16))
test_eq(4,sqrt(17)) test_eq(4,sqrt(17))
test_eq(4,sqrt(24)) test_eq(4,sqrt(24))
test_eq(sqrt(0x7fffffff),46340)
test_eq(4.0,sqrt(16.0)) test_eq(4.0,sqrt(16.0))
test_any([[int e,i; for(e=0;e<100000;e++) { i=sqrt(e); if(i*i>e || (i+1)*(i+1)<e) return e; } return -1;]],-1)
// - floor // - floor
test_eq(17.0,floor(17.0)) test_eq(17.0,floor(17.0))
test_eq(17.0,floor(17.1)) test_eq(17.0,floor(17.1))
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment