blob: c6008520062b28dc7ae76c82269ea119b72f1494 [file] [log] [blame]
Chia-I Wu69abeac2014-08-16 12:43:53 +08001/*
2 * XGL
3 *
4 * Copyright (C) 2014 LunarG, Inc.
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining a
7 * copy of this software and associated documentation files (the "Software"),
8 * to deal in the Software without restriction, including without limitation
9 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
10 * and/or sell copies of the Software, and to permit persons to whom the
11 * Software is furnished to do so, subject to the following conditions:
12 *
13 * The above copyright notice and this permission notice shall be included
14 * in all copies or substantial portions of the Software.
15 *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
21 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
22 * DEALINGS IN THE SOFTWARE.
23 */
24
25#ifndef ICD_UTILS_H
26#define ICD_UTILS_H
27
28#include <assert.h>
29#include "icd.h"
30
31#define ARRAY_SIZE(a) (sizeof(a) / sizeof(a[0]))
32
33#define STATIC_ASSERT(expr) do { \
34 (void) sizeof(char[1 - 2 * !(expr)]); \
35} while (0)
36
37/**
38 * Return true if val is power of two, or zero.
39 */
40static inline bool u_is_pow2(unsigned int val)
41{
42 return ((val & (val - 1)) == 0);
43}
44
45static inline unsigned int u_align(unsigned int val, unsigned alignment)
46{
47 assert(alignment && u_is_pow2(alignment));
48 return (val + alignment - 1) & ~(alignment - 1);
49}
50
51static inline unsigned int u_minify(unsigned int val, unsigned level)
52{
53 return (val >> level) ? val >> level : 1;
54}
55
56#endif /* ICD_UTILS_H */