• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 #include <stdio.h>
2 
3 typedef unsigned int  		uint32_t;
4 typedef int           		int32_t;
5 
boolstring(int val)6 const char *boolstring(int val) {
7   return val ? "true" : "false";
8 }
9 
i32_eq(int32_t a,int32_t b)10 int i32_eq(int32_t a, int32_t b) {
11   return (a == b);
12 }
13 
i32_neq(int32_t a,int32_t b)14 int i32_neq(int32_t a, int32_t b) {
15   return (a != b);
16 }
17 
i32_eq_select(int32_t a,int32_t b,int32_t c,int32_t d)18 int32_t i32_eq_select(int32_t a, int32_t b, int32_t c, int32_t d) {
19   return ((a == b) ? c : d);
20 }
21 
i32_neq_select(int32_t a,int32_t b,int32_t c,int32_t d)22 int32_t i32_neq_select(int32_t a, int32_t b, int32_t c, int32_t d) {
23   return ((a != b) ? c : d);
24 }
25 
26 struct pred_s {
27   const char *name;
28   int (*predfunc)(int32_t, int32_t);
29   int (*selfunc)(int32_t, int32_t, int32_t, int32_t);
30 };
31 
32 struct pred_s preds[] = {
33   { "eq",  i32_eq,  i32_eq_select },
34   { "neq", i32_neq, i32_neq_select }
35 };
36 
main(void)37 int main(void) {
38   int i;
39   int32_t a = 1234567890;
40   int32_t b =  345678901;
41   int32_t c = 1234500000;
42   int32_t d =      10001;
43   int32_t e =      10000;
44 
45   printf("a = %12d (0x%08x)\n", a, a);
46   printf("b = %12d (0x%08x)\n", b, b);
47   printf("c = %12d (0x%08x)\n", c, c);
48   printf("d = %12d (0x%08x)\n", d, d);
49   printf("e = %12d (0x%08x)\n", e, e);
50   printf("----------------------------------------\n");
51 
52   for (i = 0; i < sizeof(preds)/sizeof(preds[0]); ++i) {
53     printf("a %s a = %s\n", preds[i].name, boolstring((*preds[i].predfunc)(a, a)));
54     printf("a %s a = %s\n", preds[i].name, boolstring((*preds[i].predfunc)(a, a)));
55     printf("a %s b = %s\n", preds[i].name, boolstring((*preds[i].predfunc)(a, b)));
56     printf("a %s c = %s\n", preds[i].name, boolstring((*preds[i].predfunc)(a, c)));
57     printf("d %s e = %s\n", preds[i].name, boolstring((*preds[i].predfunc)(d, e)));
58     printf("e %s e = %s\n", preds[i].name, boolstring((*preds[i].predfunc)(e, e)));
59 
60     printf("a %s a ? c : d = %d\n", preds[i].name, (*preds[i].selfunc)(a, a, c, d));
61     printf("a %s a ? c : d == c (%s)\n", preds[i].name, boolstring((*preds[i].selfunc)(a, a, c, d) == c));
62     printf("a %s b ? c : d = %d\n", preds[i].name, (*preds[i].selfunc)(a, b, c, d));
63     printf("a %s b ? c : d == d (%s)\n", preds[i].name, boolstring((*preds[i].selfunc)(a, b, c, d) == d));
64 
65     printf("----------------------------------------\n");
66   }
67 
68   return 0;
69 }
70