From 13482493d7baafd4aad526f42aac28f7217e5bee Mon Sep 17 00:00:00 2001 From: Halid Odat Date: Fri, 10 Sep 2021 02:22:49 +0200 Subject: [PATCH] Optimize integer negation (#1464) * Optimize integer negation * Check for `-0` * Make `JsValue::rem` spec compliant Co-authored-by: jedel1043 --- boa/src/value/operations.rs | 18 +++++++++++++----- 1 file changed, 13 insertions(+), 5 deletions(-) diff --git a/boa/src/value/operations.rs b/boa/src/value/operations.rs index f0ddec27e7..23f6dde28c 100644 --- a/boa/src/value/operations.rs +++ b/boa/src/value/operations.rs @@ -132,12 +132,19 @@ impl JsValue { if *y == 0 { Self::nan() } 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::Integer(y)) => Self::new(x % f64::from(*y)), + (Self::Rational(x), Self::Rational(y)) => Self::new((x % y).copysign(*x)), + (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)) => { if y.is_zero() { @@ -394,7 +401,8 @@ impl JsValue { Err(_) => f64::NAN, }), 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(false) | Self::Null => Self::new(0), Self::BigInt(ref x) => Self::new(JsBigInt::neg(x)),