aboutsummaryrefslogtreecommitdiff
path: root/sys/libmath/loop.h
blob: a877d84d6b13ac3e00f7ef08697c43431090bba2 (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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
#pragma once

/* increment operator */
#define INC2(x) INC_##x
#define INC1(x) INC2(x)
#define INC(x)  INC1(x)

#define INC_0 1
#define INC_1 2
#define INC_2 3
#define INC_3 4
#define INC_4 5
#define INC_5 6
#define INC_6 7
#define INC_7 8
#define INC_8 9
#define INC_9 10
#define INC_10 11
#define INC_11 12
#define INC_12 13
#define INC_13 14
#define INC_14 15
#define INC_15 16

#define ROUNDBY(x, n) ((x) & ~((n)-1))

/* subtraction tables */
#define SUB2(x, y) SUB_##x##_##y
#define SUB1(x, y) SUB2(x, y)
#define SUB(x, y)  SUB1(x, y)
#define SUB_8_0 8
#define SUB_8_1 7
#define SUB_8_2 6
#define SUB_8_3 5
#define SUB_8_4 4
#define SUB_8_5 3
#define SUB_8_6 2
#define SUB_8_7 1
#define SUB_8_8 0
#define SUB_7_0 7
#define SUB_7_1 6
#define SUB_7_2 5
#define SUB_7_3 4
#define SUB_7_4 3
#define SUB_7_5 2
#define SUB_7_6 1
#define SUB_7_7 0
#define SUB_6_0 6
#define SUB_6_1 5
#define SUB_6_2 4
#define SUB_6_3 3
#define SUB_6_4 2
#define SUB_6_5 1
#define SUB_6_6 0
#define SUB_5_0 5
#define SUB_5_1 4
#define SUB_5_2 3
#define SUB_5_3 2
#define SUB_5_4 1
#define SUB_5_5 0
#define SUB_4_0 4
#define SUB_4_1 3
#define SUB_4_2 2
#define SUB_4_3 1
#define SUB_4_4 0
#define SUB_3_0 3
#define SUB_3_1 2
#define SUB_3_2 1
#define SUB_3_3 0
#define SUB_2_0 2
#define SUB_2_1 1
#define SUB_2_2 0
#define SUB_1_0 1
#define SUB_1_1 0

/* rounding operator */
#define ROUNDBY(x, n) ((x) & ~((n)-1))

/* loop unrolling (vertical) */
#define LOOP1(I,STMT,...)  STMT(I,__VA_ARGS__) 
#define LOOP2(I,STMT,...)  STMT(I,__VA_ARGS__) LOOP1(INC(I),STMT,__VA_ARGS__)
#define LOOP3(I,STMT,...)  STMT(I,__VA_ARGS__) LOOP2(INC(I),STMT,__VA_ARGS__)
#define LOOP4(I,STMT,...)  STMT(I,__VA_ARGS__) LOOP3(INC(I),STMT,__VA_ARGS__)
#define LOOP5(I,STMT,...)  STMT(I,__VA_ARGS__) LOOP4(INC(I),STMT,__VA_ARGS__)
#define LOOP6(I,STMT,...)  STMT(I,__VA_ARGS__) LOOP5(INC(I),STMT,__VA_ARGS__)
#define LOOP7(I,STMT,...)  STMT(I,__VA_ARGS__) LOOP6(INC(I),STMT,__VA_ARGS__)
#define LOOP8(I,STMT,...)  STMT(I,__VA_ARGS__) LOOP7(INC(I),STMT,__VA_ARGS__)
#define LOOP9(I,STMT,...)  STMT(I,__VA_ARGS__) LOOP8(INC(I),STMT,__VA_ARGS__)
#define LOOP10(I,STMT,...) STMT(I,__VA_ARGS__) LOOP9(INC(I),STMT,__VA_ARGS__)
#define LOOP11(I,STMT,...) STMT(I,__VA_ARGS__) LOOP10(INC(I),STMT,__VA_ARGS__)
#define LOOP12(I,STMT,...) STMT(I,__VA_ARGS__) LOOP11(INC(I),STMT,__VA_ARGS__)
#define LOOP13(I,STMT,...) STMT(I,__VA_ARGS__) LOOP12(INC(I),STMT,__VA_ARGS__)
#define LOOP14(I,STMT,...) STMT(I,__VA_ARGS__) LOOP13(INC(I),STMT,__VA_ARGS__)
#define LOOP15(I,STMT,...) STMT(I,__VA_ARGS__) LOOP14(INC(I),STMT,__VA_ARGS__)
#define LOOP16(I,STMT,...) STMT(I,__VA_ARGS__) LOOP15(INC(I),STMT,__VA_ARGS__)

#define _LOOP_(n,I,STMT,...) LOOP##n(I,STMT,__VA_ARGS__) 
#define LOOP(n,I,STMT,...) _LOOP_(n,I,STMT,__VA_ARGS__)

/* loop expansion (horizontal) */
#define EXPAND0(I,TERM,OP,...)
#define EXPAND1(I,TERM,OP,...) TERM(I,__VA_ARGS__)
#define EXPAND2(I,TERM,OP,...) TERM(I,__VA_ARGS__) OP EXPAND1(INC(I),TERM,OP,__VA_ARGS__)
#define EXPAND3(I,TERM,OP,...) TERM(I,__VA_ARGS__) OP EXPAND2(INC(I),TERM,OP,__VA_ARGS__)
#define EXPAND4(I,TERM,OP,...) TERM(I,__VA_ARGS__) OP EXPAND3(INC(I),TERM,OP,__VA_ARGS__)
#define EXPAND5(I,TERM,OP,...) TERM(I,__VA_ARGS__) OP EXPAND4(INC(I),TERM,OP,__VA_ARGS__)
#define EXPAND6(I,TERM,OP,...) TERM(I,__VA_ARGS__) OP EXPAND5(INC(I),TERM,OP,__VA_ARGS__)
#define EXPAND7(I,TERM,OP,...) TERM(I,__VA_ARGS__) OP EXPAND6(INC(I),TERM,OP,__VA_ARGS__)
#define EXPAND8(I,TERM,OP,...) TERM(I,__VA_ARGS__) OP EXPAND7(INC(I),TERM,OP,__VA_ARGS__)

#define _EXPAND_(n,I,TERM,OP,...)     EXPAND##n(I,TERM,OP,__VA_ARGS__) 
#define EXPAND(n,I,TERM,OP,...)      _EXPAND_(n,I,TERM,OP,__VA_ARGS__)
#define EXPAND_TRI1(n,I,TERM,OP,...)  EXPAND(n,I,TERM,OP,__VA_ARGS__) 
#define EXPAND_TRI(n,I,TERM,OP,...)   EXPAND_TRI1(SUB(n,I),I,TERM,OP,__VA_ARGS__)