#include "config.h"
#include "MathObject.h"
#include "Lookup.h"
#include "ObjectPrototype.h"
#include "Operations.h"
#include <time.h>
#include <wtf/Assertions.h>
#include <wtf/MathExtras.h>
#include <wtf/RandomNumber.h>
#include <wtf/RandomNumberSeed.h>
namespace JSC {
ASSERT_HAS_TRIVIAL_DESTRUCTOR(MathObject);
static EncodedJSValue JSC_HOST_CALL mathProtoFuncAbs(ExecState*);
static EncodedJSValue JSC_HOST_CALL mathProtoFuncACos(ExecState*);
static EncodedJSValue JSC_HOST_CALL mathProtoFuncASin(ExecState*);
static EncodedJSValue JSC_HOST_CALL mathProtoFuncATan(ExecState*);
static EncodedJSValue JSC_HOST_CALL mathProtoFuncATan2(ExecState*);
static EncodedJSValue JSC_HOST_CALL mathProtoFuncCeil(ExecState*);
static EncodedJSValue JSC_HOST_CALL mathProtoFuncCos(ExecState*);
static EncodedJSValue JSC_HOST_CALL mathProtoFuncExp(ExecState*);
static EncodedJSValue JSC_HOST_CALL mathProtoFuncFloor(ExecState*);
static EncodedJSValue JSC_HOST_CALL mathProtoFuncLog(ExecState*);
static EncodedJSValue JSC_HOST_CALL mathProtoFuncMax(ExecState*);
static EncodedJSValue JSC_HOST_CALL mathProtoFuncMin(ExecState*);
static EncodedJSValue JSC_HOST_CALL mathProtoFuncPow(ExecState*);
static EncodedJSValue JSC_HOST_CALL mathProtoFuncRandom(ExecState*);
static EncodedJSValue JSC_HOST_CALL mathProtoFuncRound(ExecState*);
static EncodedJSValue JSC_HOST_CALL mathProtoFuncSin(ExecState*);
static EncodedJSValue JSC_HOST_CALL mathProtoFuncSqrt(ExecState*);
static EncodedJSValue JSC_HOST_CALL mathProtoFuncTan(ExecState*);
static EncodedJSValue JSC_HOST_CALL mathProtoFuncIMul(ExecState*);
}
#include "MathObject.lut.h"
namespace JSC {
const ClassInfo MathObject::s_info = { "Math", &Base::s_info, 0, ExecState::mathTable, CREATE_METHOD_TABLE(MathObject) };
MathObject::MathObject(JSGlobalObject* globalObject, Structure* structure)
: JSNonFinalObject(globalObject->vm(), structure)
{
}
void MathObject::finishCreation(ExecState* exec, JSGlobalObject* globalObject)
{
Base::finishCreation(globalObject->vm());
ASSERT(inherits(&s_info));
putDirectWithoutTransition(exec->vm(), Identifier(exec, "E"), jsNumber(exp(1.0)), DontDelete | DontEnum | ReadOnly);
putDirectWithoutTransition(exec->vm(), Identifier(exec, "LN2"), jsNumber(log(2.0)), DontDelete | DontEnum | ReadOnly);
putDirectWithoutTransition(exec->vm(), Identifier(exec, "LN10"), jsNumber(log(10.0)), DontDelete | DontEnum | ReadOnly);
putDirectWithoutTransition(exec->vm(), Identifier(exec, "LOG2E"), jsNumber(1.0 / log(2.0)), DontDelete | DontEnum | ReadOnly);
putDirectWithoutTransition(exec->vm(), Identifier(exec, "LOG10E"), jsNumber(0.4342944819032518), DontDelete | DontEnum | ReadOnly); putDirectWithoutTransition(exec->vm(), Identifier(exec, "PI"), jsNumber(piDouble), DontDelete | DontEnum | ReadOnly);
putDirectWithoutTransition(exec->vm(), Identifier(exec, "SQRT1_2"), jsNumber(sqrt(0.5)), DontDelete | DontEnum | ReadOnly);
putDirectWithoutTransition(exec->vm(), Identifier(exec, "SQRT2"), jsNumber(sqrt(2.0)), DontDelete | DontEnum | ReadOnly);
}
bool MathObject::getOwnPropertySlot(JSCell* cell, ExecState* exec, PropertyName propertyName, PropertySlot &slot)
{
return getStaticFunctionSlot<JSObject>(exec, ExecState::mathTable(exec), jsCast<MathObject*>(cell), propertyName, slot);
}
bool MathObject::getOwnPropertyDescriptor(JSObject* object, ExecState* exec, PropertyName propertyName, PropertyDescriptor& descriptor)
{
return getStaticFunctionDescriptor<JSObject>(exec, ExecState::mathTable(exec), jsCast<MathObject*>(object), propertyName, descriptor);
}
EncodedJSValue JSC_HOST_CALL mathProtoFuncAbs(ExecState* exec)
{
return JSValue::encode(jsNumber(fabs(exec->argument(0).toNumber(exec))));
}
EncodedJSValue JSC_HOST_CALL mathProtoFuncACos(ExecState* exec)
{
return JSValue::encode(jsDoubleNumber(acos(exec->argument(0).toNumber(exec))));
}
EncodedJSValue JSC_HOST_CALL mathProtoFuncASin(ExecState* exec)
{
return JSValue::encode(jsDoubleNumber(asin(exec->argument(0).toNumber(exec))));
}
EncodedJSValue JSC_HOST_CALL mathProtoFuncATan(ExecState* exec)
{
return JSValue::encode(jsDoubleNumber(atan(exec->argument(0).toNumber(exec))));
}
EncodedJSValue JSC_HOST_CALL mathProtoFuncATan2(ExecState* exec)
{
double arg0 = exec->argument(0).toNumber(exec);
double arg1 = exec->argument(1).toNumber(exec);
return JSValue::encode(jsDoubleNumber(atan2(arg0, arg1)));
}
EncodedJSValue JSC_HOST_CALL mathProtoFuncCeil(ExecState* exec)
{
return JSValue::encode(jsNumber(ceil(exec->argument(0).toNumber(exec))));
}
EncodedJSValue JSC_HOST_CALL mathProtoFuncCos(ExecState* exec)
{
return JSValue::encode(jsDoubleNumber(cos(exec->argument(0).toNumber(exec))));
}
EncodedJSValue JSC_HOST_CALL mathProtoFuncExp(ExecState* exec)
{
return JSValue::encode(jsDoubleNumber(exp(exec->argument(0).toNumber(exec))));
}
EncodedJSValue JSC_HOST_CALL mathProtoFuncFloor(ExecState* exec)
{
return JSValue::encode(jsNumber(floor(exec->argument(0).toNumber(exec))));
}
EncodedJSValue JSC_HOST_CALL mathProtoFuncLog(ExecState* exec)
{
return JSValue::encode(jsDoubleNumber(log(exec->argument(0).toNumber(exec))));
}
EncodedJSValue JSC_HOST_CALL mathProtoFuncMax(ExecState* exec)
{
unsigned argsCount = exec->argumentCount();
double result = -std::numeric_limits<double>::infinity();
for (unsigned k = 0; k < argsCount; ++k) {
double val = exec->argument(k).toNumber(exec);
if (std::isnan(val)) {
result = QNaN;
break;
}
if (val > result || (!val && !result && !std::signbit(val)))
result = val;
}
return JSValue::encode(jsNumber(result));
}
EncodedJSValue JSC_HOST_CALL mathProtoFuncMin(ExecState* exec)
{
unsigned argsCount = exec->argumentCount();
double result = +std::numeric_limits<double>::infinity();
for (unsigned k = 0; k < argsCount; ++k) {
double val = exec->argument(k).toNumber(exec);
if (std::isnan(val)) {
result = QNaN;
break;
}
if (val < result || (!val && !result && std::signbit(val)))
result = val;
}
return JSValue::encode(jsNumber(result));
}
#if PLATFORM(IOS) && CPU(ARM_THUMB2)
static double fdlibmPow(double x, double y);
static ALWAYS_INLINE bool isDenormal(double x)
{
static const uint64_t signbit = 0x8000000000000000ULL;
static const uint64_t minNormal = 0x0001000000000000ULL;
return (bitwise_cast<uint64_t>(x) & ~signbit) - 1 < minNormal - 1;
}
static ALWAYS_INLINE bool isEdgeCase(double x)
{
static const uint64_t signbit = 0x8000000000000000ULL;
static const uint64_t infinity = 0x7fffffffffffffffULL;
return (bitwise_cast<uint64_t>(x) & ~signbit) - 1 >= infinity - 1;
}
static ALWAYS_INLINE double mathPow(double x, double y)
{
if (!isDenormal(x) && !isDenormal(y)) {
double libmResult = pow(x,y);
if (libmResult || isEdgeCase(x) || isEdgeCase(y))
return libmResult;
}
return fdlibmPow(x,y);
}
#else
ALWAYS_INLINE double mathPow(double x, double y)
{
return pow(x, y);
}
#endif
EncodedJSValue JSC_HOST_CALL mathProtoFuncPow(ExecState* exec)
{
double arg = exec->argument(0).toNumber(exec);
double arg2 = exec->argument(1).toNumber(exec);
if (std::isnan(arg2))
return JSValue::encode(jsNaN());
if (std::isinf(arg2) && fabs(arg) == 1)
return JSValue::encode(jsNaN());
return JSValue::encode(jsNumber(mathPow(arg, arg2)));
}
EncodedJSValue JSC_HOST_CALL mathProtoFuncRandom(ExecState* exec)
{
return JSValue::encode(jsDoubleNumber(exec->lexicalGlobalObject()->weakRandomNumber()));
}
EncodedJSValue JSC_HOST_CALL mathProtoFuncRound(ExecState* exec)
{
double arg = exec->argument(0).toNumber(exec);
double integer = ceil(arg);
return JSValue::encode(jsNumber(integer - (integer - arg > 0.5)));
}
EncodedJSValue JSC_HOST_CALL mathProtoFuncSin(ExecState* exec)
{
return JSValue::encode(exec->vm().cachedSin(exec->argument(0).toNumber(exec)));
}
EncodedJSValue JSC_HOST_CALL mathProtoFuncSqrt(ExecState* exec)
{
return JSValue::encode(jsDoubleNumber(sqrt(exec->argument(0).toNumber(exec))));
}
EncodedJSValue JSC_HOST_CALL mathProtoFuncTan(ExecState* exec)
{
return JSValue::encode(jsDoubleNumber(tan(exec->argument(0).toNumber(exec))));
}
EncodedJSValue JSC_HOST_CALL mathProtoFuncIMul(ExecState* exec)
{
int32_t left = exec->argument(0).toInt32(exec);
if (exec->hadException())
return JSValue::encode(jsNull());
int32_t right = exec->argument(1).toInt32(exec);
return JSValue::encode(jsNumber(left * right));
}
#if PLATFORM(IOS) && CPU(ARM_THUMB2)
#define __HI(x) *(1+(int*)&x)
#define __LO(x) *(int*)&x
static const double
bp[] = {1.0, 1.5,},
dp_h[] = { 0.0, 5.84962487220764160156e-01,},
dp_l[] = { 0.0, 1.35003920212974897128e-08,},
zero = 0.0,
one = 1.0,
two = 2.0,
two53 = 9007199254740992.0,
huge = 1.0e300,
tiny = 1.0e-300,
two54 = 1.80143985094819840000e+16,
twom54 = 5.55111512312578270212e-17,
L1 = 5.99999999999994648725e-01,
L2 = 4.28571428578550184252e-01,
L3 = 3.33333329818377432918e-01,
L4 = 2.72728123808534006489e-01,
L5 = 2.30660745775561754067e-01,
L6 = 2.06975017800338417784e-01,
P1 = 1.66666666666666019037e-01,
P2 = -2.77777777770155933842e-03,
P3 = 6.61375632143793436117e-05,
P4 = -1.65339022054652515390e-06,
P5 = 4.13813679705723846039e-08,
lg2 = 6.93147180559945286227e-01,
lg2_h = 6.93147182464599609375e-01,
lg2_l = -1.90465429995776804525e-09,
ovt = 8.0085662595372944372e-0017,
cp = 9.61796693925975554329e-01,
cp_h = 9.61796700954437255859e-01,
cp_l = -7.02846165095275826516e-09,
ivln2 = 1.44269504088896338700e+00,
ivln2_h = 1.44269502162933349609e+00,
ivln2_l = 1.92596299112661746887e-08;
inline double fdlibmScalbn (double x, int n)
{
int k,hx,lx;
hx = __HI(x);
lx = __LO(x);
k = (hx&0x7ff00000)>>20;
if (k==0) {
if ((lx|(hx&0x7fffffff))==0) return x;
x *= two54;
hx = __HI(x);
k = ((hx&0x7ff00000)>>20) - 54;
if (n< -50000) return tiny*x;
}
if (k==0x7ff) return x+x;
k = k+n;
if (k > 0x7fe) return huge*copysign(huge,x);
if (k > 0)
{__HI(x) = (hx&0x800fffff)|(k<<20); return x;}
if (k <= -54) {
if (n > 50000)
return huge*copysign(huge,x);
else return tiny*copysign(tiny,x);
}
k += 54;
__HI(x) = (hx&0x800fffff)|(k<<20);
return x*twom54;
}
double fdlibmPow(double x, double y)
{
double z,ax,z_h,z_l,p_h,p_l;
double y1,t1,t2,r,s,t,u,v,w;
int i0,i1,i,j,k,yisint,n;
int hx,hy,ix,iy;
unsigned lx,ly;
i0 = ((*(int*)&one)>>29)^1; i1=1-i0;
hx = __HI(x); lx = __LO(x);
hy = __HI(y); ly = __LO(y);
ix = hx&0x7fffffff; iy = hy&0x7fffffff;
if((iy|ly)==0) return one;
if(ix > 0x7ff00000 || ((ix==0x7ff00000)&&(lx!=0)) ||
iy > 0x7ff00000 || ((iy==0x7ff00000)&&(ly!=0)))
return x+y;
yisint = 0;
if(hx<0) {
if(iy>=0x43400000) yisint = 2;
else if(iy>=0x3ff00000) {
k = (iy>>20)-0x3ff;
if(k>20) {
j = ly>>(52-k);
if(static_cast<unsigned>(j<<(52-k))==ly) yisint = 2-(j&1);
} else if(ly==0) {
j = iy>>(20-k);
if((j<<(20-k))==iy) yisint = 2-(j&1);
}
}
}
if(ly==0) {
if (iy==0x7ff00000) {
if(((ix-0x3ff00000)|lx)==0)
return y - y;
else if (ix >= 0x3ff00000)
return (hy>=0)? y: zero;
else
return (hy<0)?-y: zero;
}
if(iy==0x3ff00000) {
if(hy<0) return one/x; else return x;
}
if(hy==0x40000000) return x*x;
if(hy==0x3fe00000) {
if(hx>=0)
return sqrt(x);
}
}
ax = fabs(x);
if(lx==0) {
if(ix==0x7ff00000||ix==0||ix==0x3ff00000){
z = ax;
if(hy<0) z = one/z;
if(hx<0) {
if(((ix-0x3ff00000)|yisint)==0) {
z = (z-z)/(z-z);
} else if(yisint==1)
z = -z;
}
return z;
}
}
n = (hx>>31)+1;
if((n|yisint)==0) return (x-x)/(x-x);
s = one;
if((n|(yisint-1))==0) s = -one;
if(iy>0x41e00000) {
if(iy>0x43f00000){
if(ix<=0x3fefffff) return (hy<0)? huge*huge:tiny*tiny;
if(ix>=0x3ff00000) return (hy>0)? huge*huge:tiny*tiny;
}
if(ix<0x3fefffff) return (hy<0)? s*huge*huge:s*tiny*tiny;
if(ix>0x3ff00000) return (hy>0)? s*huge*huge:s*tiny*tiny;
t = ax-one;
w = (t*t)*(0.5-t*(0.3333333333333333333333-t*0.25));
u = ivln2_h*t;
v = t*ivln2_l-w*ivln2;
t1 = u+v;
__LO(t1) = 0;
t2 = v-(t1-u);
} else {
double ss,s2,s_h,s_l,t_h,t_l;
n = 0;
if(ix<0x00100000)
{ax *= two53; n -= 53; ix = __HI(ax); }
n += ((ix)>>20)-0x3ff;
j = ix&0x000fffff;
ix = j|0x3ff00000;
if(j<=0x3988E) k=0;
else if(j<0xBB67A) k=1;
else {k=0;n+=1;ix -= 0x00100000;}
__HI(ax) = ix;
u = ax-bp[k];
v = one/(ax+bp[k]);
ss = u*v;
s_h = ss;
__LO(s_h) = 0;
t_h = zero;
__HI(t_h)=((ix>>1)|0x20000000)+0x00080000+(k<<18);
t_l = ax - (t_h-bp[k]);
s_l = v*((u-s_h*t_h)-s_h*t_l);
s2 = ss*ss;
r = s2*s2*(L1+s2*(L2+s2*(L3+s2*(L4+s2*(L5+s2*L6)))));
r += s_l*(s_h+ss);
s2 = s_h*s_h;
t_h = 3.0+s2+r;
__LO(t_h) = 0;
t_l = r-((t_h-3.0)-s2);
u = s_h*t_h;
v = s_l*t_h+t_l*ss;
p_h = u+v;
__LO(p_h) = 0;
p_l = v-(p_h-u);
z_h = cp_h*p_h;
z_l = cp_l*p_h+p_l*cp+dp_l[k];
t = (double)n;
t1 = (((z_h+z_l)+dp_h[k])+t);
__LO(t1) = 0;
t2 = z_l-(((t1-t)-dp_h[k])-z_h);
}
y1 = y;
__LO(y1) = 0;
p_l = (y-y1)*t1+y*t2;
p_h = y1*t1;
z = p_l+p_h;
j = __HI(z);
i = __LO(z);
if (j>=0x40900000) {
if(((j-0x40900000)|i)!=0)
return s*huge*huge;
else {
if(p_l+ovt>z-p_h) return s*huge*huge;
}
} else if((j&0x7fffffff)>=0x4090cc00 ) {
if(((j-0xc090cc00)|i)!=0)
return s*tiny*tiny;
else {
if(p_l<=z-p_h) return s*tiny*tiny;
}
}
i = j&0x7fffffff;
k = (i>>20)-0x3ff;
n = 0;
if(i>0x3fe00000) {
n = j+(0x00100000>>(k+1));
k = ((n&0x7fffffff)>>20)-0x3ff;
t = zero;
__HI(t) = (n&~(0x000fffff>>k));
n = ((n&0x000fffff)|0x00100000)>>(20-k);
if(j<0) n = -n;
p_h -= t;
}
t = p_l+p_h;
__LO(t) = 0;
u = t*lg2_h;
v = (p_l-(t-p_h))*lg2+t*lg2_l;
z = u+v;
w = v-(z-u);
t = z*z;
t1 = z - t*(P1+t*(P2+t*(P3+t*(P4+t*P5))));
r = (z*t1)/(t1-two)-(w+z*w);
z = one-(r-z);
j = __HI(z);
j += (n<<20);
if((j>>20)<=0) z = fdlibmScalbn(z,n);
else __HI(z) += (n<<20);
return s*z;
}
#endif
}