PageRenderTime 131ms CodeModel.GetById 16ms RepoModel.GetById 0ms app.codeStats 0ms

/test/mjsunit/math-round.js

http://github.com/v8/v8
JavaScript | 194 lines | 141 code | 16 blank | 37 comment | 1 complexity | b71614cda5d2c98a95ed46bc24f94bae MD5 | raw file
Possible License(s): BSD-3-Clause, CC0-1.0, Apache-2.0
  1. // Copyright 2011 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. // Flags: --allow-natives-syntax
  28. var test_id = 0;
  29. function testRound(expect, input) {
  30. // Make source code different on each invocation to make
  31. // sure it gets optimized each time.
  32. var doRound = new Function('input',
  33. '"' + (test_id++) + '";return Math.round(input)');
  34. %PrepareFunctionForOptimization(doRound);
  35. assertEquals(expect, doRound(input));
  36. assertEquals(expect, doRound(input));
  37. assertEquals(expect, doRound(input));
  38. %OptimizeFunctionOnNextCall(doRound);
  39. assertEquals(expect, doRound(input));
  40. // Force the Math.round() representation to double to exercise the associated
  41. // optimized code.
  42. var doRoundToDouble = new Function('input',
  43. '"' + (test_id++) + '";return Math.round(input) + -0.0');
  44. %PrepareFunctionForOptimization(doRoundToDouble);
  45. assertEquals(expect, doRoundToDouble(input));
  46. assertEquals(expect, doRoundToDouble(input));
  47. assertEquals(expect, doRoundToDouble(input));
  48. %OptimizeFunctionOnNextCall(doRoundToDouble);
  49. assertEquals(expect, doRoundToDouble(input));
  50. }
  51. testRound(0, 0);
  52. testRound(-0, -0);
  53. testRound(Infinity, Infinity);
  54. testRound(-Infinity, -Infinity);
  55. testRound(NaN, NaN);
  56. // Regression test for a bug where a negative zero coming from Math.round
  57. // was not properly handled by other operations.
  58. function roundsum(i, n) {
  59. var ret = Math.round(n);
  60. while (--i > 0) {
  61. ret += Math.round(n);
  62. }
  63. return ret;
  64. };
  65. %PrepareFunctionForOptimization(roundsum);
  66. assertEquals(-0, roundsum(1, -0));
  67. %OptimizeFunctionOnNextCall(roundsum);
  68. // The optimized function will deopt. Run it with enough iterations to try
  69. // to optimize via OSR (triggering the bug).
  70. assertEquals(-0, roundsum(100000, -0));
  71. testRound(1, 0.5);
  72. testRound(1, 0.7);
  73. testRound(1, 1);
  74. testRound(1, 1.1);
  75. testRound(1, 1.49999);
  76. testRound(-0, -0.5);
  77. testRound(-1, -0.5000000000000001);
  78. testRound(-1, -0.7);
  79. testRound(-1, -1);
  80. testRound(-1, -1.1);
  81. testRound(-1, -1.49999);
  82. testRound(-1, -1.5);
  83. testRound(9007199254740990, 9007199254740990);
  84. testRound(9007199254740991, 9007199254740991);
  85. testRound(-9007199254740990, -9007199254740990);
  86. testRound(-9007199254740991, -9007199254740991);
  87. testRound(Number.MAX_VALUE, Number.MAX_VALUE);
  88. testRound(-Number.MAX_VALUE, -Number.MAX_VALUE);
  89. testRound(Number.MAX_SAFE_INTEGER, Number.MAX_SAFE_INTEGER);
  90. testRound(Number.MAX_SAFE_INTEGER + 1, Number.MAX_SAFE_INTEGER + 1);
  91. testRound(Number.MAX_SAFE_INTEGER + 2, Number.MAX_SAFE_INTEGER + 2);
  92. testRound(Number.MAX_SAFE_INTEGER + 3, Number.MAX_SAFE_INTEGER + 3);
  93. testRound(Number.MAX_SAFE_INTEGER + 4, Number.MAX_SAFE_INTEGER + 4);
  94. testRound(Number.MIN_SAFE_INTEGER, Number.MIN_SAFE_INTEGER);
  95. testRound(Number.MIN_SAFE_INTEGER - 1, Number.MIN_SAFE_INTEGER - 1);
  96. testRound(Number.MIN_SAFE_INTEGER - 2, Number.MIN_SAFE_INTEGER - 2);
  97. testRound(Number.MIN_SAFE_INTEGER - 3, Number.MIN_SAFE_INTEGER - 3);
  98. testRound(536870911, 536870910.5);
  99. testRound(536870911, 536870911);
  100. testRound(536870911, 536870911.4);
  101. testRound(536870912, 536870911.5);
  102. testRound(536870912, 536870912);
  103. testRound(536870912, 536870912.4);
  104. testRound(536870913, 536870912.5);
  105. testRound(536870913, 536870913);
  106. testRound(536870913, 536870913.4);
  107. testRound(1073741823, 1073741822.5);
  108. testRound(1073741823, 1073741823);
  109. testRound(1073741823, 1073741823.4);
  110. testRound(1073741824, 1073741823.5);
  111. testRound(1073741824, 1073741824);
  112. testRound(1073741824, 1073741824.4);
  113. testRound(1073741825, 1073741824.5);
  114. testRound(2147483647, 2147483646.5);
  115. testRound(2147483647, 2147483647);
  116. testRound(2147483647, 2147483647.4);
  117. testRound(2147483648, 2147483647.5);
  118. testRound(2147483648, 2147483648);
  119. testRound(2147483648, 2147483648.4);
  120. testRound(2147483649, 2147483648.5);
  121. // Tests based on WebKit LayoutTests
  122. testRound(0, 0.4);
  123. testRound(-0, -0.4);
  124. testRound(-0, -0.5);
  125. testRound(1, 0.6);
  126. testRound(-1, -0.6);
  127. testRound(2, 1.5);
  128. testRound(2, 1.6);
  129. testRound(-2, -1.6);
  130. testRound(8640000000000000, 8640000000000000);
  131. testRound(8640000000000001, 8640000000000001);
  132. testRound(8640000000000002, 8640000000000002);
  133. testRound(9007199254740990, 9007199254740990);
  134. testRound(9007199254740991, 9007199254740991);
  135. testRound(1.7976931348623157e+308, 1.7976931348623157e+308);
  136. testRound(-8640000000000000, -8640000000000000);
  137. testRound(-8640000000000001, -8640000000000001);
  138. testRound(-8640000000000002, -8640000000000002);
  139. testRound(-9007199254740990, -9007199254740990);
  140. testRound(-9007199254740991, -9007199254740991);
  141. testRound(-1.7976931348623157e+308, -1.7976931348623157e+308);
  142. testRound(Infinity, Infinity);
  143. testRound(-Infinity, -Infinity);
  144. // Some special double number cases.
  145. var ulp = Math.pow(2, -1022 - 52);
  146. var max_denormal = (Math.pow(2, 52) - 1) * ulp;
  147. var min_normal = Math.pow(2, -1022);
  148. var max_fraction = Math.pow(2, 52) - 0.5;
  149. var min_nonfraction = Math.pow(2, 52);
  150. var max_non_infinite = Number.MAX_VALUE;
  151. var max_smi31 = Math.pow(2,30) - 1;
  152. var min_smi31 = -Math.pow(2,30);
  153. var max_smi32 = Math.pow(2,31) - 1;
  154. var min_smi32 = -Math.pow(2,31);
  155. testRound(0, ulp);
  156. testRound(0, max_denormal);
  157. testRound(0, min_normal);
  158. testRound(0, 0.49999999999999994);
  159. testRound(1, 0.5);
  160. testRound(Math.pow(2,52), max_fraction);
  161. testRound(min_nonfraction, min_nonfraction);
  162. testRound(max_non_infinite, max_non_infinite);
  163. testRound(max_smi31, max_smi31 - 0.5);
  164. testRound(max_smi31 + 1, max_smi31 + 0.5);
  165. testRound(max_smi32, max_smi32 - 0.5);
  166. testRound(max_smi32 + 1, max_smi32 + 0.5);
  167. testRound(-0, -ulp);
  168. testRound(-0, -max_denormal);
  169. testRound(-0, -min_normal);
  170. testRound(-0, -0.49999999999999994);
  171. testRound(-0, -0.5);
  172. testRound(-Math.pow(2,52)+1, -max_fraction);
  173. testRound(-min_nonfraction, -min_nonfraction);
  174. testRound(-max_non_infinite, -max_non_infinite);
  175. testRound(min_smi31, min_smi31 - 0.5);
  176. testRound(min_smi31 + 1, min_smi31 + 0.5);
  177. testRound(min_smi32, min_smi32 - 0.5);
  178. testRound(min_smi32 + 1, min_smi32 + 0.5);