summaryrefslogtreecommitdiff
path: root/lib/Numerical/Integer.cpp
blob: 4ddd785f4a811be68077b846712e915dff037306 (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
#include "Numerical/Integer.h"

// Exponentation by squaring
template<typename T>
static T ipow(T x, T y) {
  if(y < 0) return 0;

  T result = 1;
  while (y != 0) {
    if ((y & 1) == 1)
      result *= x;
    y >>= 1;
    x *= x;
  }
  return result;
}

LIBFLANG_ABI int8_t  libflang_pow_i1_i1(int8_t x, int8_t y) {
  return ipow(x, y);
}

LIBFLANG_ABI int16_t libflang_pow_i2_i2(int16_t x, int16_t y) {
  return ipow(x, y);
}

LIBFLANG_ABI int32_t libflang_pow_i4_i4(int32_t x, int32_t y) {
  return ipow(x, y);
}

LIBFLANG_ABI int64_t libflang_pow_i8_i8(int64_t x, int64_t y) {
  return ipow(x, y);
}