Home | History | Annotate | Download | only in useful-harnesses
      1 #include <stdio.h>
      2 #include "i64operations.h"
      3 
      4 int64_t         tval_a = 1234567890003LL;
      5 int64_t         tval_b = 2345678901235LL;
      6 int64_t         tval_c = 1234567890001LL;
      7 int64_t         tval_d = 10001LL;
      8 int64_t         tval_e = 10000LL;
      9 uint64_t        tval_f = 0xffffff0750135eb9;
     10 int64_t		tval_g = -1;
     11 
     12 /* ~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~- */
     13 
     14 int
     15 i64_eq(int64_t a, int64_t b)
     16 {
     17   return (a == b);
     18 }
     19 
     20 int
     21 i64_neq(int64_t a, int64_t b)
     22 {
     23   return (a != b);
     24 }
     25 
     26 int
     27 i64_gt(int64_t a, int64_t b)
     28 {
     29   return (a > b);
     30 }
     31 
     32 int
     33 i64_le(int64_t a, int64_t b)
     34 {
     35   return (a <= b);
     36 }
     37 
     38 int
     39 i64_ge(int64_t a, int64_t b) {
     40   return (a >= b);
     41 }
     42 
     43 int
     44 i64_lt(int64_t a, int64_t b) {
     45   return (a < b);
     46 }
     47 
     48 int
     49 i64_uge(uint64_t a, uint64_t b)
     50 {
     51   return (a >= b);
     52 }
     53 
     54 int
     55 i64_ult(uint64_t a, uint64_t b)
     56 {
     57   return (a < b);
     58 }
     59 
     60 int
     61 i64_ugt(uint64_t a, uint64_t b)
     62 {
     63   return (a > b);
     64 }
     65 
     66 int
     67 i64_ule(uint64_t a, uint64_t b)
     68 {
     69   return (a <= b);
     70 }
     71 
     72 int64_t
     73 i64_eq_select(int64_t a, int64_t b, int64_t c, int64_t d)
     74 {
     75   return ((a == b) ? c : d);
     76 }
     77 
     78 int64_t
     79 i64_neq_select(int64_t a, int64_t b, int64_t c, int64_t d)
     80 {
     81   return ((a != b) ? c : d);
     82 }
     83 
     84 int64_t
     85 i64_gt_select(int64_t a, int64_t b, int64_t c, int64_t d) {
     86   return ((a > b) ? c : d);
     87 }
     88 
     89 int64_t
     90 i64_le_select(int64_t a, int64_t b, int64_t c, int64_t d) {
     91   return ((a <= b) ? c : d);
     92 }
     93 
     94 int64_t
     95 i64_ge_select(int64_t a, int64_t b, int64_t c, int64_t d) {
     96   return ((a >= b) ? c : d);
     97 }
     98 
     99 int64_t
    100 i64_lt_select(int64_t a, int64_t b, int64_t c, int64_t d) {
    101   return ((a < b) ? c : d);
    102 }
    103 
    104 uint64_t
    105 i64_ugt_select(uint64_t a, uint64_t b, uint64_t c, uint64_t d)
    106 {
    107   return ((a > b) ? c : d);
    108 }
    109 
    110 uint64_t
    111 i64_ule_select(uint64_t a, uint64_t b, uint64_t c, uint64_t d)
    112 {
    113   return ((a <= b) ? c : d);
    114 }
    115 
    116 uint64_t
    117 i64_uge_select(uint64_t a, uint64_t b, uint64_t c, uint64_t d) {
    118   return ((a >= b) ? c : d);
    119 }
    120 
    121 uint64_t
    122 i64_ult_select(uint64_t a, uint64_t b, uint64_t c, uint64_t d) {
    123   return ((a < b) ? c : d);
    124 }
    125 
    126 /* ~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~- */
    127 
    128 struct harness_int64_pred int64_tests_eq[] = {
    129   {"a %s a", &tval_a, &tval_a, &tval_c, &tval_d, TRUE_VAL, &tval_c},
    130   {"a %s b", &tval_a, &tval_b, &tval_c, &tval_d, FALSE_VAL, &tval_d},
    131   {"a %s c", &tval_a, &tval_c, &tval_c, &tval_d, FALSE_VAL, &tval_d},
    132   {"d %s e", &tval_d, &tval_e, &tval_c, &tval_d, FALSE_VAL, &tval_d},
    133   {"e %s e", &tval_e, &tval_e, &tval_c, &tval_d, TRUE_VAL, &tval_c}
    134 };
    135 
    136 struct harness_int64_pred int64_tests_neq[] = {
    137   {"a %s a", &tval_a, &tval_a, &tval_c, &tval_d, FALSE_VAL, &tval_d},
    138   {"a %s b", &tval_a, &tval_b, &tval_c, &tval_d, TRUE_VAL, &tval_c},
    139   {"a %s c", &tval_a, &tval_c, &tval_c, &tval_d, TRUE_VAL, &tval_c},
    140   {"d %s e", &tval_d, &tval_e, &tval_c, &tval_d, TRUE_VAL, &tval_c},
    141   {"e %s e", &tval_e, &tval_e, &tval_c, &tval_d, FALSE_VAL, &tval_d}
    142 };
    143 
    144 struct harness_int64_pred int64_tests_sgt[] = {
    145   {"a %s a", &tval_a, &tval_a, &tval_c, &tval_d, FALSE_VAL, &tval_d},
    146   {"a %s b", &tval_a, &tval_b, &tval_c, &tval_d, FALSE_VAL, &tval_d},
    147   {"a %s c", &tval_a, &tval_c, &tval_c, &tval_d, TRUE_VAL, &tval_c},
    148   {"d %s e", &tval_d, &tval_e, &tval_c, &tval_d, TRUE_VAL, &tval_c},
    149   {"e %s e", &tval_e, &tval_e, &tval_c, &tval_d, FALSE_VAL, &tval_d}
    150 };
    151 
    152 struct harness_int64_pred int64_tests_sle[] = {
    153   {"a %s a", &tval_a, &tval_a, &tval_c, &tval_d, TRUE_VAL, &tval_c},
    154   {"a %s b", &tval_a, &tval_b, &tval_c, &tval_d, TRUE_VAL, &tval_c},
    155   {"a %s c", &tval_a, &tval_c, &tval_c, &tval_d, FALSE_VAL, &tval_d},
    156   {"d %s e", &tval_d, &tval_e, &tval_c, &tval_d, FALSE_VAL, &tval_d},
    157   {"e %s e", &tval_e, &tval_e, &tval_c, &tval_d, TRUE_VAL, &tval_c}
    158 };
    159 
    160 struct harness_int64_pred int64_tests_sge[] = {
    161   {"a %s a", &tval_a, &tval_a, &tval_c, &tval_d, TRUE_VAL, &tval_c},
    162   {"a %s b", &tval_a, &tval_b, &tval_c, &tval_d, FALSE_VAL, &tval_d},
    163   {"a %s c", &tval_a, &tval_c, &tval_c, &tval_d, TRUE_VAL, &tval_c},
    164   {"d %s e", &tval_d, &tval_e, &tval_c, &tval_d, TRUE_VAL, &tval_c},
    165   {"e %s e", &tval_e, &tval_e, &tval_c, &tval_d, TRUE_VAL, &tval_c}
    166 };
    167 
    168 struct harness_int64_pred int64_tests_slt[] = {
    169   {"a %s a", &tval_a, &tval_a, &tval_c, &tval_d, FALSE_VAL, &tval_d},
    170   {"a %s b", &tval_a, &tval_b, &tval_c, &tval_d, TRUE_VAL, &tval_c},
    171   {"a %s c", &tval_a, &tval_c, &tval_c, &tval_d, FALSE_VAL, &tval_d},
    172   {"d %s e", &tval_d, &tval_e, &tval_c, &tval_d, FALSE_VAL, &tval_d},
    173   {"e %s e", &tval_e, &tval_e, &tval_c, &tval_d, FALSE_VAL, &tval_d}
    174 };
    175 
    176 struct int64_pred_s int64_preds[] = {
    177   {"eq", i64_eq, i64_eq_select,
    178      int64_tests_eq, ARR_SIZE(int64_tests_eq)},
    179   {"neq", i64_neq, i64_neq_select,
    180      int64_tests_neq, ARR_SIZE(int64_tests_neq)},
    181   {"gt", i64_gt, i64_gt_select,
    182      int64_tests_sgt, ARR_SIZE(int64_tests_sgt)},
    183   {"le", i64_le, i64_le_select,
    184      int64_tests_sle, ARR_SIZE(int64_tests_sle)},
    185   {"ge", i64_ge, i64_ge_select,
    186      int64_tests_sge, ARR_SIZE(int64_tests_sge)},
    187   {"lt", i64_lt, i64_lt_select,
    188      int64_tests_slt, ARR_SIZE(int64_tests_slt)}
    189 };
    190 
    191 /* ~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~- */
    192 
    193 struct harness_uint64_pred uint64_tests_ugt[] = {
    194   {"a %s a", (uint64_t *) &tval_a, (uint64_t *) &tval_a, (uint64_t *) &tval_c,
    195      (uint64_t *) &tval_d, FALSE_VAL, (uint64_t *) &tval_d},
    196   {"a %s b", (uint64_t *) &tval_a, (uint64_t *) &tval_b, (uint64_t *) &tval_c,
    197      (uint64_t *) &tval_d, FALSE_VAL, (uint64_t *) &tval_d },
    198   {"a %s c", (uint64_t *) &tval_a, (uint64_t *) &tval_c, (uint64_t *) &tval_c,
    199      (uint64_t *) &tval_d, TRUE_VAL, (uint64_t *) &tval_c },
    200   {"d %s e", (uint64_t *) &tval_d, (uint64_t *) &tval_e, (uint64_t *) &tval_c,
    201      (uint64_t *) &tval_d, TRUE_VAL, (uint64_t *) &tval_c },
    202   {"e %s e", (uint64_t *) &tval_e, (uint64_t *) &tval_e, (uint64_t *) &tval_c,
    203      (uint64_t *) &tval_d, FALSE_VAL, (uint64_t *) &tval_d }
    204 };
    205 
    206 struct harness_uint64_pred uint64_tests_ule[] = {
    207   {"a %s a", (uint64_t *) &tval_a, (uint64_t *) &tval_a, (uint64_t *) &tval_c,
    208      (uint64_t *) &tval_d, TRUE_VAL, (uint64_t *) &tval_c},
    209   {"a %s b", (uint64_t *) &tval_a, (uint64_t *) &tval_b, (uint64_t *) &tval_c,
    210      (uint64_t *) &tval_d, TRUE_VAL, (uint64_t *) &tval_c},
    211   {"a %s c", (uint64_t *) &tval_a, (uint64_t *) &tval_c, (uint64_t *) &tval_c,
    212      (uint64_t *) &tval_d, FALSE_VAL, (uint64_t *) &tval_d},
    213   {"d %s e", (uint64_t *) &tval_d, (uint64_t *) &tval_e, (uint64_t *) &tval_c,
    214      (uint64_t *) &tval_d, FALSE_VAL, (uint64_t *) &tval_d},
    215   {"e %s e", (uint64_t *) &tval_e, (uint64_t *) &tval_e, (uint64_t *) &tval_c,
    216      (uint64_t *) &tval_d, TRUE_VAL, (uint64_t *) &tval_c}
    217 };
    218 
    219 struct harness_uint64_pred uint64_tests_uge[] = {
    220   {"a %s a", (uint64_t *) &tval_a, (uint64_t *) &tval_a, (uint64_t *) &tval_c,
    221      (uint64_t *) &tval_d, TRUE_VAL, (uint64_t *) &tval_c},
    222   {"a %s b", (uint64_t *) &tval_a, (uint64_t *) &tval_b, (uint64_t *) &tval_c,
    223      (uint64_t *) &tval_d, FALSE_VAL, (uint64_t *) &tval_d},
    224   {"a %s c", (uint64_t *) &tval_a, (uint64_t *) &tval_c, (uint64_t *) &tval_c,
    225      (uint64_t *) &tval_d, TRUE_VAL, (uint64_t *) &tval_c},
    226   {"d %s e", (uint64_t *) &tval_d, (uint64_t *) &tval_e, (uint64_t *) &tval_c,
    227      (uint64_t *) &tval_d, TRUE_VAL, (uint64_t *) &tval_c},
    228   {"e %s e", (uint64_t *) &tval_e, (uint64_t *) &tval_e, (uint64_t *) &tval_c,
    229      (uint64_t *) &tval_d, TRUE_VAL, (uint64_t *) &tval_c}
    230 };
    231 
    232 struct harness_uint64_pred uint64_tests_ult[] = {
    233   {"a %s a", (uint64_t *) &tval_a, (uint64_t *) &tval_a, (uint64_t *) &tval_c,
    234      (uint64_t *) &tval_d, FALSE_VAL, (uint64_t *) &tval_d},
    235   {"a %s b", (uint64_t *) &tval_a, (uint64_t *) &tval_b, (uint64_t *) &tval_c,
    236      (uint64_t *) &tval_d, TRUE_VAL, (uint64_t *) &tval_c},
    237   {"a %s c", (uint64_t *) &tval_a, (uint64_t *) &tval_c, (uint64_t *) &tval_c,
    238      (uint64_t *) &tval_d, FALSE_VAL, (uint64_t *) &tval_d},
    239   {"d %s e", (uint64_t *) &tval_d, (uint64_t *) &tval_e, (uint64_t *) &tval_c,
    240      (uint64_t *) &tval_d, FALSE_VAL, (uint64_t *) &tval_d},
    241   {"e %s e", (uint64_t *) &tval_e, (uint64_t *) &tval_e, (uint64_t *) &tval_c,
    242      (uint64_t *) &tval_d, FALSE_VAL, (uint64_t *) &tval_d}
    243 };
    244 
    245 struct uint64_pred_s uint64_preds[] = {
    246   {"ugt", i64_ugt, i64_ugt_select,
    247      uint64_tests_ugt, ARR_SIZE(uint64_tests_ugt)},
    248   {"ule", i64_ule, i64_ule_select,
    249      uint64_tests_ule, ARR_SIZE(uint64_tests_ule)},
    250   {"uge", i64_uge, i64_uge_select,
    251      uint64_tests_uge, ARR_SIZE(uint64_tests_uge)},
    252   {"ult", i64_ult, i64_ult_select,
    253      uint64_tests_ult, ARR_SIZE(uint64_tests_ult)}
    254 };
    255 
    256 int
    257 compare_expect_int64(const struct int64_pred_s * pred)
    258 {
    259   int             j, failed = 0;
    260 
    261   for (j = 0; j < pred->n_tests; ++j) {
    262     int             pred_result;
    263 
    264     pred_result = (*pred->predfunc) (*pred->tests[j].lhs, *pred->tests[j].rhs);
    265 
    266     if (pred_result != pred->tests[j].expected) {
    267       char            str[64];
    268 
    269       sprintf(str, pred->tests[j].fmt_string, pred->name);
    270       printf("%s: returned value is %d, expecting %d\n", str,
    271 	     pred_result, pred->tests[j].expected);
    272       printf("  lhs = %19lld (0x%016llx)\n", *pred->tests[j].lhs,
    273              *pred->tests[j].lhs);
    274       printf("  rhs = %19lld (0x%016llx)\n", *pred->tests[j].rhs,
    275              *pred->tests[j].rhs);
    276       ++failed;
    277     } else {
    278       int64_t         selresult;
    279 
    280       selresult = (pred->selfunc) (*pred->tests[j].lhs, *pred->tests[j].rhs,
    281                                    *pred->tests[j].select_a,
    282                                    *pred->tests[j].select_b);
    283 
    284       if (selresult != *pred->tests[j].select_expected) {
    285 	char            str[64];
    286 
    287 	sprintf(str, pred->tests[j].fmt_string, pred->name);
    288 	printf("%s select: returned value is %d, expecting %d\n", str,
    289 	       pred_result, pred->tests[j].expected);
    290 	printf("  lhs   = %19lld (0x%016llx)\n", *pred->tests[j].lhs,
    291 	       *pred->tests[j].lhs);
    292 	printf("  rhs   = %19lld (0x%016llx)\n", *pred->tests[j].rhs,
    293 	       *pred->tests[j].rhs);
    294 	printf("  true  = %19lld (0x%016llx)\n", *pred->tests[j].select_a,
    295 	       *pred->tests[j].select_a);
    296 	printf("  false = %19lld (0x%016llx)\n", *pred->tests[j].select_b,
    297 	       *pred->tests[j].select_b);
    298 	++failed;
    299       }
    300     }
    301   }
    302 
    303   printf("  %d tests performed, should be %d.\n", j, pred->n_tests);
    304 
    305   return failed;
    306 }
    307 
    308 int
    309 compare_expect_uint64(const struct uint64_pred_s * pred)
    310 {
    311   int             j, failed = 0;
    312 
    313   for (j = 0; j < pred->n_tests; ++j) {
    314     int             pred_result;
    315 
    316     pred_result = (*pred->predfunc) (*pred->tests[j].lhs, *pred->tests[j].rhs);
    317     if (pred_result != pred->tests[j].expected) {
    318       char            str[64];
    319 
    320       sprintf(str, pred->tests[j].fmt_string, pred->name);
    321       printf("%s: returned value is %d, expecting %d\n", str,
    322 	     pred_result, pred->tests[j].expected);
    323       printf("  lhs = %19llu (0x%016llx)\n", *pred->tests[j].lhs,
    324              *pred->tests[j].lhs);
    325       printf("  rhs = %19llu (0x%016llx)\n", *pred->tests[j].rhs,
    326              *pred->tests[j].rhs);
    327       ++failed;
    328     } else {
    329       uint64_t        selresult;
    330 
    331       selresult = (pred->selfunc) (*pred->tests[j].lhs, *pred->tests[j].rhs,
    332                                    *pred->tests[j].select_a,
    333                                    *pred->tests[j].select_b);
    334       if (selresult != *pred->tests[j].select_expected) {
    335 	char            str[64];
    336 
    337 	sprintf(str, pred->tests[j].fmt_string, pred->name);
    338 	printf("%s select: returned value is %d, expecting %d\n", str,
    339 	       pred_result, pred->tests[j].expected);
    340 	printf("  lhs   = %19llu (0x%016llx)\n", *pred->tests[j].lhs,
    341 	       *pred->tests[j].lhs);
    342 	printf("  rhs   = %19llu (0x%016llx)\n", *pred->tests[j].rhs,
    343 	       *pred->tests[j].rhs);
    344 	printf("  true  = %19llu (0x%016llx)\n", *pred->tests[j].select_a,
    345 	       *pred->tests[j].select_a);
    346 	printf("  false = %19llu (0x%016llx)\n", *pred->tests[j].select_b,
    347 	       *pred->tests[j].select_b);
    348 	++failed;
    349       }
    350     }
    351   }
    352 
    353   printf("  %d tests performed, should be %d.\n", j, pred->n_tests);
    354 
    355   return failed;
    356 }
    357 
    358 /* ~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~- */
    359 
    360 int
    361 test_i64_sext_i32(int in, int64_t expected) {
    362   int64_t result = (int64_t) in;
    363 
    364   if (result != expected) {
    365     char str[64];
    366     sprintf(str, "i64_sext_i32(%d) returns %lld\n", in, result);
    367     return 1;
    368   }
    369 
    370   return 0;
    371 }
    372 
    373 int
    374 test_i64_sext_i16(short in, int64_t expected) {
    375   int64_t result = (int64_t) in;
    376 
    377   if (result != expected) {
    378     char str[64];
    379     sprintf(str, "i64_sext_i16(%hd) returns %lld\n", in, result);
    380     return 1;
    381   }
    382 
    383   return 0;
    384 }
    385 
    386 int
    387 test_i64_sext_i8(signed char in, int64_t expected) {
    388   int64_t result = (int64_t) in;
    389 
    390   if (result != expected) {
    391     char str[64];
    392     sprintf(str, "i64_sext_i8(%d) returns %lld\n", in, result);
    393     return 1;
    394   }
    395 
    396   return 0;
    397 }
    398 
    399 int
    400 test_i64_zext_i32(unsigned int in, uint64_t expected) {
    401   uint64_t result = (uint64_t) in;
    402 
    403   if (result != expected) {
    404     char str[64];
    405     sprintf(str, "i64_zext_i32(%u) returns %llu\n", in, result);
    406     return 1;
    407   }
    408 
    409   return 0;
    410 }
    411 
    412 int
    413 test_i64_zext_i16(unsigned short in, uint64_t expected) {
    414   uint64_t result = (uint64_t) in;
    415 
    416   if (result != expected) {
    417     char str[64];
    418     sprintf(str, "i64_zext_i16(%hu) returns %llu\n", in, result);
    419     return 1;
    420   }
    421 
    422   return 0;
    423 }
    424 
    425 int
    426 test_i64_zext_i8(unsigned char in, uint64_t expected) {
    427   uint64_t result = (uint64_t) in;
    428 
    429   if (result != expected) {
    430     char str[64];
    431     sprintf(str, "i64_zext_i8(%u) returns %llu\n", in, result);
    432     return 1;
    433   }
    434 
    435   return 0;
    436 }
    437 
    438 /* ~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~- */
    439 
    440 int64_t
    441 i64_shl_const(int64_t a) {
    442   return a << 10;
    443 }
    444 
    445 int64_t
    446 i64_shl(int64_t a, int amt) {
    447   return a << amt;
    448 }
    449 
    450 uint64_t
    451 u64_shl_const(uint64_t a) {
    452   return a << 10;
    453 }
    454 
    455 uint64_t
    456 u64_shl(uint64_t a, int amt) {
    457   return a << amt;
    458 }
    459 
    460 int64_t
    461 i64_srl_const(int64_t a) {
    462   return a >> 10;
    463 }
    464 
    465 int64_t
    466 i64_srl(int64_t a, int amt) {
    467   return a >> amt;
    468 }
    469 
    470 uint64_t
    471 u64_srl_const(uint64_t a) {
    472   return a >> 10;
    473 }
    474 
    475 uint64_t
    476 u64_srl(uint64_t a, int amt) {
    477   return a >> amt;
    478 }
    479 
    480 int64_t
    481 i64_sra_const(int64_t a) {
    482   return a >> 10;
    483 }
    484 
    485 int64_t
    486 i64_sra(int64_t a, int amt) {
    487   return a >> amt;
    488 }
    489 
    490 uint64_t
    491 u64_sra_const(uint64_t a) {
    492   return a >> 10;
    493 }
    494 
    495 uint64_t
    496 u64_sra(uint64_t a, int amt) {
    497   return a >> amt;
    498 }
    499 
    500 int
    501 test_u64_constant_shift(const char *func_name, uint64_t (*func)(uint64_t), uint64_t a, uint64_t expected) {
    502   uint64_t result = (*func)(a);
    503 
    504   if (result != expected) {
    505     printf("%s(0x%016llx) returns 0x%016llx, expected 0x%016llx\n", func_name, a, result, expected);
    506     return 1;
    507   }
    508 
    509   return 0;
    510 }
    511 
    512 int
    513 test_i64_constant_shift(const char *func_name, int64_t (*func)(int64_t), int64_t a, int64_t expected) {
    514   int64_t result = (*func)(a);
    515 
    516   if (result != expected) {
    517     printf("%s(0x%016llx) returns 0x%016llx, expected 0x%016llx\n", func_name, a, result, expected);
    518     return 1;
    519   }
    520 
    521   return 0;
    522 }
    523 
    524 int
    525 test_u64_variable_shift(const char *func_name, uint64_t (*func)(uint64_t, int), uint64_t a, unsigned int b, uint64_t expected) {
    526   uint64_t result = (*func)(a, b);
    527 
    528   if (result != expected) {
    529     printf("%s(0x%016llx, %d) returns 0x%016llx, expected 0x%016llx\n", func_name, a, b, result, expected);
    530     return 1;
    531   }
    532 
    533   return 0;
    534 }
    535 
    536 int
    537 test_i64_variable_shift(const char *func_name, int64_t (*func)(int64_t, int), int64_t a, unsigned int b, int64_t expected) {
    538   int64_t result = (*func)(a, b);
    539 
    540   if (result != expected) {
    541     printf("%s(0x%016llx, %d) returns 0x%016llx, expected 0x%016llx\n", func_name, a, b, result, expected);
    542     return 1;
    543   }
    544 
    545   return 0;
    546 }
    547 
    548 /* ~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~- */
    549 
    550 int64_t i64_mul(int64_t a, int64_t b) {
    551   return a * b;
    552 }
    553 
    554 /* ~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~- */
    555 
    556 int
    557 main(void)
    558 {
    559   int             i, j, failed = 0;
    560   const char     *something_failed = "  %d tests failed.\n";
    561   const char     *all_tests_passed = "  All tests passed.\n";
    562 
    563   printf("tval_a = %20lld (0x%016llx)\n", tval_a, tval_a);
    564   printf("tval_b = %20lld (0x%016llx)\n", tval_b, tval_b);
    565   printf("tval_c = %20lld (0x%016llx)\n", tval_c, tval_c);
    566   printf("tval_d = %20lld (0x%016llx)\n", tval_d, tval_d);
    567   printf("tval_e = %20lld (0x%016llx)\n", tval_e, tval_e);
    568   printf("tval_f = %20llu (0x%016llx)\n", tval_f, tval_f);
    569   printf("tval_g = %20llu (0x%016llx)\n", tval_g, tval_g);
    570   printf("----------------------------------------\n");
    571 
    572   for (i = 0; i < ARR_SIZE(int64_preds); ++i) {
    573     printf("%s series:\n", int64_preds[i].name);
    574     if ((failed = compare_expect_int64(int64_preds + i)) > 0) {
    575       printf(something_failed, failed);
    576     } else {
    577       printf(all_tests_passed);
    578     }
    579 
    580     printf("----------------------------------------\n");
    581   }
    582 
    583   for (i = 0; i < ARR_SIZE(uint64_preds); ++i) {
    584     printf("%s series:\n", uint64_preds[i].name);
    585     if ((failed = compare_expect_uint64(uint64_preds + i)) > 0) {
    586       printf(something_failed, failed);
    587     } else {
    588       printf(all_tests_passed);
    589     }
    590 
    591     printf("----------------------------------------\n");
    592   }
    593 
    594   /*----------------------------------------------------------------------*/
    595 
    596   puts("signed/zero-extend tests:");
    597 
    598   failed = 0;
    599   failed += test_i64_sext_i32(-1, -1LL);
    600   failed += test_i64_sext_i32(10, 10LL);
    601   failed += test_i64_sext_i32(0x7fffffff, 0x7fffffffLL);
    602   failed += test_i64_sext_i16(-1, -1LL);
    603   failed += test_i64_sext_i16(10, 10LL);
    604   failed += test_i64_sext_i16(0x7fff, 0x7fffLL);
    605   failed += test_i64_sext_i8(-1, -1LL);
    606   failed += test_i64_sext_i8(10, 10LL);
    607   failed += test_i64_sext_i8(0x7f, 0x7fLL);
    608 
    609   failed += test_i64_zext_i32(0xffffffff, 0x00000000ffffffffLLU);
    610   failed += test_i64_zext_i32(0x01234567, 0x0000000001234567LLU);
    611   failed += test_i64_zext_i16(0xffff,     0x000000000000ffffLLU);
    612   failed += test_i64_zext_i16(0x569a,     0x000000000000569aLLU);
    613   failed += test_i64_zext_i8(0xff,        0x00000000000000ffLLU);
    614   failed += test_i64_zext_i8(0xa0,        0x00000000000000a0LLU);
    615 
    616   if (failed > 0) {
    617     printf("  %d tests failed.\n", failed);
    618   } else {
    619     printf("  All tests passed.\n");
    620   }
    621 
    622   printf("----------------------------------------\n");
    623 
    624   failed = 0;
    625   puts("signed left/right shift tests:");
    626   failed += test_i64_constant_shift("i64_shl_const", i64_shl_const, tval_a,     0x00047dc7ec114c00LL);
    627   failed += test_i64_variable_shift("i64_shl",       i64_shl,       tval_a, 10, 0x00047dc7ec114c00LL);
    628   failed += test_i64_constant_shift("i64_srl_const", i64_srl_const, tval_a,     0x0000000047dc7ec1LL);
    629   failed += test_i64_variable_shift("i64_srl",       i64_srl,       tval_a, 10, 0x0000000047dc7ec1LL);
    630   failed += test_i64_constant_shift("i64_sra_const", i64_sra_const, tval_a,     0x0000000047dc7ec1LL);
    631   failed += test_i64_variable_shift("i64_sra",       i64_sra,       tval_a, 10, 0x0000000047dc7ec1LL);
    632 
    633   if (failed > 0) {
    634     printf("  %d tests ailed.\n", failed);
    635   } else {
    636     printf("  All tests passed.\n");
    637   }
    638 
    639   printf("----------------------------------------\n");
    640 
    641   failed = 0;
    642   puts("unsigned left/right shift tests:");
    643   failed += test_u64_constant_shift("u64_shl_const", u64_shl_const,  tval_f,     0xfffc1d404d7ae400LL);
    644   failed += test_u64_variable_shift("u64_shl",       u64_shl,        tval_f, 10, 0xfffc1d404d7ae400LL);
    645   failed += test_u64_constant_shift("u64_srl_const", u64_srl_const,  tval_f,     0x003fffffc1d404d7LL);
    646   failed += test_u64_variable_shift("u64_srl",       u64_srl,        tval_f, 10, 0x003fffffc1d404d7LL);
    647   failed += test_i64_constant_shift("i64_sra_const", i64_sra_const,  tval_f,     0xffffffffc1d404d7LL);
    648   failed += test_i64_variable_shift("i64_sra",       i64_sra,        tval_f, 10, 0xffffffffc1d404d7LL);
    649   failed += test_u64_constant_shift("u64_sra_const", u64_sra_const,  tval_f,     0x003fffffc1d404d7LL);
    650   failed += test_u64_variable_shift("u64_sra",       u64_sra,        tval_f, 10, 0x003fffffc1d404d7LL);
    651 
    652   if (failed > 0) {
    653     printf("  %d tests ailed.\n", failed);
    654   } else {
    655     printf("  All tests passed.\n");
    656   }
    657 
    658   printf("----------------------------------------\n");
    659 
    660   int64_t result;
    661 
    662   result = i64_mul(tval_g, tval_g);
    663   printf("%20lld * %20lld = %20lld (0x%016llx)\n", tval_g, tval_g, result, result);
    664   result = i64_mul(tval_d, tval_e);
    665   printf("%20lld * %20lld = %20lld (0x%016llx)\n", tval_d, tval_e, result, result);
    666   /* 0xba7a664f13077c9 */
    667   result = i64_mul(tval_a, tval_b);
    668   printf("%20lld * %20lld = %20lld (0x%016llx)\n", tval_a, tval_b, result, result);
    669 
    670   printf("----------------------------------------\n");
    671 
    672   return 0;
    673 }
    674