qemu/tests/tcg/riscv64/test-div.c
<<
>>
Prefs
   1#include <assert.h>
   2#include <limits.h>
   3
   4struct TestS {
   5    long x, y, q, r;
   6};
   7
   8static struct TestS test_s[] = {
   9    { 4, 2, 2, 0 },                 /* normal cases */
  10    { 9, 7, 1, 2 },
  11    { 0, 0, -1, 0 },                /* div by zero cases */
  12    { 9, 0, -1, 9 },
  13    { LONG_MIN, -1, LONG_MIN, 0 },  /* overflow case */
  14};
  15
  16struct TestU {
  17    unsigned long x, y, q, r;
  18};
  19
  20static struct TestU test_u[] = {
  21    { 4, 2, 2, 0 },                 /* normal cases */
  22    { 9, 7, 1, 2 },
  23    { 0, 0, ULONG_MAX, 0 },         /* div by zero cases */
  24    { 9, 0, ULONG_MAX, 9 },
  25};
  26
  27#define ARRAY_SIZE(X)  (sizeof(X) / sizeof(*(X)))
  28
  29int main (void)
  30{
  31    int i;
  32
  33    for (i = 0; i < ARRAY_SIZE(test_s); i++) {
  34        long q, r;
  35
  36        asm("div %0, %2, %3\n\t"
  37            "rem %1, %2, %3"
  38            : "=&r" (q), "=r" (r)
  39            : "r" (test_s[i].x), "r" (test_s[i].y));
  40
  41        assert(q == test_s[i].q);
  42        assert(r == test_s[i].r);
  43    }
  44
  45    for (i = 0; i < ARRAY_SIZE(test_u); i++) {
  46        unsigned long q, r;
  47
  48        asm("divu %0, %2, %3\n\t"
  49            "remu %1, %2, %3"
  50            : "=&r" (q), "=r" (r)
  51            : "r" (test_u[i].x), "r" (test_u[i].y));
  52
  53        assert(q == test_u[i].q);
  54        assert(r == test_u[i].r);
  55    }
  56
  57    return 0;
  58}
  59