1 // Copyright 2010 the V8 project authors. All rights reserved. 2 // Redistribution and use in source and binary forms, with or without 3 // modification, are permitted provided that the following conditions are 4 // met: 5 // 6 // * Redistributions of source code must retain the above copyright 7 // notice, this list of conditions and the following disclaimer. 8 // * Redistributions in binary form must reproduce the above 9 // copyright notice, this list of conditions and the following 10 // disclaimer in the documentation and/or other materials provided 11 // with the distribution. 12 // * Neither the name of Google Inc. nor the names of its 13 // contributors may be used to endorse or promote products derived 14 // from this software without specific prior written permission. 15 // 16 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 17 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 18 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 19 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 20 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 21 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 22 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 23 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 24 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 25 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 26 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 27 28 #include <stdlib.h> 29 30 #include "v8.h" 31 32 #include "platform.h" 33 #include "cctest.h" 34 #include "double.h" 35 #include "fixed-dtoa.h" 36 #include "gay-fixed.h" 37 38 using namespace v8::internal; 39 40 static const int kBufferSize = 500; 41 42 TEST(FastFixedVariousDoubles) { 43 char buffer_container[kBufferSize]; 44 Vector<char> buffer(buffer_container, kBufferSize); 45 int length; 46 int point; 47 48 CHECK(FastFixedDtoa(1.0, 1, buffer, &length, &point)); 49 CHECK_EQ("1", buffer.start()); 50 CHECK_EQ(1, point); 51 52 CHECK(FastFixedDtoa(1.0, 15, buffer, &length, &point)); 53 CHECK_EQ("1", buffer.start()); 54 CHECK_EQ(1, point); 55 56 CHECK(FastFixedDtoa(1.0, 0, buffer, &length, &point)); 57 CHECK_EQ("1", buffer.start()); 58 CHECK_EQ(1, point); 59 60 CHECK(FastFixedDtoa(0xFFFFFFFF, 5, buffer, &length, &point)); 61 CHECK_EQ("4294967295", buffer.start()); 62 CHECK_EQ(10, point); 63 64 CHECK(FastFixedDtoa(4294967296.0, 5, buffer, &length, &point)); 65 CHECK_EQ("4294967296", buffer.start()); 66 CHECK_EQ(10, point); 67 68 CHECK(FastFixedDtoa(1e21, 5, buffer, &length, &point)); 69 CHECK_EQ("1", buffer.start()); 70 // CHECK_EQ(22, point); 71 CHECK_EQ(22, point); 72 73 CHECK(FastFixedDtoa(999999999999999868928.00, 2, buffer, &length, &point)); 74 CHECK_EQ("999999999999999868928", buffer.start()); 75 CHECK_EQ(21, point); 76 77 CHECK(FastFixedDtoa(6.9999999999999989514240000e+21, 5, buffer, 78 &length, &point)); 79 CHECK_EQ("6999999999999998951424", buffer.start()); 80 CHECK_EQ(22, point); 81 82 CHECK(FastFixedDtoa(1.5, 5, buffer, &length, &point)); 83 CHECK_EQ("15", buffer.start()); 84 CHECK_EQ(1, point); 85 86 CHECK(FastFixedDtoa(1.55, 5, buffer, &length, &point)); 87 CHECK_EQ("155", buffer.start()); 88 CHECK_EQ(1, point); 89 90 CHECK(FastFixedDtoa(1.55, 1, buffer, &length, &point)); 91 CHECK_EQ("16", buffer.start()); 92 CHECK_EQ(1, point); 93 94 CHECK(FastFixedDtoa(1.00000001, 15, buffer, &length, &point)); 95 CHECK_EQ("100000001", buffer.start()); 96 CHECK_EQ(1, point); 97 98 CHECK(FastFixedDtoa(0.1, 10, buffer, &length, &point)); 99 CHECK_EQ("1", buffer.start()); 100 CHECK_EQ(0, point); 101 102 CHECK(FastFixedDtoa(0.01, 10, buffer, &length, &point)); 103 CHECK_EQ("1", buffer.start()); 104 CHECK_EQ(-1, point); 105 106 CHECK(FastFixedDtoa(0.001, 10, buffer, &length, &point)); 107 CHECK_EQ("1", buffer.start()); 108 CHECK_EQ(-2, point); 109 110 CHECK(FastFixedDtoa(0.0001, 10, buffer, &length, &point)); 111 CHECK_EQ("1", buffer.start()); 112 CHECK_EQ(-3, point); 113 114 CHECK(FastFixedDtoa(0.00001, 10, buffer, &length, &point)); 115 CHECK_EQ("1", buffer.start()); 116 CHECK_EQ(-4, point); 117 118 CHECK(FastFixedDtoa(0.000001, 10, buffer, &length, &point)); 119 CHECK_EQ("1", buffer.start()); 120 CHECK_EQ(-5, point); 121 122 CHECK(FastFixedDtoa(0.0000001, 10, buffer, &length, &point)); 123 CHECK_EQ("1", buffer.start()); 124 CHECK_EQ(-6, point); 125 126 CHECK(FastFixedDtoa(0.00000001, 10, buffer, &length, &point)); 127 CHECK_EQ("1", buffer.start()); 128 CHECK_EQ(-7, point); 129 130 CHECK(FastFixedDtoa(0.000000001, 10, buffer, &length, &point)); 131 CHECK_EQ("1", buffer.start()); 132 CHECK_EQ(-8, point); 133 134 CHECK(FastFixedDtoa(0.0000000001, 15, buffer, &length, &point)); 135 CHECK_EQ("1", buffer.start()); 136 CHECK_EQ(-9, point); 137 138 CHECK(FastFixedDtoa(0.00000000001, 15, buffer, &length, &point)); 139 CHECK_EQ("1", buffer.start()); 140 CHECK_EQ(-10, point); 141 142 CHECK(FastFixedDtoa(0.000000000001, 15, buffer, &length, &point)); 143 CHECK_EQ("1", buffer.start()); 144 CHECK_EQ(-11, point); 145 146 CHECK(FastFixedDtoa(0.0000000000001, 15, buffer, &length, &point)); 147 CHECK_EQ("1", buffer.start()); 148 CHECK_EQ(-12, point); 149 150 CHECK(FastFixedDtoa(0.00000000000001, 15, buffer, &length, &point)); 151 CHECK_EQ("1", buffer.start()); 152 CHECK_EQ(-13, point); 153 154 CHECK(FastFixedDtoa(0.000000000000001, 20, buffer, &length, &point)); 155 CHECK_EQ("1", buffer.start()); 156 CHECK_EQ(-14, point); 157 158 CHECK(FastFixedDtoa(0.0000000000000001, 20, buffer, &length, &point)); 159 CHECK_EQ("1", buffer.start()); 160 CHECK_EQ(-15, point); 161 162 CHECK(FastFixedDtoa(0.00000000000000001, 20, buffer, &length, &point)); 163 CHECK_EQ("1", buffer.start()); 164 CHECK_EQ(-16, point); 165 166 CHECK(FastFixedDtoa(0.000000000000000001, 20, buffer, &length, &point)); 167 CHECK_EQ("1", buffer.start()); 168 CHECK_EQ(-17, point); 169 170 CHECK(FastFixedDtoa(0.0000000000000000001, 20, buffer, &length, &point)); 171 CHECK_EQ("1", buffer.start()); 172 CHECK_EQ(-18, point); 173 174 CHECK(FastFixedDtoa(0.00000000000000000001, 20, buffer, &length, &point)); 175 CHECK_EQ("1", buffer.start()); 176 CHECK_EQ(-19, point); 177 178 CHECK(FastFixedDtoa(0.10000000004, 10, buffer, &length, &point)); 179 CHECK_EQ("1", buffer.start()); 180 CHECK_EQ(0, point); 181 182 CHECK(FastFixedDtoa(0.01000000004, 10, buffer, &length, &point)); 183 CHECK_EQ("1", buffer.start()); 184 CHECK_EQ(-1, point); 185 186 CHECK(FastFixedDtoa(0.00100000004, 10, buffer, &length, &point)); 187 CHECK_EQ("1", buffer.start()); 188 CHECK_EQ(-2, point); 189 190 CHECK(FastFixedDtoa(0.00010000004, 10, buffer, &length, &point)); 191 CHECK_EQ("1", buffer.start()); 192 CHECK_EQ(-3, point); 193 194 CHECK(FastFixedDtoa(0.00001000004, 10, buffer, &length, &point)); 195 CHECK_EQ("1", buffer.start()); 196 CHECK_EQ(-4, point); 197 198 CHECK(FastFixedDtoa(0.00000100004, 10, buffer, &length, &point)); 199 CHECK_EQ("1", buffer.start()); 200 CHECK_EQ(-5, point); 201 202 CHECK(FastFixedDtoa(0.00000010004, 10, buffer, &length, &point)); 203 CHECK_EQ("1", buffer.start()); 204 CHECK_EQ(-6, point); 205 206 CHECK(FastFixedDtoa(0.00000001004, 10, buffer, &length, &point)); 207 CHECK_EQ("1", buffer.start()); 208 CHECK_EQ(-7, point); 209 210 CHECK(FastFixedDtoa(0.00000000104, 10, buffer, &length, &point)); 211 CHECK_EQ("1", buffer.start()); 212 CHECK_EQ(-8, point); 213 214 CHECK(FastFixedDtoa(0.0000000001000004, 15, buffer, &length, &point)); 215 CHECK_EQ("1", buffer.start()); 216 CHECK_EQ(-9, point); 217 218 CHECK(FastFixedDtoa(0.0000000000100004, 15, buffer, &length, &point)); 219 CHECK_EQ("1", buffer.start()); 220 CHECK_EQ(-10, point); 221 222 CHECK(FastFixedDtoa(0.0000000000010004, 15, buffer, &length, &point)); 223 CHECK_EQ("1", buffer.start()); 224 CHECK_EQ(-11, point); 225 226 CHECK(FastFixedDtoa(0.0000000000001004, 15, buffer, &length, &point)); 227 CHECK_EQ("1", buffer.start()); 228 CHECK_EQ(-12, point); 229 230 CHECK(FastFixedDtoa(0.0000000000000104, 15, buffer, &length, &point)); 231 CHECK_EQ("1", buffer.start()); 232 CHECK_EQ(-13, point); 233 234 CHECK(FastFixedDtoa(0.000000000000001000004, 20, buffer, &length, &point)); 235 CHECK_EQ("1", buffer.start()); 236 CHECK_EQ(-14, point); 237 238 CHECK(FastFixedDtoa(0.000000000000000100004, 20, buffer, &length, &point)); 239 CHECK_EQ("1", buffer.start()); 240 CHECK_EQ(-15, point); 241 242 CHECK(FastFixedDtoa(0.000000000000000010004, 20, buffer, &length, &point)); 243 CHECK_EQ("1", buffer.start()); 244 CHECK_EQ(-16, point); 245 246 CHECK(FastFixedDtoa(0.000000000000000001004, 20, buffer, &length, &point)); 247 CHECK_EQ("1", buffer.start()); 248 CHECK_EQ(-17, point); 249 250 CHECK(FastFixedDtoa(0.000000000000000000104, 20, buffer, &length, &point)); 251 CHECK_EQ("1", buffer.start()); 252 CHECK_EQ(-18, point); 253 254 CHECK(FastFixedDtoa(0.000000000000000000014, 20, buffer, &length, &point)); 255 CHECK_EQ("1", buffer.start()); 256 CHECK_EQ(-19, point); 257 258 CHECK(FastFixedDtoa(0.10000000006, 10, buffer, &length, &point)); 259 CHECK_EQ("1000000001", buffer.start()); 260 CHECK_EQ(0, point); 261 262 CHECK(FastFixedDtoa(0.01000000006, 10, buffer, &length, &point)); 263 CHECK_EQ("100000001", buffer.start()); 264 CHECK_EQ(-1, point); 265 266 CHECK(FastFixedDtoa(0.00100000006, 10, buffer, &length, &point)); 267 CHECK_EQ("10000001", buffer.start()); 268 CHECK_EQ(-2, point); 269 270 CHECK(FastFixedDtoa(0.00010000006, 10, buffer, &length, &point)); 271 CHECK_EQ("1000001", buffer.start()); 272 CHECK_EQ(-3, point); 273 274 CHECK(FastFixedDtoa(0.00001000006, 10, buffer, &length, &point)); 275 CHECK_EQ("100001", buffer.start()); 276 CHECK_EQ(-4, point); 277 278 CHECK(FastFixedDtoa(0.00000100006, 10, buffer, &length, &point)); 279 CHECK_EQ("10001", buffer.start()); 280 CHECK_EQ(-5, point); 281 282 CHECK(FastFixedDtoa(0.00000010006, 10, buffer, &length, &point)); 283 CHECK_EQ("1001", buffer.start()); 284 CHECK_EQ(-6, point); 285 286 CHECK(FastFixedDtoa(0.00000001006, 10, buffer, &length, &point)); 287 CHECK_EQ("101", buffer.start()); 288 CHECK_EQ(-7, point); 289 290 CHECK(FastFixedDtoa(0.00000000106, 10, buffer, &length, &point)); 291 CHECK_EQ("11", buffer.start()); 292 CHECK_EQ(-8, point); 293 294 CHECK(FastFixedDtoa(0.0000000001000006, 15, buffer, &length, &point)); 295 CHECK_EQ("100001", buffer.start()); 296 CHECK_EQ(-9, point); 297 298 CHECK(FastFixedDtoa(0.0000000000100006, 15, buffer, &length, &point)); 299 CHECK_EQ("10001", buffer.start()); 300 CHECK_EQ(-10, point); 301 302 CHECK(FastFixedDtoa(0.0000000000010006, 15, buffer, &length, &point)); 303 CHECK_EQ("1001", buffer.start()); 304 CHECK_EQ(-11, point); 305 306 CHECK(FastFixedDtoa(0.0000000000001006, 15, buffer, &length, &point)); 307 CHECK_EQ("101", buffer.start()); 308 CHECK_EQ(-12, point); 309 310 CHECK(FastFixedDtoa(0.0000000000000106, 15, buffer, &length, &point)); 311 CHECK_EQ("11", buffer.start()); 312 CHECK_EQ(-13, point); 313 314 CHECK(FastFixedDtoa(0.000000000000001000006, 20, buffer, &length, &point)); 315 CHECK_EQ("100001", buffer.start()); 316 CHECK_EQ(-14, point); 317 318 CHECK(FastFixedDtoa(0.000000000000000100006, 20, buffer, &length, &point)); 319 CHECK_EQ("10001", buffer.start()); 320 CHECK_EQ(-15, point); 321 322 CHECK(FastFixedDtoa(0.000000000000000010006, 20, buffer, &length, &point)); 323 CHECK_EQ("1001", buffer.start()); 324 CHECK_EQ(-16, point); 325 326 CHECK(FastFixedDtoa(0.000000000000000001006, 20, buffer, &length, &point)); 327 CHECK_EQ("101", buffer.start()); 328 CHECK_EQ(-17, point); 329 330 CHECK(FastFixedDtoa(0.000000000000000000106, 20, buffer, &length, &point)); 331 CHECK_EQ("11", buffer.start()); 332 CHECK_EQ(-18, point); 333 334 CHECK(FastFixedDtoa(0.000000000000000000016, 20, buffer, &length, &point)); 335 CHECK_EQ("2", buffer.start()); 336 CHECK_EQ(-19, point); 337 338 CHECK(FastFixedDtoa(0.6, 0, buffer, &length, &point)); 339 CHECK_EQ("1", buffer.start()); 340 CHECK_EQ(1, point); 341 342 CHECK(FastFixedDtoa(0.96, 1, buffer, &length, &point)); 343 CHECK_EQ("1", buffer.start()); 344 CHECK_EQ(1, point); 345 346 CHECK(FastFixedDtoa(0.996, 2, buffer, &length, &point)); 347 CHECK_EQ("1", buffer.start()); 348 CHECK_EQ(1, point); 349 350 CHECK(FastFixedDtoa(0.9996, 3, buffer, &length, &point)); 351 CHECK_EQ("1", buffer.start()); 352 CHECK_EQ(1, point); 353 354 CHECK(FastFixedDtoa(0.99996, 4, buffer, &length, &point)); 355 CHECK_EQ("1", buffer.start()); 356 CHECK_EQ(1, point); 357 358 CHECK(FastFixedDtoa(0.999996, 5, buffer, &length, &point)); 359 CHECK_EQ("1", buffer.start()); 360 CHECK_EQ(1, point); 361 362 CHECK(FastFixedDtoa(0.9999996, 6, buffer, &length, &point)); 363 CHECK_EQ("1", buffer.start()); 364 CHECK_EQ(1, point); 365 366 CHECK(FastFixedDtoa(0.99999996, 7, buffer, &length, &point)); 367 CHECK_EQ("1", buffer.start()); 368 CHECK_EQ(1, point); 369 370 CHECK(FastFixedDtoa(0.999999996, 8, buffer, &length, &point)); 371 CHECK_EQ("1", buffer.start()); 372 CHECK_EQ(1, point); 373 374 CHECK(FastFixedDtoa(0.9999999996, 9, buffer, &length, &point)); 375 CHECK_EQ("1", buffer.start()); 376 CHECK_EQ(1, point); 377 378 CHECK(FastFixedDtoa(0.99999999996, 10, buffer, &length, &point)); 379 CHECK_EQ("1", buffer.start()); 380 CHECK_EQ(1, point); 381 382 CHECK(FastFixedDtoa(0.999999999996, 11, buffer, &length, &point)); 383 CHECK_EQ("1", buffer.start()); 384 CHECK_EQ(1, point); 385 386 CHECK(FastFixedDtoa(0.9999999999996, 12, buffer, &length, &point)); 387 CHECK_EQ("1", buffer.start()); 388 CHECK_EQ(1, point); 389 390 CHECK(FastFixedDtoa(0.99999999999996, 13, buffer, &length, &point)); 391 CHECK_EQ("1", buffer.start()); 392 CHECK_EQ(1, point); 393 394 CHECK(FastFixedDtoa(0.999999999999996, 14, buffer, &length, &point)); 395 CHECK_EQ("1", buffer.start()); 396 CHECK_EQ(1, point); 397 398 CHECK(FastFixedDtoa(0.9999999999999996, 15, buffer, &length, &point)); 399 CHECK_EQ("1", buffer.start()); 400 CHECK_EQ(1, point); 401 402 CHECK(FastFixedDtoa(0.00999999999999996, 16, buffer, &length, &point)); 403 CHECK_EQ("1", buffer.start()); 404 CHECK_EQ(-1, point); 405 406 CHECK(FastFixedDtoa(0.000999999999999996, 17, buffer, &length, &point)); 407 CHECK_EQ("1", buffer.start()); 408 CHECK_EQ(-2, point); 409 410 CHECK(FastFixedDtoa(0.0000999999999999996, 18, buffer, &length, &point)); 411 CHECK_EQ("1", buffer.start()); 412 CHECK_EQ(-3, point); 413 414 CHECK(FastFixedDtoa(0.00000999999999999996, 19, buffer, &length, &point)); 415 CHECK_EQ("1", buffer.start()); 416 CHECK_EQ(-4, point); 417 418 CHECK(FastFixedDtoa(0.000000999999999999996, 20, buffer, &length, &point)); 419 CHECK_EQ("1", buffer.start()); 420 CHECK_EQ(-5, point); 421 422 CHECK(FastFixedDtoa(323423.234234, 10, buffer, &length, &point)); 423 CHECK_EQ("323423234234", buffer.start()); 424 CHECK_EQ(6, point); 425 426 CHECK(FastFixedDtoa(12345678.901234, 4, buffer, &length, &point)); 427 CHECK_EQ("123456789012", buffer.start()); 428 CHECK_EQ(8, point); 429 430 CHECK(FastFixedDtoa(98765.432109, 5, buffer, &length, &point)); 431 CHECK_EQ("9876543211", buffer.start()); 432 CHECK_EQ(5, point); 433 434 CHECK(FastFixedDtoa(42, 20, buffer, &length, &point)); 435 CHECK_EQ("42", buffer.start()); 436 CHECK_EQ(2, point); 437 438 CHECK(FastFixedDtoa(0.5, 0, buffer, &length, &point)); 439 CHECK_EQ("1", buffer.start()); 440 CHECK_EQ(1, point); 441 442 CHECK(FastFixedDtoa(1e-23, 10, buffer, &length, &point)); 443 CHECK_EQ("", buffer.start()); 444 CHECK_EQ(-10, point); 445 446 CHECK(FastFixedDtoa(1e-123, 2, buffer, &length, &point)); 447 CHECK_EQ("", buffer.start()); 448 CHECK_EQ(-2, point); 449 450 CHECK(FastFixedDtoa(1e-123, 0, buffer, &length, &point)); 451 CHECK_EQ("", buffer.start()); 452 CHECK_EQ(0, point); 453 454 CHECK(FastFixedDtoa(1e-23, 20, buffer, &length, &point)); 455 CHECK_EQ("", buffer.start()); 456 CHECK_EQ(-20, point); 457 458 CHECK(FastFixedDtoa(1e-21, 20, buffer, &length, &point)); 459 CHECK_EQ("", buffer.start()); 460 CHECK_EQ(-20, point); 461 462 CHECK(FastFixedDtoa(1e-22, 20, buffer, &length, &point)); 463 CHECK_EQ("", buffer.start()); 464 CHECK_EQ(-20, point); 465 466 CHECK(FastFixedDtoa(6e-21, 20, buffer, &length, &point)); 467 CHECK_EQ("1", buffer.start()); 468 CHECK_EQ(-19, point); 469 470 CHECK(FastFixedDtoa(9.1193616301674545152000000e+19, 0, 471 buffer, &length, &point)); 472 CHECK_EQ("91193616301674545152", buffer.start()); 473 CHECK_EQ(20, point); 474 475 CHECK(FastFixedDtoa(4.8184662102767651659096515e-04, 19, 476 buffer, &length, &point)); 477 CHECK_EQ("4818466210276765", buffer.start()); 478 CHECK_EQ(-3, point); 479 480 CHECK(FastFixedDtoa(1.9023164229540652612705182e-23, 8, 481 buffer, &length, &point)); 482 CHECK_EQ("", buffer.start()); 483 CHECK_EQ(-8, point); 484 485 CHECK(FastFixedDtoa(1000000000000000128.0, 0, 486 buffer, &length, &point)); 487 CHECK_EQ("1000000000000000128", buffer.start()); 488 CHECK_EQ(19, point); 489 } 490 491 492 TEST(FastFixedDtoaGayFixed) { 493 char buffer_container[kBufferSize]; 494 Vector<char> buffer(buffer_container, kBufferSize); 495 bool status; 496 int length; 497 int point; 498 499 Vector<const PrecomputedFixed> precomputed = 500 PrecomputedFixedRepresentations(); 501 for (int i = 0; i < precomputed.length(); ++i) { 502 const PrecomputedFixed current_test = precomputed[i]; 503 double v = current_test.v; 504 int number_digits = current_test.number_digits; 505 status = FastFixedDtoa(v, number_digits, 506 buffer, &length, &point); 507 CHECK(status); 508 CHECK_EQ(current_test.decimal_point, point); 509 CHECK_GE(number_digits, length - point); 510 CHECK_EQ(current_test.representation, buffer.start()); 511 } 512 } 513