6 template <
typename T>
static inline T udiv(T a, T b) {
15 char num_digits_remaining = 0;
16 while (!(b &
static_cast<T
>(1) << (
sizeof(T) * 8 - 1)) && (b << 1) <= a) {
18 ++num_digits_remaining;
27 for (; num_digits_remaining; --num_digits_remaining) {
47 template <
typename T>
static inline T umod(T a, T b) {
56 char num_digits_remaining = 0;
57 while (!(b &
static_cast<T
>(1) << (
sizeof(T) * 8 - 1)) && (b << 1) <= a) {
59 ++num_digits_remaining;
67 for (; num_digits_remaining; --num_digits_remaining) {
82 template <
typename T>
static inline T udivmod(T a, T b, T *rem) {
93 char num_digits_remaining = 0;
94 while (!(b &
static_cast<T
>(1) << (
sizeof(T) * 8 - 1)) && (b << 1) <= a) {
96 ++num_digits_remaining;
105 for (; num_digits_remaining; --num_digits_remaining) {
132 template <
typename T>
static inline T udiv(T a, T b) {
141 template <
typename T>
static inline T umod(T a, T b) {
160 typedef unsigned long long type;
164 template <
typename T>
167 UT int_min =
static_cast<UT
>(1) <<
sizeof(UT) * 8 - 1;
168 UT ua =
static_cast<UT
>(a);
169 return (a >= 0 || ua == int_min) ? ua :
static_cast<UT
>(-a);
172 template <
typename T>
static inline T
div(T a, T b) {
174 T u =
static_cast<T
>(safe_abs(a) / safe_abs(b));
177 return (a < 0 != b < 0) ? -u : u;
180 template <
typename T>
static inline T mod(T a, T b) {
182 T u =
static_cast<T
>(safe_abs(a) % safe_abs(b));
185 return a < 0 ? -u : u;
188 template <
typename T>
static inline T divmod(T a, T b, T *rem) {
191 T uq =
static_cast<T
>(udivmod(safe_abs(a), safe_abs(b), &urem));
195 *rem = a < 0 ? -urem : urem;
196 return (a < 0 != b < 0) ? -uq : uq;