GFXprim
2D bitmap graphics library with emphasis on speed and correctness
Loading...
Searching...
No Matches
include
core
gp_fixed_point.h
1
// SPDX-License-Identifier: LGPL-2.1-or-later
2
/*
3
* Copyright (C) 2009-2012 Cyril Hrubis <metan@ucw.cz>
4
*/
5
6
/*
7
8
Macros for fixed point arithmetic.
9
10
We use 8 bits for fractional part for coordinates and sizes for Anti Aliased
11
primitives.
12
13
*/
14
15
#ifndef CORE_GP_FIXED_POINT_H
16
#define CORE_GP_FIXED_POINT_H
17
18
#include <stdint.h>
19
20
/*
21
* Number of bits used for fractional part.
22
*/
23
#define GP_FP_FRAC_BITS 8
24
25
/*
26
* One
27
*/
28
#define GP_FP_1 (1<<GP_FP_FRAC_BITS)
29
30
/*
31
* One Half
32
*/
33
#define GP_FP_1_2 (1<<(GP_FP_FRAC_BITS - 1))
34
35
/*
36
* Fraction part bitmask.
37
*/
38
#define GP_FP_FRAC_MASK ((1<<GP_FP_FRAC_BITS)-1)
39
40
/*
41
* Integer part bitmask.
42
*/
43
#define GP_FP_INT_MASK (~GP_FP_FRAC_MASK)
44
45
/*
46
* Addition.
47
*/
48
#define GP_FP_ADD(a, b) ((a)+(b))
49
50
/*
51
* Substraction.
52
*/
53
#define GP_FP_SUB(a, b) ((a)-(b))
54
55
/*
56
* Multiplication, may overflow.
57
*/
58
#define GP_FP_MUL(a, b) ((((a) * (b)) + GP_FP_1_2)>>GP_FP_FRAC_BITS)
59
60
/*
61
* Division, may overflow
62
*/
63
#define GP_FP_DIV(a, b) ((((a)<<GP_FP_FRAC_BITS) + GP_FP_1_2) / (b))
64
65
/*
66
* Floor.
67
*/
68
#define GP_FP_FLOOR(a) ((a) & GP_FP_INT_MASK)
69
70
/*
71
* Floor with conversion to integer.
72
*/
73
#define GP_FP_TO_INT(a) ((a)>>GP_FP_FRAC_BITS)
74
#define GP_FP_FLOOR_TO_INT(a) GP_FP_TO_INT(a)
75
76
/*
77
* Ceiling.
78
*/
79
#define GP_FP_CEIL(a) (((a) & GP_FP_INT_MASK) + ((a) & GP_FP_FRAC_MASK) ? GP_FP_1 : 0)
80
81
/*
82
* Ceilling with conversion to integer.
83
*/
84
#define GP_FP_CEIL_TO_INT(a) (((a)>>GP_FP_FRAC_BITS) + !!(GP_FP_FRAC(a)))
85
86
/*
87
* Rounding.
88
*/
89
#define GP_FP_ROUND(a) GP_FP_FLOOR((a) + GP_FP_1_2)
90
91
/*
92
* Rounding with conversion to integer.
93
*/
94
#define GP_FP_ROUND_TO_INT(a) ((((a) + GP_FP_1_2))>>GP_FP_FRAC_BITS)
95
96
/*
97
* Fractional part.
98
*/
99
#define GP_FP_FRAC(a) ((a) & GP_FP_FRAC_MASK)
100
101
/*
102
* Reverse fractional part 1 - frac(x)
103
*/
104
#define GP_FP_RFRAC(a) (GP_FP_1 - GP_FP_FRAC(a))
105
106
/*
107
* Returns an float.
108
*/
109
#define GP_FP_TO_FLOAT(a) (((float)(a))/((float)GP_FP_1))
110
111
/*
112
* Returns fixed point from integer.
113
*/
114
#define GP_FP_FROM_INT(a) ((a)<<GP_FP_FRAC_BITS)
115
116
#endif
/* CORE_GP_FIXED_POINT_H */
Generated by
1.9.8