summaryrefslogtreecommitdiffstats
path: root/runtime/tests/math/div64.c
blob: ce637a92d813e0140c49bf064dc2ff8c0f4837f8 (plain)
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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
/* test of 64-bit division */
#include "runtime.h"
#define LLONG_MAX    9223372036854775807LL
#define LLONG_MIN    (-LLONG_MAX - 1LL)

/* This tests a lot of edge conditions.*/
/* Then it does 10 million random divisions, comparing the result */
/* with the results from glibc */

int main()
{
  int64_t x, y, div1, mod1, div2, mod2;
  const char *error;
  int i;

  x = 0;
  y = 0;
  div1 = _stp_div64(&error, x, y);
  if (div1 != 0 || *error != 'd') {
    printf("Failed 0/0 test\n");
    exit(-1);
  }
  error = "";

  mod1 = _stp_mod64(&error, x, y);
  if (mod1 != 0 || *error != 'd') {
    printf("Failed 0%0 test\n");
    exit(-1);
  }
  error = "";

  x = 1;
  y = 0;
  div1 = _stp_div64(&error, x, y);
  if (div1 != 0 || *error != 'd') {
    printf("Failed 1/0 test\n");
    exit(-1);
  }
  error = "";

  mod1 = _stp_mod64(&error, x, y);
  if (mod1 != 0 || *error != 'd') {
    printf("Failed 1%0 test\n");
    exit(-1);
  }
  error = "";

  x = 0;
  y = 1;

  div1 = _stp_div64(&error, x, y);
  if (*error || div1 != 0) {
    printf("Failed 0/1 test\n");
    exit(-1);
  }

  mod1 = _stp_mod64(&error, x, y);
  if (*error || mod1 != 0) {
    printf("Failed 0%1 test\n");
    exit(-1);
  }

  x = -1;
  y = -1;

  div1 = _stp_div64(&error, x, y);
  if (*error || div1 != 1) {
    printf("Failed -1/-1 test\n");
    exit(-1);
  }

  mod1 = _stp_mod64(&error, x, y);
  if (*error || mod1 != 0) {
    printf("Failed -1%-1 test\n");
    exit(-1);
  }


  for (y = -1; y < 2; y++) {
    if (y == 0)
      continue;

    for (x = LONG_MIN - 1LL; x < LONG_MIN + 2LL; x++ ) {
      div1 = _stp_div64(&error, x, y);
      mod1 = _stp_mod64(&error, x, y);
      div2 = x/y;
      mod2 = x%y;
      if (div1 != div2) {
	printf ("%lld/%lld was %lld and should have been %lld\n", x,y,div1,div2);
	exit (-1);
      }
      if (mod1 != mod2) {
	printf ("%lld\%%lld was %lld and should have been %lld\n", x,y,mod1,mod2);
	exit (-1);
      }
    }

    for (x = LONG_MAX - 1LL; x < LONG_MAX + 2LL; x++ ) {
      div1 = _stp_div64(&error, x, y);
      mod1 = _stp_mod64(&error, x, y);
      div2 = x/y;
      mod2 = x%y;
      if (div1 != div2) {
	printf ("%lld/%lld was %lld and should have been %lld\n", x,y,div1,div2);
	exit (-1);
      }
      if (mod1 != mod2) {
	printf ("%lld\%%lld was %lld and should have been %lld\n", x,y,mod1,mod2);
	exit (-1);
      }
    }

    for (x = LLONG_MIN; x <= LLONG_MIN + 1LL; x++ ) {
      div1 = _stp_div64(&error, x, y);
      mod1 = _stp_mod64(&error, x, y);
      div2 = x/y;
      mod2 = x%y;
      if (div1 != div2) {
	printf ("%lld/%lld was %lld and should have been %lld\n", x,y,div1,div2);
	exit (-1);
      }
      if (mod1 != mod2) {
	printf ("%lld\%%lld was %lld and should have been %lld\n", x,y,mod1,mod2);
	exit (-1);
      }
    }

    for (x = LONG_MAX - 1; x <= LONG_MAX; x++ ) {
      div1 = _stp_div64(&error, x, y);
      mod1 = _stp_mod64(&error, x, y);
      div2 = x/y;
      mod2 = x%y;
      if (div1 != div2) {
	printf ("%lld/%lld was %lld and should have been %lld\n", x,y,div1,div2);
	exit (-1);
      }
      if (mod1 != mod2) {
	printf ("%lld\%%lld was %lld and should have been %lld\n", x,y,mod1,mod2);
	exit (-1);
      }
    }
  }

  /* now do ten million random divisions and mods */
  for (i = 0; i < 10000000; i++)  {
    x = mrand48();
    y = mrand48();
    if (y == 0) {
      i--;
      continue;
    }

    div1 = _stp_div64(NULL, x, y);
    mod1 = _stp_mod64(NULL, x, y);
    div2 = x/y;
    mod2 = x%y;

    if (div1 != div2) {
      printf ("%lld/%lld was %lld and should have been %lld\n", x,y,div1,div2);
      exit (-1);
    }
    if (mod1 != mod2) {
      printf ("%lld\%%lld was %lld and should have been %lld\n", x,y,mod1,mod2);
      exit (-1);
    }
  }
  printf("OK\n");
  return 0;
}