blob: 26f911e2c4c253bcbcf54ec00e90e24d07b9d274 [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
Chia-I Wud3e77a62014-08-18 14:39:31 +080028#include <stdbool.h>
29#include <stdint.h>
Chia-I Wu69abeac2014-08-16 12:43:53 +080030#include <assert.h>
31#include "icd.h"
32
33#define ARRAY_SIZE(a) (sizeof(a) / sizeof(a[0]))
34
35#define STATIC_ASSERT(expr) do { \
36 (void) sizeof(char[1 - 2 * !(expr)]); \
37} while (0)
38
Chia-I Wud3e77a62014-08-18 14:39:31 +080039#define U_CLAMP(val, min, max) \
40 ((val) < (min) ? (min) : (val) > (max)? (max) : (val))
41
Chia-I Wu69abeac2014-08-16 12:43:53 +080042/**
43 * Return true if val is power of two, or zero.
44 */
45static inline bool u_is_pow2(unsigned int val)
46{
47 return ((val & (val - 1)) == 0);
48}
49
50static inline unsigned int u_align(unsigned int val, unsigned alignment)
51{
52 assert(alignment && u_is_pow2(alignment));
53 return (val + alignment - 1) & ~(alignment - 1);
54}
55
56static inline unsigned int u_minify(unsigned int val, unsigned level)
57{
58 return (val >> level) ? val >> level : 1;
59}
60
Chia-I Wud3e77a62014-08-18 14:39:31 +080061static inline uint32_t u_fui(float f)
62{
63 union {
64 float f;
65 uint32_t ui;
66 } u = { .f = f };
67
68 return u.ui;
69}
70
71static inline int u_iround(float f)
72{
73 if (f >= 0.0f)
74 return (int) (f + 0.5f);
75 else
76 return (int) (f - 0.5f);
77}
78
79uint16_t u_float_to_half(float f);
80
Chia-I Wu69abeac2014-08-16 12:43:53 +080081#endif /* ICD_UTILS_H */