summaryrefslogtreecommitdiff
path: root/test/regression/int32.c
blob: d7b6c9e8b3b9d7cc8de43137a99eed737a62c210 (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
/* Semi-random testing of 32-bit integer operations */

#include <stdio.h>

typedef unsigned int u32;
typedef signed int s32;
typedef unsigned long long u64;

static u32 rnd32(void)
{
  static u32 seed = 0;
  seed = seed * 69069 + 25173;
  return seed;
}

static inline u32 safe_udiv32(u32 x, u32 y)
{
  if (y == 0) return 0; else return x / y;
}

static inline u32 safe_umod32(u32 x, u32 y)
{
  if (y == 0) return 0; else return x % y;
}

static inline s32 safe_sdiv32(s32 x, s32 y)
{
  if (y == 0 || (y == -1 && x == (-1 << 31))) return 0; else return x / y;
}

static inline s32 safe_smod32(s32 x, s32 y)
{
  if (y == 0 || (y == -1 && x == (-1 << 31))) return 0; else return x % y;
}

static void test1(u32 x, u32 y)
{
  int i;
  double f;

  printf("x = %x\n", x);
  printf("y = %x\n", y);
  printf("-x = %x\n", -x);
  printf("x + y = %x\n", x + y);
  printf("x - y = %x\n", x - y);
  printf("x * y = %x\n", x * y);
  printf("x /u y = %x\n", safe_udiv32(x, y));
  printf("x %%u y = %x\n", safe_umod32(x, y));
  printf("x /s y = %x\n", safe_sdiv32(x, y));
  printf("x %%s y = %x\n", safe_smod32(x, y));
  printf("x /u 3 = %x\n", x / 3);
  printf("x %%u 3 = %x\n", x % 3);
  printf("x /s 3 = %x\n", (s32)x / 3);
  printf("x %%s 3 = %x\n", (s32)x % 3);
  printf("x /u 5 = %x\n", x / 5);
  printf("x %%u 5 = %x\n", x % 5);
  printf("x /s 5 = %x\n", (s32)x / 5);
  printf("x %%s 5 = %x\n", (s32)x % 5);
  printf("x /u 11 = %x\n", x / 11);
  printf("x %%u 11 = %x\n", x % 11);
  printf("x /s 11 = %x\n", (s32)x / 11);
  printf("x %%s 11 = %x\n", (s32)x % 11);
  printf("~x = %x\n", ~x);
  printf("x & y = %x\n", x & y);
  printf("x | y = %x\n", x | y);
  printf("x ^ y = %x\n", x ^ y);
  i = y & 31;
  printf("x << i = %x\n", x << i);
  printf("x >>u i = %x\n", x >> i);
  printf("x >>s i = %x\n", (s32) x >> i);
  printf("x cmpu y = %s\n",
         x == y ? "eq" : x < y ? "lt" : "gt");
  printf("x cmps y = %s\n",
         x == y ? "eq" : (s32)x < (s32)y ? "lt" : "gt");
  f = (double) x;
  printf("utod x = %llx\n", *((u64*) &f));
  f = f * 0.0001;
  printf("dtou f = %x\n", (u32) f);
  f = (double) ((s32) x);
  printf("stod x = %llx\n", *((u64*) &f));
  f = f * 0.0001;
  printf("dtos f = %x\n", (s32) f);
  printf("\n");
}

u32 special_values[] = {
  0,
  1,
  -1,
  0x7FFFFFFFU,
  0x80000000U,
};

#define NUM_SPECIAL_VALUES (sizeof(special_values) / sizeof(u32))

int main()
{
  int i, j;
  u32 x, y;

  for (i = 0; i < NUM_SPECIAL_VALUES; i++) {
    for (j = 0; j < NUM_SPECIAL_VALUES; j++) {
      test1(special_values[i], special_values[j]);
    }
    test1(special_values[i], rnd32());
    test1(rnd32(), special_values[i]);
  }
  for (i = 0; i < 100; i++) {
    x = rnd32(); y = rnd32();
    test1(x, y);
  }
  return 0;
}