Skip to content

Commit

Permalink
Merge pull request #4 from Zueuk/master
Browse files Browse the repository at this point in the history
Some small improvements
  • Loading branch information
kobalicek committed Feb 25, 2016
2 parents 60f2cdb + 07a2444 commit 7e01ef5
Show file tree
Hide file tree
Showing 6 changed files with 50 additions and 7 deletions.
3 changes: 2 additions & 1 deletion src/mathpresso/mathpresso.cpp
Expand Up @@ -83,7 +83,7 @@ const OpInfo mpOpInfo[kOpCount] = {
ROW(Gt , Gt , 2, 8, 0, 0, LTR | F(Condition) , ">" ),
ROW(Ge , Ge , 2, 8, 0, 0, LTR | F(Condition) , ">=" ),
ROW(Add , Add , 2, 6, 0, 0, LTR | F(Arithmetic) | F(NopIfZero) , "+" ),
ROW(Sub , Sub , 2, 6, 0, 0, LTR | F(Arithmetic) | F(NopIfZero) , "-" ),
ROW(Sub , Sub , 2, 6, 0, 0, LTR | F(Arithmetic) | F(NopIfRZero) , "-" ),
ROW(Mul , Mul , 2, 5, 0, 0, LTR | F(Arithmetic) | F(NopIfOne) , "*" ),
ROW(Div , Div , 2, 5, 0, 0, LTR | F(Arithmetic) | F(NopIfROne) , "/" ),
ROW(Mod , Mod , 2, 5, 0, 0, LTR | F(Arithmetic) , "%" ),
Expand All @@ -92,6 +92,7 @@ const OpInfo mpOpInfo[kOpCount] = {
ROW(Max , Max , 2, 0, 0, 1, LTR | 0 , "max" ),
ROW(Pow , Pow , 2, 0, 0, 1, LTR | F(NopIfROne) , "pow" ),
ROW(Atan2 , Atan2 , 2, 0, 0, 1, LTR | F(Trigonometric) , "atan2" ),
ROW(Hypot , Hypot , 2, 0, 0, 1, LTR | F(Trigonometric) , "hypot" ),
ROW(CopySign , CopySign , 2, 0, 0, 1, LTR | 0 , "copysign" )
};
#undef F
Expand Down
1 change: 1 addition & 0 deletions src/mathpresso/mathpresso_p.h
Expand Up @@ -236,6 +236,7 @@ enum OpType {
kOpMax, // max(a, b)
kOpPow, // pow(a, b)
kOpAtan2, // atan2(a, b)
kOpHypot, // hypot(a, b)
kOpCopySign, // copysign(a, b)

//! \internal
Expand Down
2 changes: 2 additions & 0 deletions src/mathpresso/mpcompiler.cpp
Expand Up @@ -70,6 +70,7 @@ struct JitUtils {
case kOpMax : return (void*)(Arg2Func)mpMax<double>;
case kOpPow : return (void*)(Arg2Func)pow;
case kOpAtan2 : return (void*)(Arg2Func)atan2;
case kOpHypot : return (void*)(Arg2Func)hypot;
case kOpCopySign : return (void*)(Arg2Func)mpCopySign;

default:
Expand Down Expand Up @@ -543,6 +544,7 @@ emitInst: {

case kOpPow:
case kOpAtan2:
case kOpHypot:
break;

case kOpCopySign: {
Expand Down
3 changes: 2 additions & 1 deletion src/mathpresso/mpeval_p.h
Expand Up @@ -102,7 +102,8 @@ static MATHPRESSO_INLINE double mpAsin(double x) { return ::asin(x); }
static MATHPRESSO_INLINE double mpAcos(double x) { return ::acos(x); }
static MATHPRESSO_INLINE double mpAtan(double x) { return ::atan(x); }

static MATHPRESSO_INLINE double mpAtan2(double x, double y) { return ::atan2(x, y); }
static MATHPRESSO_INLINE double mpAtan2(double y, double x) { return ::atan2(y, x); }
static MATHPRESSO_INLINE double mpHypot(double x, double y) { return ::hypot(x, y); }

} // mathpresso namespace

Expand Down
1 change: 1 addition & 0 deletions src/mathpresso/mpoptimizer.cpp
Expand Up @@ -203,6 +203,7 @@ Error AstOptimizer::onBinaryOp(AstBinaryOp* node) {
case kOpMax : result = mpMax(lVal, rVal); break;
case kOpPow : result = mpPow(lVal, rVal); break;
case kOpAtan2 : result = mpAtan2(lVal, rVal); break;
case kOpHypot : result = mpHypot(lVal, rVal); break;
case kOpCopySign: result = mpCopySign(lVal, rVal); break;

default:
Expand Down
47 changes: 42 additions & 5 deletions src/mathpresso/mptokenizer.cpp
Expand Up @@ -172,21 +172,22 @@ uint32_t Tokenizer::next(Token* token) {
// Save the first character of the token.
pToken = p;

if (c <= kTokenChar0x9) {
double iPart = static_cast<double>(static_cast<int>(c));
if (c <= kTokenChar0x9 || c == kTokenCharDot) {
double iPart = 0.0;
double fPart = 0.0;
int fPos = 0;
double exponent = 0;

// Parse the decimal part.
for (;;) {
if (++p == pEnd)
goto _NumberEnd;

c = static_cast<uint8_t>(p[0]) - '0';
if (c > 9)
break;

iPart = (iPart * 10.0) + static_cast<double>(static_cast<int>(c));

if (++p == pEnd)
goto _NumberEnd;
}

// Parse an optional fraction.
Expand All @@ -206,6 +207,40 @@ uint32_t Tokenizer::next(Token* token) {
}
}

// Parse an optional exponent
if (p != pEnd && mpGetLower(p[0]) == 'e') {
if (++p == pEnd)
goto _Invalid;

bool neg;
if (*p == '-') {
neg = true;
++p;
}
else {
neg = false;
if (*p == '+')
++p;
}
// Error if there is no number after the 'e'
if (p == pEnd || mpCharClass[static_cast<uint8_t>(p[0])] > kTokenChar0x9)
goto _Invalid;

for (;;) {
c = static_cast<uint8_t>(p[0]) - '0';
if (c > 9)
break;

exponent = (exponent * 10) + static_cast<double>(static_cast<int>(c));

if (++p == pEnd)
goto _NumberEnd;
}

if (neg)
exponent = -exponent;
}

// Error if there is an alpha-numeric character after the number.
if (p != pEnd && mpCharClass[static_cast<uint8_t>(p[0])] <= kTokenCharSym) {
goto _Invalid;
Expand All @@ -216,6 +251,8 @@ uint32_t Tokenizer::next(Token* token) {
double val = iPart;
if (fPos != 0)
val += fPart * ::pow(10.0, fPos);
if (exponent != 0)
val *= ::pow(10.0, exponent);

token->value = val;
token->setData((size_t)(pToken - _start), (size_t)(p - pToken), 0, kTokenNumber);
Expand Down

0 comments on commit 7e01ef5

Please sign in to comment.