summaryrefslogtreecommitdiff
path: root/graphics/asymptote/mod.h
blob: 74525909141629dcf801807335d99c033020a3a6 (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
/*****
 * mod.h
 * Andy Hammerlindl 2005/03/16
 *
 * Definition of implementation-independent mod function.
 *****/

#ifndef MOD_H
#define MOD_H

#include <cmath>

#include "common.h"

using std::fmod;

inline Int Mod(Int x, Int y) {return x % y;}
inline double Mod(double x, double y) {return fmod(x,y);}

template<typename T>
inline T portableMod(T x,T y)
{
// Implementation-independent definition of mod; ensure result has
// same sign as divisor
  T val=Mod(x,y);
  return ((y > 0 && val >= 0) || (y < 0 && val <= 0)) ? val : val+y;
}

inline Int imod(Int x, size_t y) {
  if((y & (y-1)) == 0)
    return x & (y-1); // Use mask if y is a power of two
  x %= (Int) y;
  return (x >= 0) ? x : x+(Int) y;
}

inline Int imod(Int x, Int y)
{
  if(y > 0)
    return imod(x,(size_t) y);
  else
    return portableMod<Int>(x,y);
}

#endif