intel: Add a new "common" library for more code sharing
[mesa.git] / src / intel / isl / isl_priv.h
1 /*
2 * Copyright 2015 Intel Corporation
3 *
4 * Permission is hereby granted, free of charge, to any person obtaining a
5 * copy of this software and associated documentation files (the "Software"),
6 * to deal in the Software without restriction, including without limitation
7 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8 * and/or sell copies of the Software, and to permit persons to whom the
9 * Software is furnished to do so, subject to the following conditions:
10 *
11 * The above copyright notice and this permission notice (including the next
12 * paragraph) shall be included in all copies or substantial portions of the
13 * Software.
14 *
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
18 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
21 * IN THE SOFTWARE.
22 */
23
24 #pragma once
25
26 #include <assert.h>
27 #include <strings.h>
28
29 #include "common/brw_device_info.h"
30 #include "util/macros.h"
31
32 #include "isl.h"
33
34 #define isl_finishme(format, ...) \
35 __isl_finishme(__FILE__, __LINE__, format, ##__VA_ARGS__)
36
37 void PRINTFLIKE(3, 4) UNUSED
38 __isl_finishme(const char *file, int line, const char *fmt, ...);
39
40 #define MIN(a, b) ((a) < (b) ? (a) : (b))
41 #define MAX(a, b) ((a) > (b) ? (a) : (b))
42
43 static inline bool
44 isl_is_pow2(uintmax_t n)
45 {
46 return !(n & (n - 1));
47 }
48
49 /**
50 * Alignment must be a power of 2.
51 */
52 static inline bool
53 isl_is_aligned(uintmax_t n, uintmax_t a)
54 {
55 assert(isl_is_pow2(a));
56 return (n & (a - 1)) == 0;
57 }
58
59 /**
60 * Alignment must be a power of 2.
61 */
62 static inline uintmax_t
63 isl_align(uintmax_t n, uintmax_t a)
64 {
65 assert(a != 0 && isl_is_pow2(a));
66 return (n + a - 1) & ~(a - 1);
67 }
68
69 static inline uintmax_t
70 isl_align_npot(uintmax_t n, uintmax_t a)
71 {
72 assert(a > 0);
73 return ((n + a - 1) / a) * a;
74 }
75
76 /**
77 * Alignment must be a power of 2.
78 */
79 static inline uintmax_t
80 isl_align_div(uintmax_t n, uintmax_t a)
81 {
82 return isl_align(n, a) / a;
83 }
84
85 static inline uintmax_t
86 isl_align_div_npot(uintmax_t n, uintmax_t a)
87 {
88 return isl_align_npot(n, a) / a;
89 }
90
91 /**
92 * Log base 2, rounding towards zero.
93 */
94 static inline uint32_t
95 isl_log2u(uint32_t n)
96 {
97 assert(n != 0);
98 return 31 - __builtin_clz(n);
99 }
100
101 static inline uint32_t
102 isl_round_up_to_power_of_two(uint32_t value)
103 {
104 if (value <= 1)
105 return value;
106
107 return 1 << (32 - __builtin_clz(value - 1));
108 }
109
110 static inline uint32_t
111 isl_minify(uint32_t n, uint32_t levels)
112 {
113 if (unlikely(n == 0))
114 return 0;
115 else
116 return MAX(n >> levels, 1);
117 }
118
119 static inline struct isl_extent3d
120 isl_extent3d_sa_to_el(enum isl_format fmt, struct isl_extent3d extent_sa)
121 {
122 const struct isl_format_layout *fmtl = isl_format_get_layout(fmt);
123
124 assert(extent_sa.w % fmtl->bw == 0);
125 assert(extent_sa.h % fmtl->bh == 0);
126 assert(extent_sa.d % fmtl->bd == 0);
127
128 return (struct isl_extent3d) {
129 .w = extent_sa.w / fmtl->bw,
130 .h = extent_sa.h / fmtl->bh,
131 .d = extent_sa.d / fmtl->bd,
132 };
133 }
134
135 static inline struct isl_extent3d
136 isl_extent3d_el_to_sa(enum isl_format fmt, struct isl_extent3d extent_el)
137 {
138 const struct isl_format_layout *fmtl = isl_format_get_layout(fmt);
139
140 return (struct isl_extent3d) {
141 .w = extent_el.w * fmtl->bw,
142 .h = extent_el.h * fmtl->bh,
143 .d = extent_el.d * fmtl->bd,
144 };
145 }
146
147 void
148 isl_gen4_surf_fill_state_s(const struct isl_device *dev, void *state,
149 const struct isl_surf_fill_state_info *restrict info);
150
151 void
152 isl_gen5_surf_fill_state_s(const struct isl_device *dev, void *state,
153 const struct isl_surf_fill_state_info *restrict info);
154
155 void
156 isl_gen6_surf_fill_state_s(const struct isl_device *dev, void *state,
157 const struct isl_surf_fill_state_info *restrict info);
158
159 void
160 isl_gen7_surf_fill_state_s(const struct isl_device *dev, void *state,
161 const struct isl_surf_fill_state_info *restrict info);
162
163 void
164 isl_gen75_surf_fill_state_s(const struct isl_device *dev, void *state,
165 const struct isl_surf_fill_state_info *restrict info);
166 void
167 isl_gen8_surf_fill_state_s(const struct isl_device *dev, void *state,
168 const struct isl_surf_fill_state_info *restrict info);
169 void
170 isl_gen9_surf_fill_state_s(const struct isl_device *dev, void *state,
171 const struct isl_surf_fill_state_info *restrict info);
172
173 void
174 isl_gen4_buffer_fill_state_s(void *state,
175 const struct isl_buffer_fill_state_info *restrict info);
176
177 void
178 isl_gen5_buffer_fill_state_s(void *state,
179 const struct isl_buffer_fill_state_info *restrict info);
180
181 void
182 isl_gen6_buffer_fill_state_s(void *state,
183 const struct isl_buffer_fill_state_info *restrict info);
184
185 void
186 isl_gen7_buffer_fill_state_s(void *state,
187 const struct isl_buffer_fill_state_info *restrict info);
188
189 void
190 isl_gen75_buffer_fill_state_s(void *state,
191 const struct isl_buffer_fill_state_info *restrict info);
192
193 void
194 isl_gen8_buffer_fill_state_s(void *state,
195 const struct isl_buffer_fill_state_info *restrict info);
196
197 void
198 isl_gen9_buffer_fill_state_s(void *state,
199 const struct isl_buffer_fill_state_info *restrict info);