/examples.js

https://github.com/mbebenita/WasmExplorer · JavaScript · 104 lines · 89 code · 10 blank · 5 comment · 5 complexity · 34fbb60b614d54d3758bbe95fa650bd7 MD5 · raw file

  1. var cppExamples = {
  2. "Q_rsqrt": `float Q_rsqrt(float number) {
  3. long i;
  4. float x2, y;
  5. const float threehalfs = 1.5F;
  6. x2 = number * 0.5F;
  7. y = number;
  8. i = *(long *) &y;
  9. i = 0x5f3759df - (i >> 1);
  10. y = *(float *) &i;
  11. y = y * (threehalfs - (x2 * y * y));
  12. y = y * (threehalfs - (x2 * y * y));
  13. return y;
  14. }`,
  15. "testFunction": `int testFunction(int* input, int length) {
  16. int sum = 0;
  17. for (int i = 0; i < length; ++i) {
  18. sum += input[i];
  19. }
  20. return sum;
  21. }`,
  22. "fact": `double fact(int i) {
  23. long long n = 1;
  24. for (;i > 0; i--) {
  25. n *= i;
  26. }
  27. return (double)n;
  28. }`,
  29. "virtual": `struct A {
  30. A();
  31. ~A();
  32. virtual void virtual_member_function();
  33. };
  34. A *ctor() {
  35. return new A();
  36. }
  37. void dtor(A *a) {
  38. delete a;
  39. }
  40. void call_member_function(A *a) {
  41. a->virtual_member_function();
  42. }`,
  43. "popcnt": `int main(int a) {
  44. return __builtin_popcount(a) +
  45. __builtin_popcount(a);
  46. }
  47. int count(unsigned int x) {
  48. int v = 0;
  49. while(x != 0) {
  50. x &= x - 1;
  51. v++;
  52. }
  53. return v;
  54. }
  55. `,"fast-math": `// compile with/without -ffast-math
  56. double foo(double d) {
  57. return d / 3.0;
  58. }
  59. double maybe_min(double d, double e) {
  60. return d < e ? d : e;
  61. }
  62. double pow(double x, double y);
  63. double call_pow(double x) {
  64. return pow(x, 8);
  65. }
  66. double do_pow(double x) {
  67. return x*x*x*x*x*x*x*x;
  68. }
  69. double factor(double a, double b, double c) {
  70. return (a * c) + (b * c);
  71. }
  72. `, "duff": `/**
  73. More expressive control flow constructs are needed to
  74. implement Duff's device effectively.
  75. See:
  76. https://github.com/WebAssembly/design/blob/master/FutureFeatures.md#more-expressive-control-flow
  77. */
  78. void send(char *to, char *from, unsigned long count)
  79. {
  80. unsigned long n = (count + 7) / 8;
  81. switch (count % 8) {
  82. case 0: do { *to++ = *from++;
  83. case 7: *to++ = *from++;
  84. case 6: *to++ = *from++;
  85. case 5: *to++ = *from++;
  86. case 4: *to++ = *from++;
  87. case 3: *to++ = *from++;
  88. case 2: *to++ = *from++;
  89. case 1: *to++ = *from++;
  90. } while (--n > 0);
  91. }
  92. }
  93. `
  94. }