summaryrefslogtreecommitdiff
path: root/include/fitz/math.h
blob: bfc1f8e60f3bdf438036c7e3b6a30432fe3a757b (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
/* multiply 8-bit fixpoint (0..1) so that 0*0==0 and 255*255==255 */
static inline unsigned char fz_mul255(unsigned char a, unsigned char b)
{
	return ((a + 1) * b) >> 8;
}

/* floor / ceil towards/from +/- inf */
static inline float fz_floor(float x)
{
	if (x > 0)
		return floor(x);
	return ceil(x);
}

static inline float fz_ceil(float x)
{
	if (x > 0)
		return ceil(x);
	return floor(x);
}

/* divide and floor towards -inf */
static inline int fz_idiv(int a, int b)
{
	return a < 0 ? (a - b + 1) / b : a / b;
}

/* from python */
static inline void fz_idivmod(int x, int y, int *d, int *m)
{
	int xdivy = x / y;
	int xmody = x - xdivy * y;
	/* If the signs of x and y differ, and the remainder is non-0,
	 * C89 doesn't define whether xdivy is now the floor or the
	 * ceiling of the infinitely precise quotient.  We want the floor,
	 * and we have it iff the remainder's sign matches y's.
	 */
	if (xmody && ((y ^ xmody) < 0)) {
		xmody += y;
		xdivy --;
	}
	*d = xdivy;
	*m = xmody;
}