blob: c102158cd0cf099807e9c1630a1ab69599a1473d [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.
Chia-I Wu44e42362014-09-02 08:32:09 +080023 *
24 * Authors:
25 * Chia-I Wu <olv@lunarg.com>
Chia-I Wu69abeac2014-08-16 12:43:53 +080026 */
27
28#ifndef ICD_UTILS_H
29#define ICD_UTILS_H
30
Chia-I Wud3e77a62014-08-18 14:39:31 +080031#include <stdbool.h>
32#include <stdint.h>
Chia-I Wu69abeac2014-08-16 12:43:53 +080033#include <assert.h>
34#include "icd.h"
35
36#define ARRAY_SIZE(a) (sizeof(a) / sizeof(a[0]))
37
GregF8a814422014-11-10 17:26:41 -070038#define u_popcount(u) __builtin_popcount(u)
39
Chia-I Wu69abeac2014-08-16 12:43:53 +080040#define STATIC_ASSERT(expr) do { \
41 (void) sizeof(char[1 - 2 * !(expr)]); \
42} while (0)
43
Chia-I Wud3e77a62014-08-18 14:39:31 +080044#define U_CLAMP(val, min, max) \
45 ((val) < (min) ? (min) : (val) > (max)? (max) : (val))
46
Chia-I Wu69abeac2014-08-16 12:43:53 +080047/**
48 * Return true if val is power of two, or zero.
49 */
50static inline bool u_is_pow2(unsigned int val)
51{
52 return ((val & (val - 1)) == 0);
53}
54
55static inline unsigned int u_align(unsigned int val, unsigned alignment)
56{
57 assert(alignment && u_is_pow2(alignment));
58 return (val + alignment - 1) & ~(alignment - 1);
59}
60
61static inline unsigned int u_minify(unsigned int val, unsigned level)
62{
63 return (val >> level) ? val >> level : 1;
64}
65
Chia-I Wud3e77a62014-08-18 14:39:31 +080066static inline uint32_t u_fui(float f)
67{
68 union {
69 float f;
70 uint32_t ui;
71 } u = { .f = f };
72
73 return u.ui;
74}
75
Chia-I Wub3a3ccf2014-09-09 00:23:07 +080076static inline float u_uif(uint32_t ui)
77{
78 union {
79 float f;
80 uint32_t ui;
81 } u = { .ui = ui };
82
83 return u.f;
84}
85
Chia-I Wud3e77a62014-08-18 14:39:31 +080086static inline int u_iround(float f)
87{
88 if (f >= 0.0f)
89 return (int) (f + 0.5f);
90 else
91 return (int) (f - 0.5f);
92}
93
94uint16_t u_float_to_half(float f);
95
Chia-I Wu69abeac2014-08-16 12:43:53 +080096#endif /* ICD_UTILS_H */