summaryrefslogtreecommitdiffstats
path: root/runtime/arith.c
blob: 175cc4e8e64d52a8e380a1893b2db94197e6edb7 (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
#ifndef _ARITH_C_ /* -*- linux-c -*- */
#define _ARITH_C_

/** @file arith.
 * @brief Implements 64-bit signed division/multiplication.
 */

struct context;
void _stp_divmod64 (unsigned *errorcount, int64_t x, int64_t y,
                    int64_t *quo, int64_t *rem);


/** Divide x by y.  In case of overflow or division-by-zero,
 * increment context errorcount, and return any old value.
 */
inline int64_t _stp_div64 (unsigned *errorcount, int64_t x, int64_t y)
{
  if (likely ((x >= LONG_MIN && x <= LONG_MAX) &&
              (y >= LONG_MIN && y <= LONG_MAX)))
    {
      long xx = (long) x;
      long yy = (long) y;
      // check for division-by-zero and overflow
      if (unlikely (yy == 0 || (xx == LONG_MIN && yy == -1)))
        {
          (*errorcount) ++;
          return 0;
        }
      return xx / yy;
    }
  else
    {
      int64_t quo = 0;
      _stp_divmod64 (errorcount, x, y, &quo, NULL);
      return quo;
    }
}


/** Modulo x by y.  In case of overflow or division-by-zero,
 * increment context errorcount, and return any old value.
 */
inline int64_t _stp_mod64 (unsigned *errorcount, int64_t x, int64_t y)
{
  if (likely ((x >= LONG_MIN && x <= LONG_MAX) &&
              (y >= LONG_MIN && y <= LONG_MAX)))
    {
      long xx = (long) x;
      long yy = (long) y;
      // check for division-by-zero and overflow
      if (unlikely (yy == 0 || (xx == LONG_MIN && yy == -1)))
        {
          (*errorcount) ++;
          return 0;
        }
      return xx % yy;
    }
  else
    {
      int64_t rem = 0;
      _stp_divmod64 (errorcount, x, y, NULL, &rem);
      return rem;
    }
}


/** Perform general long division/modulus. */
void _stp_divmod64 (unsigned *errorcount, int64_t x, int64_t y,
                    int64_t *quo, int64_t *rem)
{
  // XXX: wimp out for now
  (*errorcount) ++;
  if (quo) *quo = 0;
  if (rem) *rem = 0;
}



#endif /* _ARITH_C_ */