version 1.9, 2015/08/06 10:01:52 |
version 1.12, 2018/03/29 01:32:52 |
|
|
* DEVELOPER SHALL HAVE NO LIABILITY IN CONNECTION WITH THE USE, |
* DEVELOPER SHALL HAVE NO LIABILITY IN CONNECTION WITH THE USE, |
* PERFORMANCE OR NON-PERFORMANCE OF THE SOFTWARE. |
* PERFORMANCE OR NON-PERFORMANCE OF THE SOFTWARE. |
* |
* |
* $OpenXM: OpenXM_contrib2/asir2000/engine/real.c,v 1.8 2003/12/24 08:00:38 noro Exp $ |
* $OpenXM: OpenXM_contrib2/asir2000/engine/real.c,v 1.11 2016/06/29 08:16:11 ohara Exp $ |
*/ |
*/ |
#include "ca.h" |
#include "ca.h" |
#include "base.h" |
#include "base.h" |
|
|
double RatnToReal(a) |
double RatnToReal(a) |
Q a; |
Q a; |
{ |
{ |
double nm,dn,man; |
double nm,dn,man; |
int enm,edn,e; |
int enm,edn,e; |
|
|
nm = NatToReal(NM(a),&enm); |
nm = NatToReal(NM(a),&enm); |
if ( INT(a) ) |
if ( INT(a) ) |
if ( enm >= 1 ) { |
if ( enm >= 1 ) { |
error("RatnToReal : Overflow"); |
error("RatnToReal : Overflow"); |
/* NOTREACHED */ |
/* NOTREACHED */ |
return 0; |
return 0; |
} else |
} else |
return SGN(a)>0 ? nm : -nm; |
return SGN(a)>0 ? nm : -nm; |
else { |
else { |
dn = NatToReal(DN(a),&edn); |
dn = NatToReal(DN(a),&edn); |
man = nm/dn; |
man = nm/dn; |
if ( SGN(a) < 0 ) |
if ( SGN(a) < 0 ) |
man = -man; |
man = -man; |
if ( ((e = enm - edn) >= 1024) || (e <= -1023) ) { |
if ( ((e = enm - edn) >= 1024) || (e <= -1023) ) { |
error("RatnToReal : Overflow"); /* XXX */ |
error("RatnToReal : Overflow"); /* XXX */ |
/* NOTREACHED */ |
/* NOTREACHED */ |
return 0; |
return 0; |
} else if ( !e ) |
} else if ( !e ) |
return man; |
return man; |
else |
else |
return man*pow(2,e); |
return man*pow(2,e); |
} |
} |
} |
} |
|
|
double NatToReal(a,expo) |
double NatToReal(a,expo) |
N a; |
N a; |
int *expo; |
int *expo; |
{ |
{ |
unsigned int *p; |
unsigned int *p; |
int l,all,i,j,s,tb,top,tail; |
int l,all,i,j,s,tb,top,tail; |
unsigned int t,m[2]; |
unsigned int t,m[2]; |
|
|
p = BD(a); l = PL(a) - 1; |
p = BD(a); l = PL(a) - 1; |
for ( top = 0, t = p[l]; t; t >>= 1, top++ ); |
for ( top = 0, t = p[l]; t; t >>= 1, top++ ); |
all = top + BSH*l; tail = (53-top)%BSH; i = l-(53-top)/BSH-1; |
all = top + BSH*l; tail = (53-top)%BSH; i = l-(53-top)/BSH-1; |
m[1] = i < 0 ? 0 : p[i]>>(BSH-tail); |
m[1] = i < 0 ? 0 : p[i]>>(BSH-tail); |
for ( j = 1, i++, tb = tail; i <= l; i++ ) { |
for ( j = 1, i++, tb = tail; i <= l; i++ ) { |
s = 32-tb; t = i < 0 ? 0 : p[i]; |
s = 32-tb; t = i < 0 ? 0 : p[i]; |
if ( BSH > s ) { |
if ( BSH > s ) { |
m[j] |= ((t&((1<<s)-1))<<tb); |
m[j] |= ((t&((1<<s)-1))<<tb); |
if ( !j ) |
if ( !j ) |
break; |
break; |
else { |
else { |
j--; m[j] = t>>s; tb = BSH-s; |
j--; m[j] = t>>s; tb = BSH-s; |
} |
} |
} else { |
} else { |
m[j] |= (t<<tb); tb += BSH; |
m[j] |= (t<<tb); tb += BSH; |
} |
} |
} |
} |
s = (all-1)+1023; |
s = (all-1)+1023; |
m[0] = (m[0]&((1<<20)-1))|(MIN(2046,s)<<20); *expo = MAX(s-2046,0); |
m[0] = (m[0]&((1<<20)-1))|(MIN(2046,s)<<20); *expo = MAX(s-2046,0); |
#ifdef vax |
#ifdef vax |
t = m[0]; m[0] = m[1]; m[1] = t; itod(m); |
t = m[0]; m[0] = m[1]; m[1] = t; itod(m); |
#endif |
#endif |
#if defined(__i386__) || defined(MIPSEL) || defined(VISUAL) || defined(__MINGW32__) || defined(__MINGW64__) || defined(__alpha) || defined(__FreeBSD__) || defined(__NetBSD__) || defined(__x86_64) |
#if defined(__i386__) || defined(MIPSEL) || defined(VISUAL) || defined(__MINGW32__) || defined(__alpha) || defined(__FreeBSD__) || defined(__NetBSD__) || defined(__x86_64) || defined(__ARM_ARCH) || defined(ANDROID) |
t = m[0]; m[0] = m[1]; m[1] = t; |
t = m[0]; m[0] = m[1]; m[1] = t; |
#endif |
#endif |
return *((double *)m); |
return *((double *)m); |
} |
} |
|
|
void addreal(a,b,c) |
void addreal(a,b,c) |
Num a,b; |
Num a,b; |
Real *c; |
Real *c; |
{ |
{ |
double t; |
double t; |
|
|
t = ToReal(a)+ToReal(b); MKReal(t,*c); |
t = ToReal(a)+ToReal(b); MKReal(t,*c); |
} |
} |
|
|
void subreal(a,b,c) |
void subreal(a,b,c) |
Num a,b; |
Num a,b; |
Real *c; |
Real *c; |
{ |
{ |
double t; |
double t; |
|
|
t = ToReal(a)-ToReal(b); MKReal(t,*c); |
t = ToReal(a)-ToReal(b); MKReal(t,*c); |
} |
} |
|
|
void mulreal(a,b,c) |
void mulreal(a,b,c) |
Num a,b; |
Num a,b; |
Real *c; |
Real *c; |
{ |
{ |
double t; |
double t; |
|
|
if ( !a || !b ) |
if ( !a || !b ) |
*c = 0; |
*c = 0; |
else { |
else { |
t = ToReal(a)*ToReal(b); |
t = ToReal(a)*ToReal(b); |
#if 0 |
#if 0 |
if ( !t ) |
if ( !t ) |
error("mulreal : Underflow"); /* XXX */ |
error("mulreal : Underflow"); /* XXX */ |
else |
else |
#endif |
#endif |
MKReal(t,*c); |
MKReal(t,*c); |
} |
} |
} |
} |
|
|
void divreal(a,b,c) |
void divreal(a,b,c) |
Num a,b; |
Num a,b; |
Real *c; |
Real *c; |
{ |
{ |
double t; |
double t; |
|
|
if ( !a ) |
if ( !a ) |
*c = 0; |
*c = 0; |
else { |
else { |
t = ToReal(a)/ToReal(b); |
t = ToReal(a)/ToReal(b); |
#if 0 |
#if 0 |
if ( !t ) |
if ( !t ) |
error("divreal : Underflow"); /* XXX */ |
error("divreal : Underflow"); /* XXX */ |
else |
else |
#endif |
#endif |
MKReal(t,*c); |
MKReal(t,*c); |
} |
} |
} |
} |
|
|
void chsgnreal(a,c) |
void chsgnreal(a,c) |
Num a,*c; |
Num a,*c; |
{ |
{ |
double t; |
double t; |
Real s; |
Real s; |
|
|
if ( !a ) |
if ( !a ) |
*c = 0; |
*c = 0; |
else if ( NID(a) == N_Q ) |
else if ( NID(a) == N_Q ) |
chsgnq((Q)a,(Q *)c); |
chsgnq((Q)a,(Q *)c); |
else { |
else { |
t = -ToReal(a); MKReal(t,s); *c = (Num)s; |
t = -ToReal(a); MKReal(t,s); *c = (Num)s; |
} |
} |
} |
} |
|
|
void pwrreal(a,b,c) |
void pwrreal(a,b,c) |
Num a,b; |
Num a,b; |
Real *c; |
Real *c; |
{ |
{ |
double t; |
double t; |
double pwrreal0(); |
double pwrreal0(); |
|
|
if ( !b ) |
if ( !b ) |
MKReal(1.0,*c); |
MKReal(1.0,*c); |
else if ( !a ) |
else if ( !a ) |
*c = 0; |
*c = 0; |
else if ( !RATN(b) || !INT(b) || (PL(NM((Q)b)) > 1) ) { |
else if ( !RATN(b) || !INT(b) || (PL(NM((Q)b)) > 1) ) { |
t = (double)pow((double)ToReal(a),(double)ToReal(b)); |
t = (double)pow((double)ToReal(a),(double)ToReal(b)); |
#if 0 |
#if 0 |
if ( !t ) |
if ( !t ) |
error("pwrreal : Underflow"); /* XXX */ |
error("pwrreal : Underflow"); /* XXX */ |
else |
else |
#endif |
#endif |
MKReal(t,*c); |
MKReal(t,*c); |
} else { |
} else { |
t = pwrreal0(BDY((Real)a),BD(NM((Q)b))[0]); |
t = pwrreal0(BDY((Real)a),BD(NM((Q)b))[0]); |
t = SGN((Q)b)>0?t:1/t; |
t = SGN((Q)b)>0?t:1/t; |
#if 0 |
#if 0 |
if ( !t ) |
if ( !t ) |
error("pwrreal : Underflow"); /* XXX */ |
error("pwrreal : Underflow"); /* XXX */ |
else |
else |
#endif |
#endif |
MKReal(t,*c); |
MKReal(t,*c); |
} |
} |
} |
} |
|
|
double pwrreal0(n,e) |
double pwrreal0(n,e) |
double n; |
double n; |
int e; |
int e; |
{ |
{ |
double t; |
double t; |
|
|
if ( e == 1 ) |
if ( e == 1 ) |
return n; |
return n; |
else { |
else { |
t = pwrreal0(n,e / 2); |
t = pwrreal0(n,e / 2); |
return e%2 ? t*t*n : t*t; |
return e%2 ? t*t*n : t*t; |
} |
} |
} |
} |
|
|
int cmpreal(a,b) |
int cmpreal(a,b) |
Real a,b; |
Real a,b; |
{ |
{ |
double t; |
double t; |
|
|
t = ToReal(a)-ToReal(b); |
t = ToReal(a)-ToReal(b); |
return t>0.0 ? 1 : t<0.0?-1 : 0; |
return t>0.0 ? 1 : t<0.0?-1 : 0; |
} |
} |