blob: 5304d1581a57b362e4477749b2b6ae009a312202 [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>
Chia-I Wu7af7b9c2014-12-19 14:39:08 +080034#include <strings.h> /* for ffs() */
Chia-I Wu69abeac2014-08-16 12:43:53 +080035#include "icd.h"
36
Chia-I Wu00405912015-02-11 13:23:57 -070037#if defined(NDEBUG) && defined(__GNUC__)
38#define U_ASSERT_ONLY __attribute__((unused))
39#else
40#define U_ASSERT_ONLY
41#endif
42
Chia-I Wuaecf1fe2015-02-17 14:20:58 -070043#if defined(__GNUC__)
44#define likely(x) __builtin_expect(!!(x), 1)
45#define unlikely(x) __builtin_expect(!!(x), 0)
46#else
47#define likely(x) (x)
48#define unlikely(x) (x)
49#endif
50
Chia-I Wu69abeac2014-08-16 12:43:53 +080051#define ARRAY_SIZE(a) (sizeof(a) / sizeof(a[0]))
52
GregF8a814422014-11-10 17:26:41 -070053#define u_popcount(u) __builtin_popcount(u)
GregF8cd81832014-11-18 18:01:01 -070054#define u_popcountll(u) __builtin_popcountll(u)
GregF8a814422014-11-10 17:26:41 -070055
Chia-I Wu69abeac2014-08-16 12:43:53 +080056#define STATIC_ASSERT(expr) do { \
57 (void) sizeof(char[1 - 2 * !(expr)]); \
58} while (0)
59
Chia-I Wud3e77a62014-08-18 14:39:31 +080060#define U_CLAMP(val, min, max) \
61 ((val) < (min) ? (min) : (val) > (max)? (max) : (val))
62
Chia-I Wu69abeac2014-08-16 12:43:53 +080063/**
64 * Return true if val is power of two, or zero.
65 */
66static inline bool u_is_pow2(unsigned int val)
67{
68 return ((val & (val - 1)) == 0);
69}
70
Chia-I Wu7af7b9c2014-12-19 14:39:08 +080071static inline int u_ffs(int val)
72{
73 return ffs(val);
74}
75
Chia-I Wu69abeac2014-08-16 12:43:53 +080076static inline unsigned int u_align(unsigned int val, unsigned alignment)
77{
78 assert(alignment && u_is_pow2(alignment));
79 return (val + alignment - 1) & ~(alignment - 1);
80}
81
82static inline unsigned int u_minify(unsigned int val, unsigned level)
83{
84 return (val >> level) ? val >> level : 1;
85}
86
Chia-I Wud3e77a62014-08-18 14:39:31 +080087static inline uint32_t u_fui(float f)
88{
89 union {
90 float f;
91 uint32_t ui;
92 } u = { .f = f };
93
94 return u.ui;
95}
96
Chia-I Wub3a3ccf2014-09-09 00:23:07 +080097static inline float u_uif(uint32_t ui)
98{
99 union {
100 float f;
101 uint32_t ui;
102 } u = { .ui = ui };
103
104 return u.f;
105}
106
Chia-I Wud3e77a62014-08-18 14:39:31 +0800107static inline int u_iround(float f)
108{
109 if (f >= 0.0f)
110 return (int) (f + 0.5f);
111 else
112 return (int) (f - 0.5f);
113}
114
115uint16_t u_float_to_half(float f);
116
Chia-I Wu69abeac2014-08-16 12:43:53 +0800117#endif /* ICD_UTILS_H */