• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 #ifndef ISL_PRIV_H
25 #define ISL_PRIV_H
26 
27 #include <assert.h>
28 #include <strings.h>
29 
30 #include "common/gen_device_info.h"
31 #include "util/macros.h"
32 
33 #include "isl.h"
34 
35 #define isl_finishme(format, ...) \
36    do { \
37       static bool reported = false; \
38       if (!reported) { \
39          __isl_finishme(__FILE__, __LINE__, format, ##__VA_ARGS__); \
40          reported = true; \
41       } \
42    } while (0)
43 
44 void PRINTFLIKE(3, 4) UNUSED
45 __isl_finishme(const char *file, int line, const char *fmt, ...);
46 
47 #define MIN(a, b) ((a) < (b) ? (a) : (b))
48 #define MAX(a, b) ((a) > (b) ? (a) : (b))
49 
50 static inline bool
isl_is_pow2(uintmax_t n)51 isl_is_pow2(uintmax_t n)
52 {
53    return !(n & (n - 1));
54 }
55 
56 /**
57  * Alignment must be a power of 2.
58  */
59 static inline bool
isl_is_aligned(uintmax_t n,uintmax_t a)60 isl_is_aligned(uintmax_t n, uintmax_t a)
61 {
62    assert(isl_is_pow2(a));
63    return (n & (a - 1)) == 0;
64 }
65 
66 /**
67  * Alignment must be a power of 2.
68  */
69 static inline uintmax_t
isl_align(uintmax_t n,uintmax_t a)70 isl_align(uintmax_t n, uintmax_t a)
71 {
72    assert(a != 0 && isl_is_pow2(a));
73    return (n + a - 1) & ~(a - 1);
74 }
75 
76 static inline uintmax_t
isl_align_npot(uintmax_t n,uintmax_t a)77 isl_align_npot(uintmax_t n, uintmax_t a)
78 {
79    assert(a > 0);
80    return ((n + a - 1) / a) * a;
81 }
82 
83 static inline uintmax_t
isl_assert_div(uintmax_t n,uintmax_t a)84 isl_assert_div(uintmax_t n, uintmax_t a)
85 {
86    assert(n % a == 0);
87    return n / a;
88 }
89 
90 /**
91  * Alignment must be a power of 2.
92  */
93 static inline uintmax_t
isl_align_div(uintmax_t n,uintmax_t a)94 isl_align_div(uintmax_t n, uintmax_t a)
95 {
96    return isl_align(n, a) / a;
97 }
98 
99 static inline uintmax_t
isl_align_div_npot(uintmax_t n,uintmax_t a)100 isl_align_div_npot(uintmax_t n, uintmax_t a)
101 {
102    return isl_align_npot(n, a) / a;
103 }
104 
105 /**
106  * Log base 2, rounding towards zero.
107  */
108 static inline uint32_t
isl_log2u(uint32_t n)109 isl_log2u(uint32_t n)
110 {
111    assert(n != 0);
112    return 31 - __builtin_clz(n);
113 }
114 
115 static inline uint32_t
isl_round_up_to_power_of_two(uint32_t value)116 isl_round_up_to_power_of_two(uint32_t value)
117 {
118    if (value <= 1)
119       return value;
120 
121    return 1 << (32 - __builtin_clz(value - 1));
122 }
123 
124 static inline uint32_t
isl_minify(uint32_t n,uint32_t levels)125 isl_minify(uint32_t n, uint32_t levels)
126 {
127    if (unlikely(n == 0))
128       return 0;
129    else
130       return MAX(n >> levels, 1);
131 }
132 
133 static inline struct isl_extent3d
isl_extent3d_sa_to_el(enum isl_format fmt,struct isl_extent3d extent_sa)134 isl_extent3d_sa_to_el(enum isl_format fmt, struct isl_extent3d extent_sa)
135 {
136    const struct isl_format_layout *fmtl = isl_format_get_layout(fmt);
137 
138    assert(extent_sa.w % fmtl->bw == 0);
139    assert(extent_sa.h % fmtl->bh == 0);
140    assert(extent_sa.d % fmtl->bd == 0);
141 
142    return (struct isl_extent3d) {
143       .w = extent_sa.w / fmtl->bw,
144       .h = extent_sa.h / fmtl->bh,
145       .d = extent_sa.d / fmtl->bd,
146    };
147 }
148 
149 static inline struct isl_extent3d
isl_extent3d_el_to_sa(enum isl_format fmt,struct isl_extent3d extent_el)150 isl_extent3d_el_to_sa(enum isl_format fmt, struct isl_extent3d extent_el)
151 {
152    const struct isl_format_layout *fmtl = isl_format_get_layout(fmt);
153 
154    return (struct isl_extent3d) {
155       .w = extent_el.w * fmtl->bw,
156       .h = extent_el.h * fmtl->bh,
157       .d = extent_el.d * fmtl->bd,
158    };
159 }
160 
161 /* This is useful for adding the isl_prefix to genX functions */
162 #define __PASTE2(x, y) x ## y
163 #define __PASTE(x, y) __PASTE2(x, y)
164 #define isl_genX(x) __PASTE(isl_, genX(x))
165 
166 #ifdef genX
167 #  include "isl_genX_priv.h"
168 #else
169 #  define genX(x) gen4_##x
170 #  include "isl_genX_priv.h"
171 #  undef genX
172 #  define genX(x) gen5_##x
173 #  include "isl_genX_priv.h"
174 #  undef genX
175 #  define genX(x) gen6_##x
176 #  include "isl_genX_priv.h"
177 #  undef genX
178 #  define genX(x) gen7_##x
179 #  include "isl_genX_priv.h"
180 #  undef genX
181 #  define genX(x) gen75_##x
182 #  include "isl_genX_priv.h"
183 #  undef genX
184 #  define genX(x) gen8_##x
185 #  include "isl_genX_priv.h"
186 #  undef genX
187 #  define genX(x) gen9_##x
188 #  include "isl_genX_priv.h"
189 #  undef genX
190 #  define genX(x) gen10_##x
191 #  include "isl_genX_priv.h"
192 #  undef genX
193 #endif
194 
195 #endif /* ISL_PRIV_H */
196