File: test_convertI32PairToI53Checked.cpp

package info (click to toggle)
emscripten 3.1.69%2Bdfsg-3
  • links: PTS, VCS
  • area: main
  • in suites: trixie
  • size: 121,860 kB
  • sloc: ansic: 636,110; cpp: 425,974; javascript: 78,401; python: 58,404; sh: 49,154; pascal: 5,237; makefile: 3,366; asm: 2,415; lisp: 1,869
file content (79 lines) | stat: -rw-r--r-- 2,097 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
#ifdef __EMSCRIPTEN__
#include <emscripten.h>
#endif

#include "test_int53.h"

#include <stdio.h>
#include <stdlib.h>

// Uncomment to compute the expected results without testing:
//#define GENERATE_ANSWERS

EM_JS_DEPS(test, "$convertI32PairToI53Checked");

double test(int64_t val) {
  int32_t lo = (uint32_t)val;
  int32_t hi = (uint64_t)val >> 32;
  printf("input=%lld / 0x%llx: convertI32PairToI53Checked(lo=%d hi=%d)=", val, val, lo, hi);
#ifdef GENERATE_ANSWERS
  int64_t v = (uint64_t)(uint32_t)lo;
  v |= ((uint64_t)(uint32_t)hi) << 32;
  double out = v > 9007199254740992ll || v < -9007199254740992ll ? (double)NAN : v;
#else
  double out = EM_ASM_DOUBLE(return convertI32PairToI53Checked($0, $1), lo, hi);
#endif
  printf("%f\n", out);
  return out;
}

int main() {
  printf("Testing library_int53.js function $convertI32PairToI53Checked():\n\n");

  printf("Testing positive losslessIntegers:\n");
  for (uint64_t num : losslessIntegers) {
    double ret = test(num);
    if (ret != num) return 1;
  }

  printf("\nTesting negative losslessIntegers:\n");
  for (uint64_t num : losslessIntegers) {
    int64_t neg = -(int64_t)num;
    double ret = test(neg);
    if (ret != neg) return 1;
  }

  printf("\nTesting preciseUnsignedIntegers:\n");
  for (uint64_t num : preciseUnsignedIntegers) {
    double ret = test(num);
    if (!isnan(ret)) return 1;
  }

  printf("\nTesting preciseNegativeIntegers:\n");
  for (uint64_t num : preciseNegativeIntegers) {
    double ret = test(num);
    if (!isnan(ret)) return 1;
  }

  printf("\nTesting impreciseUnsignedIntegers:\n");
  for (uint64_t num : impreciseUnsignedIntegers) {
    test(num);
  }

  printf("\nTesting impreciseNegativeIntegers:\n");
  for (uint64_t num : impreciseNegativeIntegers) {
    double ret = test(num);
    if (!isnan(ret)) return 1;
  }

  printf("\nTesting otherDoubles:\n");
  for (int sign = 0; sign < 2; ++sign) {
    for (double d : otherDoubles) {
      double num = sign ? -d : d;
      uint64_t u = *(uint64_t*)&num;
      printf("Double: %f -> ", num);
      test(u);
    }
  }
  printf("All done!\n");
}