math-cordic.html 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. <!DOCTYPE html>
  2. <head>
  3. <!--
  4. Copyright (C) 2007 Apple Inc. All rights reserved.
  5. Redistribution and use in source and binary forms, with or without
  6. modification, are permitted provided that the following conditions
  7. are met:
  8. 1. Redistributions of source code must retain the above copyright
  9. notice, this list of conditions and the following disclaimer.
  10. 2. Redistributions in binary form must reproduce the above copyright
  11. notice, this list of conditions and the following disclaimer in the
  12. documentation and/or other materials provided with the distribution.
  13. THIS SOFTWARE IS PROVIDED BY APPLE COMPUTER, INC. ``AS IS'' AND ANY
  14. EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  15. IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  16. PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE COMPUTER, INC. OR
  17. CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
  18. EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
  19. PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
  20. PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
  21. OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  22. (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  23. OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  24. -->
  25. <title>SunSpider math-cordic</title>
  26. </head>
  27. <body>
  28. <h3>math-cordic</h3>
  29. <div id="console">
  30. </div>
  31. <script>
  32. var _sunSpiderStartDate = new Date();
  33. /*
  34. * Copyright (C) Rich Moore. All rights reserved.
  35. *
  36. * Redistribution and use in source and binary forms, with or without
  37. * modification, are permitted provided that the following conditions
  38. * are met:
  39. * 1. Redistributions of source code must retain the above copyright
  40. * notice, this list of conditions and the following disclaimer.
  41. * 2. Redistributions in binary form must reproduce the above copyright
  42. * notice, this list of conditions and the following disclaimer in the
  43. * documentation and/or other materials provided with the distribution.
  44. *
  45. * THIS SOFTWARE IS PROVIDED BY CONTRIBUTORS ``AS IS'' AND ANY
  46. * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  47. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  48. * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE COMPUTER, INC. OR
  49. * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
  50. * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
  51. * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
  52. * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
  53. * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  54. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  55. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  56. */
  57. /////. Start CORDIC
  58. var AG_CONST = 0.6072529350;
  59. function FIXED(X)
  60. {
  61. return X * 65536.0;
  62. }
  63. function FLOAT(X)
  64. {
  65. return X / 65536.0;
  66. }
  67. function DEG2RAD(X)
  68. {
  69. return 0.017453 * (X);
  70. }
  71. var Angles = [
  72. FIXED(45.0), FIXED(26.565), FIXED(14.0362), FIXED(7.12502),
  73. FIXED(3.57633), FIXED(1.78991), FIXED(0.895174), FIXED(0.447614),
  74. FIXED(0.223811), FIXED(0.111906), FIXED(0.055953),
  75. FIXED(0.027977)
  76. ];
  77. function cordicsincos() {
  78. var X;
  79. var Y;
  80. var TargetAngle;
  81. var CurrAngle;
  82. var Step;
  83. X = FIXED(AG_CONST); /* AG_CONST * cos(0) */
  84. Y = 0; /* AG_CONST * sin(0) */
  85. TargetAngle = FIXED(28.027);
  86. CurrAngle = 0;
  87. for (Step = 0; Step < 12; Step++) {
  88. var NewX;
  89. if (TargetAngle > CurrAngle) {
  90. NewX = X - (Y >> Step);
  91. Y = (X >> Step) + Y;
  92. X = NewX;
  93. CurrAngle += Angles[Step];
  94. } else {
  95. NewX = X + (Y >> Step);
  96. Y = -(X >> Step) + Y;
  97. X = NewX;
  98. CurrAngle -= Angles[Step];
  99. }
  100. }
  101. }
  102. ///// End CORDIC
  103. function cordic( runs ) {
  104. var start = new Date();
  105. for ( var i = 0 ; i < runs ; i++ ) {
  106. cordicsincos();
  107. }
  108. var end = new Date();
  109. return end.getTime() - start.getTime();
  110. }
  111. cordic(25000);
  112. var _sunSpiderInterval = new Date() - _sunSpiderStartDate;
  113. document.getElementById("console").innerHTML = _sunSpiderInterval;
  114. </script>
  115. </body>
  116. </html>