BasicLooping.js 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. //-------------------------------------------------------------------------------------------------------
  2. // Copyright (C) Microsoft. All rights reserved.
  3. // Licensed under the MIT license. See LICENSE.txt file in the project root for full license information.
  4. //-------------------------------------------------------------------------------------------------------
  5. function foo() {}
  6. var all = [ undefined, null,
  7. true, false, new Boolean(true), new Boolean(false),
  8. NaN, +0, -0, 0, 1, 10.0, 10.1, -1, -5, 5,
  9. 124, 248, 654, 987, -1026, +98768.2546, -88754.15478,
  10. 1<<32, -(1<<32), (1<<32)-1, 1<<31, -(1<<31), 1<<25, -1<<25,
  11. Number.MAX_VALUE, Number.MIN_VALUE, Number.NaN, Number.POSITIVE_INFINITY, Number.NEGATIVE_INFINITY,
  12. new Number(NaN), new Number(+0), new Number( -0), new Number(0), new Number(1),
  13. new Number(10.0), new Number(10.1),
  14. new Number(Number.MAX_VALUE), new Number(Number.MIN_VALUE), new Number(Number.NaN),
  15. new Number(Number.POSITIVE_INFINITY), new Number(Number.NEGATIVE_INFINITY),
  16. "", "hello", "hel" + "lo", "+0", "-0", "0", "1", "10.0", "10.1",
  17. new String(""), new String("hello"), new String("he" + "llo"),
  18. new Object(), [1,2,3], new Object(), [1,2,3] , foo
  19. ];
  20. function AsmModule() {
  21. "use asm";
  22. function sum(x,y) {
  23. x = x|0;
  24. y = +y;
  25. if( (x|0) > 10){
  26. x = 10;
  27. }
  28. while( (x|0) > 0){
  29. x = (x - 1)|0;
  30. y = +(y + 1.0);
  31. if( (x|0) == 0 ){
  32. break;
  33. }
  34. }
  35. return +y;
  36. }
  37. function sumWhile(x,y) {
  38. x = x|0;
  39. y = +y;
  40. if( (x|0) > 10){
  41. x = 10;
  42. }
  43. do{
  44. x = (x - 1)|0;
  45. if((x|0) > 10){
  46. x = 10;
  47. }
  48. y = +(y + 1.0);
  49. if( (x|0) == 0 ){
  50. break;
  51. }
  52. }
  53. while( (x|0) > 0 );
  54. return +y;
  55. }
  56. function forLoop(x,y) {
  57. x = x|0;
  58. y = y|0;
  59. var i=0;
  60. if( (x|0) > 10){
  61. x = 10;
  62. }
  63. if( (y|0) <= 0){
  64. y = 1;
  65. }
  66. for(; (i|0) < (x|0) ; i = (i+1)|0) {
  67. y = (y + 1)|0;
  68. }
  69. for(i=0; (i|0) < (x|0) ; i = (i+1)|0 ) {
  70. y = (y + 2)|0;
  71. }
  72. for(;;){
  73. y = y << 1;
  74. if( (y|0) > (1<<20)) {
  75. break;
  76. }
  77. }
  78. return y|0;
  79. }
  80. return {
  81. f1 : sum,
  82. f2 : sumWhile,
  83. f3 : forLoop,
  84. };
  85. }
  86. var asmModule = AsmModule();
  87. for (var i=0; i<all.length; ++i) {
  88. for (var j=0; j<all.length; ++j) {
  89. print("f1 a["+i+"](" + all[i] +") , a["+j+"]("+all[j]+") = " + (asmModule.f1(all[i],all[j])));
  90. print("f2 a["+i+"](" + all[i] +") , a["+j+"]("+all[j]+") = " + (asmModule.f2(all[i],all[j])));
  91. print("f3 a["+i+"](" + all[i] +") , a["+j+"]("+all[j]+") = " + (asmModule.f3(all[i],all[j])));
  92. }
  93. }