1 // Copyright 2016 the V8 project authors. All rights reserved. 2 // Use of this source code is governed by a BSD-style license that can be 3 // found in the LICENSE file. 4 5 #include <limits> 6 7 #include "src/base/ieee754.h" 8 #include "src/base/macros.h" 9 #include "testing/gmock-support.h" 10 #include "testing/gtest-support.h" 11 12 using testing::BitEq; 13 using testing::IsNaN; 14 15 namespace v8 { 16 namespace base { 17 namespace ieee754 { 18 19 namespace { 20 21 double const kE = 2.718281828459045; 22 double const kPI = 3.141592653589793; 23 double const kTwo120 = 1.329227995784916e+36; 24 25 } // namespace 26 27 TEST(Ieee754, Atan) { 28 EXPECT_THAT(atan(std::numeric_limits<double>::quiet_NaN()), IsNaN()); 29 EXPECT_THAT(atan(std::numeric_limits<double>::signaling_NaN()), IsNaN()); 30 EXPECT_THAT(atan(-0.0), BitEq(-0.0)); 31 EXPECT_THAT(atan(0.0), BitEq(0.0)); 32 EXPECT_DOUBLE_EQ(1.5707963267948966, 33 atan(std::numeric_limits<double>::infinity())); 34 EXPECT_DOUBLE_EQ(-1.5707963267948966, 35 atan(-std::numeric_limits<double>::infinity())); 36 } 37 38 TEST(Ieee754, Atan2) { 39 EXPECT_THAT(atan2(std::numeric_limits<double>::quiet_NaN(), 40 std::numeric_limits<double>::quiet_NaN()), 41 IsNaN()); 42 EXPECT_THAT(atan2(std::numeric_limits<double>::quiet_NaN(), 43 std::numeric_limits<double>::signaling_NaN()), 44 IsNaN()); 45 EXPECT_THAT(atan2(std::numeric_limits<double>::signaling_NaN(), 46 std::numeric_limits<double>::quiet_NaN()), 47 IsNaN()); 48 EXPECT_THAT(atan2(std::numeric_limits<double>::signaling_NaN(), 49 std::numeric_limits<double>::signaling_NaN()), 50 IsNaN()); 51 EXPECT_DOUBLE_EQ(0.7853981633974483, 52 atan2(std::numeric_limits<double>::infinity(), 53 std::numeric_limits<double>::infinity())); 54 EXPECT_DOUBLE_EQ(2.356194490192345, 55 atan2(std::numeric_limits<double>::infinity(), 56 -std::numeric_limits<double>::infinity())); 57 EXPECT_DOUBLE_EQ(-0.7853981633974483, 58 atan2(-std::numeric_limits<double>::infinity(), 59 std::numeric_limits<double>::infinity())); 60 EXPECT_DOUBLE_EQ(-2.356194490192345, 61 atan2(-std::numeric_limits<double>::infinity(), 62 -std::numeric_limits<double>::infinity())); 63 } 64 65 TEST(Ieee754, Atanh) { 66 EXPECT_THAT(atanh(std::numeric_limits<double>::quiet_NaN()), IsNaN()); 67 EXPECT_THAT(atanh(std::numeric_limits<double>::signaling_NaN()), IsNaN()); 68 EXPECT_THAT(atanh(std::numeric_limits<double>::infinity()), IsNaN()); 69 EXPECT_EQ(std::numeric_limits<double>::infinity(), atanh(1)); 70 EXPECT_EQ(-std::numeric_limits<double>::infinity(), atanh(-1)); 71 EXPECT_DOUBLE_EQ(0.54930614433405478, atanh(0.5)); 72 } 73 74 TEST(Ieee754, Cos) { 75 // Test values mentioned in the EcmaScript spec. 76 EXPECT_THAT(cos(std::numeric_limits<double>::quiet_NaN()), IsNaN()); 77 EXPECT_THAT(cos(std::numeric_limits<double>::signaling_NaN()), IsNaN()); 78 EXPECT_THAT(cos(std::numeric_limits<double>::infinity()), IsNaN()); 79 EXPECT_THAT(cos(-std::numeric_limits<double>::infinity()), IsNaN()); 80 81 // Tests for cos for |x| < pi/4 82 EXPECT_EQ(1.0, 1 / cos(-0.0)); 83 EXPECT_EQ(1.0, 1 / cos(0.0)); 84 // cos(x) = 1 for |x| < 2^-27 85 EXPECT_EQ(1, cos(2.3283064365386963e-10)); 86 EXPECT_EQ(1, cos(-2.3283064365386963e-10)); 87 // Test KERNELCOS for |x| < 0.3. 88 // cos(pi/20) = sqrt(sqrt(2)*sqrt(sqrt(5)+5)+4)/2^(3/2) 89 EXPECT_EQ(0.9876883405951378, cos(0.15707963267948966)); 90 // Test KERNELCOS for x ~= 0.78125 91 EXPECT_EQ(0.7100335477927638, cos(0.7812504768371582)); 92 EXPECT_EQ(0.7100338835660797, cos(0.78125)); 93 // Test KERNELCOS for |x| > 0.3. 94 // cos(pi/8) = sqrt(sqrt(2)+1)/2^(3/4) 95 EXPECT_EQ(0.9238795325112867, cos(0.39269908169872414)); 96 // Test KERNELTAN for |x| < 0.67434. 97 EXPECT_EQ(0.9238795325112867, cos(-0.39269908169872414)); 98 99 // Tests for cos. 100 EXPECT_EQ(1, cos(3.725290298461914e-9)); 101 // Cover different code paths in KERNELCOS. 102 EXPECT_EQ(0.9689124217106447, cos(0.25)); 103 EXPECT_EQ(0.8775825618903728, cos(0.5)); 104 EXPECT_EQ(0.7073882691671998, cos(0.785)); 105 // Test that cos(Math.PI/2) != 0 since Math.PI is not exact. 106 EXPECT_EQ(6.123233995736766e-17, cos(1.5707963267948966)); 107 // Test cos for various phases. 108 EXPECT_EQ(0.7071067811865474, cos(7.0 / 4 * kPI)); 109 EXPECT_EQ(0.7071067811865477, cos(9.0 / 4 * kPI)); 110 EXPECT_EQ(-0.7071067811865467, cos(11.0 / 4 * kPI)); 111 EXPECT_EQ(-0.7071067811865471, cos(13.0 / 4 * kPI)); 112 EXPECT_EQ(0.9367521275331447, cos(1000000.0)); 113 EXPECT_EQ(-3.435757038074824e-12, cos(1048575.0 / 2 * kPI)); 114 115 // Test Hayne-Panek reduction. 116 EXPECT_EQ(-0.9258790228548379e0, cos(kTwo120)); 117 EXPECT_EQ(-0.9258790228548379e0, cos(-kTwo120)); 118 } 119 120 TEST(Ieee754, Exp) { 121 EXPECT_THAT(exp(std::numeric_limits<double>::quiet_NaN()), IsNaN()); 122 EXPECT_THAT(exp(std::numeric_limits<double>::signaling_NaN()), IsNaN()); 123 EXPECT_EQ(0.0, exp(-std::numeric_limits<double>::infinity())); 124 EXPECT_EQ(0.0, exp(-1000)); 125 EXPECT_EQ(0.0, exp(-745.1332191019412)); 126 EXPECT_EQ(2.2250738585072626e-308, exp(-708.39641853226408)); 127 EXPECT_EQ(3.307553003638408e-308, exp(-708.0)); 128 EXPECT_EQ(4.9406564584124654e-324, exp(-7.45133219101941108420e+02)); 129 EXPECT_EQ(0.36787944117144233, exp(-1.0)); 130 EXPECT_EQ(1.0, exp(-0.0)); 131 EXPECT_EQ(1.0, exp(0.0)); 132 EXPECT_EQ(1.0, exp(2.2250738585072014e-308)); 133 134 // Test that exp(x) is monotonic near 1. 135 EXPECT_GE(exp(1.0), exp(0.9999999999999999)); 136 EXPECT_LE(exp(1.0), exp(1.0000000000000002)); 137 138 // Test that we produce the correctly rounded result for 1. 139 EXPECT_EQ(kE, exp(1.0)); 140 141 EXPECT_EQ(7.38905609893065e0, exp(2.0)); 142 EXPECT_EQ(1.7976931348622732e308, exp(7.09782712893383973096e+02)); 143 EXPECT_EQ(2.6881171418161356e+43, exp(100.0)); 144 EXPECT_EQ(8.218407461554972e+307, exp(709.0)); 145 EXPECT_EQ(1.7968190737295725e308, exp(709.7822265625e0)); 146 EXPECT_EQ(std::numeric_limits<double>::infinity(), exp(709.7827128933841e0)); 147 EXPECT_EQ(std::numeric_limits<double>::infinity(), exp(710.0)); 148 EXPECT_EQ(std::numeric_limits<double>::infinity(), exp(1000.0)); 149 EXPECT_EQ(std::numeric_limits<double>::infinity(), 150 exp(std::numeric_limits<double>::infinity())); 151 } 152 153 TEST(Ieee754, Expm1) { 154 EXPECT_THAT(expm1(std::numeric_limits<double>::quiet_NaN()), IsNaN()); 155 EXPECT_THAT(expm1(std::numeric_limits<double>::signaling_NaN()), IsNaN()); 156 EXPECT_EQ(-1.0, expm1(-std::numeric_limits<double>::infinity())); 157 EXPECT_EQ(std::numeric_limits<double>::infinity(), 158 expm1(std::numeric_limits<double>::infinity())); 159 EXPECT_EQ(0.0, expm1(-0.0)); 160 EXPECT_EQ(0.0, expm1(0.0)); 161 EXPECT_EQ(1.718281828459045, expm1(1.0)); 162 EXPECT_EQ(2.6881171418161356e+43, expm1(100.0)); 163 EXPECT_EQ(8.218407461554972e+307, expm1(709.0)); 164 EXPECT_EQ(std::numeric_limits<double>::infinity(), expm1(710.0)); 165 } 166 167 TEST(Ieee754, Log) { 168 EXPECT_THAT(log(std::numeric_limits<double>::quiet_NaN()), IsNaN()); 169 EXPECT_THAT(log(std::numeric_limits<double>::signaling_NaN()), IsNaN()); 170 EXPECT_THAT(log(-std::numeric_limits<double>::infinity()), IsNaN()); 171 EXPECT_THAT(log(-1.0), IsNaN()); 172 EXPECT_EQ(-std::numeric_limits<double>::infinity(), log(-0.0)); 173 EXPECT_EQ(-std::numeric_limits<double>::infinity(), log(0.0)); 174 EXPECT_EQ(0.0, log(1.0)); 175 EXPECT_EQ(std::numeric_limits<double>::infinity(), 176 log(std::numeric_limits<double>::infinity())); 177 178 // Test that log(E) produces the correctly rounded result. 179 EXPECT_EQ(1.0, log(kE)); 180 } 181 182 TEST(Ieee754, Log1p) { 183 EXPECT_THAT(log1p(std::numeric_limits<double>::quiet_NaN()), IsNaN()); 184 EXPECT_THAT(log1p(std::numeric_limits<double>::signaling_NaN()), IsNaN()); 185 EXPECT_THAT(log1p(-std::numeric_limits<double>::infinity()), IsNaN()); 186 EXPECT_EQ(-std::numeric_limits<double>::infinity(), log1p(-1.0)); 187 EXPECT_EQ(0.0, log1p(0.0)); 188 EXPECT_EQ(-0.0, log1p(-0.0)); 189 EXPECT_EQ(std::numeric_limits<double>::infinity(), 190 log1p(std::numeric_limits<double>::infinity())); 191 EXPECT_EQ(6.9756137364252422e-03, log1p(0.007)); 192 EXPECT_EQ(709.782712893384, log1p(1.7976931348623157e308)); 193 EXPECT_EQ(2.7755575615628914e-17, log1p(2.7755575615628914e-17)); 194 EXPECT_EQ(9.313225741817976e-10, log1p(9.313225746154785e-10)); 195 EXPECT_EQ(-0.2876820724517809, log1p(-0.25)); 196 EXPECT_EQ(0.22314355131420976, log1p(0.25)); 197 EXPECT_EQ(2.3978952727983707, log1p(10)); 198 EXPECT_EQ(36.841361487904734, log1p(10e15)); 199 EXPECT_EQ(37.08337388996168, log1p(12738099905822720)); 200 EXPECT_EQ(37.08336444902049, log1p(12737979646738432)); 201 EXPECT_EQ(1.3862943611198906, log1p(3)); 202 EXPECT_EQ(1.3862945995384413, log1p(3 + 9.5367431640625e-7)); 203 EXPECT_EQ(0.5596157879354227, log1p(0.75)); 204 EXPECT_EQ(0.8109302162163288, log1p(1.25)); 205 } 206 207 TEST(Ieee754, Log2) { 208 EXPECT_THAT(log2(std::numeric_limits<double>::quiet_NaN()), IsNaN()); 209 EXPECT_THAT(log2(std::numeric_limits<double>::signaling_NaN()), IsNaN()); 210 EXPECT_THAT(log2(-std::numeric_limits<double>::infinity()), IsNaN()); 211 EXPECT_THAT(log2(-1.0), IsNaN()); 212 EXPECT_EQ(-std::numeric_limits<double>::infinity(), log2(0.0)); 213 EXPECT_EQ(-std::numeric_limits<double>::infinity(), log2(-0.0)); 214 EXPECT_EQ(std::numeric_limits<double>::infinity(), 215 log2(std::numeric_limits<double>::infinity())); 216 } 217 218 TEST(Ieee754, Log10) { 219 EXPECT_THAT(log10(std::numeric_limits<double>::quiet_NaN()), IsNaN()); 220 EXPECT_THAT(log10(std::numeric_limits<double>::signaling_NaN()), IsNaN()); 221 EXPECT_THAT(log10(-std::numeric_limits<double>::infinity()), IsNaN()); 222 EXPECT_THAT(log10(-1.0), IsNaN()); 223 EXPECT_EQ(-std::numeric_limits<double>::infinity(), log10(0.0)); 224 EXPECT_EQ(-std::numeric_limits<double>::infinity(), log10(-0.0)); 225 EXPECT_EQ(std::numeric_limits<double>::infinity(), 226 log10(std::numeric_limits<double>::infinity())); 227 EXPECT_EQ(3.0, log10(1000.0)); 228 EXPECT_EQ(14.0, log10(100000000000000)); // log10(10 ^ 14) 229 EXPECT_EQ(3.7389561269540406, log10(5482.2158)); 230 EXPECT_EQ(14.661551142893833, log10(458723662312872.125782332587)); 231 EXPECT_EQ(-0.9083828622192334, log10(0.12348583358871)); 232 EXPECT_EQ(5.0, log10(100000.0)); 233 } 234 235 TEST(Ieee754, Cbrt) { 236 EXPECT_THAT(cbrt(std::numeric_limits<double>::quiet_NaN()), IsNaN()); 237 EXPECT_THAT(cbrt(std::numeric_limits<double>::signaling_NaN()), IsNaN()); 238 EXPECT_EQ(std::numeric_limits<double>::infinity(), 239 cbrt(std::numeric_limits<double>::infinity())); 240 EXPECT_EQ(-std::numeric_limits<double>::infinity(), 241 cbrt(-std::numeric_limits<double>::infinity())); 242 EXPECT_EQ(1.4422495703074083, cbrt(3)); 243 EXPECT_EQ(100, cbrt(100 * 100 * 100)); 244 EXPECT_EQ(46.415888336127786, cbrt(100000)); 245 } 246 247 TEST(Ieee754, Sin) { 248 // Test values mentioned in the EcmaScript spec. 249 EXPECT_THAT(sin(std::numeric_limits<double>::quiet_NaN()), IsNaN()); 250 EXPECT_THAT(sin(std::numeric_limits<double>::signaling_NaN()), IsNaN()); 251 EXPECT_THAT(sin(std::numeric_limits<double>::infinity()), IsNaN()); 252 EXPECT_THAT(sin(-std::numeric_limits<double>::infinity()), IsNaN()); 253 254 // Tests for sin for |x| < pi/4 255 EXPECT_EQ(-std::numeric_limits<double>::infinity(), 1 / sin(-0.0)); 256 EXPECT_EQ(std::numeric_limits<double>::infinity(), 1 / sin(0.0)); 257 // sin(x) = x for x < 2^-27 258 EXPECT_EQ(2.3283064365386963e-10, sin(2.3283064365386963e-10)); 259 EXPECT_EQ(-2.3283064365386963e-10, sin(-2.3283064365386963e-10)); 260 // sin(pi/8) = sqrt(sqrt(2)-1)/2^(3/4) 261 EXPECT_EQ(0.3826834323650898, sin(0.39269908169872414)); 262 EXPECT_EQ(-0.3826834323650898, sin(-0.39269908169872414)); 263 264 // Tests for sin. 265 EXPECT_EQ(0.479425538604203, sin(0.5)); 266 EXPECT_EQ(-0.479425538604203, sin(-0.5)); 267 EXPECT_EQ(1, sin(kPI / 2.0)); 268 EXPECT_EQ(-1, sin(-kPI / 2.0)); 269 // Test that sin(Math.PI) != 0 since Math.PI is not exact. 270 EXPECT_EQ(1.2246467991473532e-16, sin(kPI)); 271 EXPECT_EQ(-7.047032979958965e-14, sin(2200.0 * kPI)); 272 // Test sin for various phases. 273 EXPECT_EQ(-0.7071067811865477, sin(7.0 / 4.0 * kPI)); 274 EXPECT_EQ(0.7071067811865474, sin(9.0 / 4.0 * kPI)); 275 EXPECT_EQ(0.7071067811865483, sin(11.0 / 4.0 * kPI)); 276 EXPECT_EQ(-0.7071067811865479, sin(13.0 / 4.0 * kPI)); 277 EXPECT_EQ(-3.2103381051568376e-11, sin(1048576.0 / 4 * kPI)); 278 279 // Test Hayne-Panek reduction. 280 EXPECT_EQ(0.377820109360752e0, sin(kTwo120)); 281 EXPECT_EQ(-0.377820109360752e0, sin(-kTwo120)); 282 } 283 284 TEST(Ieee754, Tan) { 285 // Test values mentioned in the EcmaScript spec. 286 EXPECT_THAT(tan(std::numeric_limits<double>::quiet_NaN()), IsNaN()); 287 EXPECT_THAT(tan(std::numeric_limits<double>::signaling_NaN()), IsNaN()); 288 EXPECT_THAT(tan(std::numeric_limits<double>::infinity()), IsNaN()); 289 EXPECT_THAT(tan(-std::numeric_limits<double>::infinity()), IsNaN()); 290 291 // Tests for tan for |x| < pi/4 292 EXPECT_EQ(std::numeric_limits<double>::infinity(), 1 / tan(0.0)); 293 EXPECT_EQ(-std::numeric_limits<double>::infinity(), 1 / tan(-0.0)); 294 // tan(x) = x for |x| < 2^-28 295 EXPECT_EQ(2.3283064365386963e-10, tan(2.3283064365386963e-10)); 296 EXPECT_EQ(-2.3283064365386963e-10, tan(-2.3283064365386963e-10)); 297 // Test KERNELTAN for |x| > 0.67434. 298 EXPECT_EQ(0.8211418015898941, tan(11.0 / 16.0)); 299 EXPECT_EQ(-0.8211418015898941, tan(-11.0 / 16.0)); 300 EXPECT_EQ(0.41421356237309503, tan(0.39269908169872414)); 301 // crbug/427468 302 EXPECT_EQ(0.7993357819992383, tan(0.6743358)); 303 304 // Tests for tan. 305 EXPECT_EQ(3.725290298461914e-9, tan(3.725290298461914e-9)); 306 // Test that tan(PI/2) != Infinity since PI is not exact. 307 EXPECT_EQ(1.633123935319537e16, tan(kPI / 2)); 308 // Cover different code paths in KERNELTAN (tangent and cotangent) 309 EXPECT_EQ(0.5463024898437905, tan(0.5)); 310 EXPECT_EQ(2.0000000000000027, tan(1.107148717794091)); 311 EXPECT_EQ(-1.0000000000000004, tan(7.0 / 4.0 * kPI)); 312 EXPECT_EQ(0.9999999999999994, tan(9.0 / 4.0 * kPI)); 313 EXPECT_EQ(-6.420676210313675e-11, tan(1048576.0 / 2.0 * kPI)); 314 EXPECT_EQ(2.910566692924059e11, tan(1048575.0 / 2.0 * kPI)); 315 316 // Test Hayne-Panek reduction. 317 EXPECT_EQ(-0.40806638884180424e0, tan(kTwo120)); 318 EXPECT_EQ(0.40806638884180424e0, tan(-kTwo120)); 319 } 320 321 } // namespace ieee754 322 } // namespace base 323 } // namespace v8 324