basicMathUnary.js 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  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(stdlib) {
  21. "use asm";
  22. var fround = stdlib.Math.fround;
  23. function posInt(x) {
  24. x = x|0;
  25. return +(+(x|0));
  26. }
  27. function posUInt(x) {
  28. x = x|0;
  29. return +(+(x>>>0));
  30. }
  31. function posDouble(x) {
  32. x = +x;
  33. return +(+x);
  34. }
  35. function posFloat(x) {
  36. x = fround(x);
  37. return fround(+x);
  38. }
  39. function negInt(x) {
  40. x = x|0;
  41. return (-x)|0;
  42. }
  43. function negDouble(x) {
  44. x = +x;
  45. return +(-x);
  46. }
  47. function negFloat(x) {
  48. x = fround(x);
  49. return fround(-x);
  50. }
  51. function bitnotInt(x) {
  52. x = x|0;
  53. return (~x)|0;
  54. }
  55. function bitnotnotInt(x) {
  56. x = +x;
  57. return (~~x)|0;
  58. }
  59. function lognotInt(x) {
  60. x = x|0;
  61. return (!x)|0;
  62. }
  63. function lognot2Int(x) {
  64. x = x|0;
  65. return (!!x)|0;
  66. }
  67. return {
  68. posInt : posInt ,
  69. posUInt : posUInt ,
  70. posDouble : posDouble ,
  71. posFloat : posFloat ,
  72. negInt : negInt ,
  73. negDouble : negDouble ,
  74. negFloat : negFloat ,
  75. bitnotInt : bitnotInt ,
  76. bitnotnotInt : bitnotnotInt ,
  77. lognotInt : lognotInt ,
  78. lognot2Int : lognot2Int ,
  79. };
  80. }
  81. var asmModule = AsmModule({Math:Math});
  82. for (var i=0; i<all.length; ++i) {
  83. print("i +a["+i+"](" + all[i] +") = " + (asmModule.posInt (all[i])));
  84. print("ui +a["+i+"](" + all[i] +") = " + (asmModule.posUInt (all[i])));
  85. print("d +a["+i+"](" + all[i] +") = " + (asmModule.posDouble (all[i])));
  86. print("f +a["+i+"](" + all[i] +") = " + (asmModule.posFloat (all[i])));
  87. print("i -a["+i+"](" + all[i] +") = " + (asmModule.negInt (all[i])));
  88. print("d -a["+i+"](" + all[i] +") = " + (asmModule.negDouble (all[i])));
  89. print("f -a["+i+"](" + all[i] +") = " + (asmModule.negFloat (all[i])));
  90. print("i ~a["+i+"](" + all[i] +") = " + (asmModule.bitnotInt (all[i])));
  91. print("i ~~a["+i+"](" + all[i] +") = " + (asmModule.bitnotnotInt (all[i])));
  92. print("i !a["+i+"](" + all[i] +") = " + (asmModule.lognotInt (all[i])));
  93. print("i !!a["+i+"](" + all[i] +") = " + (asmModule.lognot2Int (all[i])));
  94. }