Browse Source

Optimize integer negation (#1464)

* Optimize integer negation

* Check for `-0`

* Make `JsValue::rem` spec compliant

Co-authored-by: jedel1043 <jedel0124@gmail.com>
pull/1571/head
Halid Odat 3 years ago committed by GitHub
parent
commit
13482493d7
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 18
      boa/src/value/operations.rs

18
boa/src/value/operations.rs

@ -132,12 +132,19 @@ impl JsValue {
if *y == 0 { if *y == 0 {
Self::nan() Self::nan()
} else { } else {
Self::new(x % *y) match x % *y {
rem if rem == 0 && *x < 0 => Self::new(-0.0),
rem => Self::new(rem),
} }
} }
(Self::Rational(x), Self::Rational(y)) => Self::new(x % y), }
(Self::Integer(x), Self::Rational(y)) => Self::new(f64::from(*x) % y), (Self::Rational(x), Self::Rational(y)) => Self::new((x % y).copysign(*x)),
(Self::Rational(x), Self::Integer(y)) => Self::new(x % f64::from(*y)), (Self::Integer(x), Self::Rational(y)) => {
let x = f64::from(*x);
Self::new((x % y).copysign(x))
}
(Self::Rational(x), Self::Integer(y)) => Self::new((x % f64::from(*y)).copysign(*x)),
(Self::BigInt(ref x), Self::BigInt(ref y)) => { (Self::BigInt(ref x), Self::BigInt(ref y)) => {
if y.is_zero() { if y.is_zero() {
@ -394,7 +401,8 @@ impl JsValue {
Err(_) => f64::NAN, Err(_) => f64::NAN,
}), }),
Self::Rational(num) => Self::new(-num), Self::Rational(num) => Self::new(-num),
Self::Integer(num) => Self::new(-f64::from(num)), Self::Integer(num) if num == 0 => Self::new(-f64::from(0)),
Self::Integer(num) => Self::new(-num),
Self::Boolean(true) => Self::new(1), Self::Boolean(true) => Self::new(1),
Self::Boolean(false) | Self::Null => Self::new(0), Self::Boolean(false) | Self::Null => Self::new(0),
Self::BigInt(ref x) => Self::new(JsBigInt::neg(x)), Self::BigInt(ref x) => Self::new(JsBigInt::neg(x)),

Loading…
Cancel
Save