From cc9b522d7a8d8f6658ce4b411eb4d50278e516b7 Mon Sep 17 00:00:00 2001 From: Zueuk Date: Sun, 21 Feb 2016 15:26:30 +0400 Subject: [PATCH 1/3] fixed wrong optimization for binary "-" --- src/mathpresso/mathpresso.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/mathpresso/mathpresso.cpp b/src/mathpresso/mathpresso.cpp index 3226031..8433f93 100644 --- a/src/mathpresso/mathpresso.cpp +++ b/src/mathpresso/mathpresso.cpp @@ -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) , "%" ), From 6c6616bf8239d6331064060e70293cf6647ecea6 Mon Sep 17 00:00:00 2001 From: Zueuk Date: Sun, 21 Feb 2016 18:06:31 +0400 Subject: [PATCH 2/3] added function hypot(x, y) --- src/mathpresso/mathpresso.cpp | 1 + src/mathpresso/mathpresso_p.h | 1 + src/mathpresso/mpcompiler.cpp | 2 ++ src/mathpresso/mpeval_p.h | 3 ++- src/mathpresso/mpoptimizer.cpp | 1 + 5 files changed, 7 insertions(+), 1 deletion(-) diff --git a/src/mathpresso/mathpresso.cpp b/src/mathpresso/mathpresso.cpp index 8433f93..ba34325 100644 --- a/src/mathpresso/mathpresso.cpp +++ b/src/mathpresso/mathpresso.cpp @@ -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 diff --git a/src/mathpresso/mathpresso_p.h b/src/mathpresso/mathpresso_p.h index 2f8bec7..cf61d13 100644 --- a/src/mathpresso/mathpresso_p.h +++ b/src/mathpresso/mathpresso_p.h @@ -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 diff --git a/src/mathpresso/mpcompiler.cpp b/src/mathpresso/mpcompiler.cpp index f079072..396149b 100644 --- a/src/mathpresso/mpcompiler.cpp +++ b/src/mathpresso/mpcompiler.cpp @@ -70,6 +70,7 @@ struct JitUtils { case kOpMax : return (void*)(Arg2Func)mpMax; 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: @@ -543,6 +544,7 @@ emitInst: { case kOpPow: case kOpAtan2: + case kOpHypot: break; case kOpCopySign: { diff --git a/src/mathpresso/mpeval_p.h b/src/mathpresso/mpeval_p.h index dcb3850..492fd95 100644 --- a/src/mathpresso/mpeval_p.h +++ b/src/mathpresso/mpeval_p.h @@ -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 diff --git a/src/mathpresso/mpoptimizer.cpp b/src/mathpresso/mpoptimizer.cpp index 1c5656a..4dfaa4d 100644 --- a/src/mathpresso/mpoptimizer.cpp +++ b/src/mathpresso/mpoptimizer.cpp @@ -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: From 07a2444aa051534f2a6f59d0a4df580025a430a4 Mon Sep 17 00:00:00 2001 From: Zueuk Date: Wed, 24 Feb 2016 20:55:01 +0400 Subject: [PATCH 3/3] added exponential notation, allowed fractions without leading zero --- src/mathpresso/mptokenizer.cpp | 47 ++++++++++++++++++++++++++++++---- 1 file changed, 42 insertions(+), 5 deletions(-) diff --git a/src/mathpresso/mptokenizer.cpp b/src/mathpresso/mptokenizer.cpp index d49cc47..eab4692 100644 --- a/src/mathpresso/mptokenizer.cpp +++ b/src/mathpresso/mptokenizer.cpp @@ -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(static_cast(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(p[0]) - '0'; if (c > 9) break; iPart = (iPart * 10.0) + static_cast(static_cast(c)); + + if (++p == pEnd) + goto _NumberEnd; } // Parse an optional fraction. @@ -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(p[0])] > kTokenChar0x9) + goto _Invalid; + + for (;;) { + c = static_cast(p[0]) - '0'; + if (c > 9) + break; + + exponent = (exponent * 10) + static_cast(static_cast(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(p[0])] <= kTokenCharSym) { goto _Invalid; @@ -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);