blob: 6e164439445b3032cad9f77d5b65703ff587a1ab [file] [log] [blame]
Chia-I Wu69abeac2014-08-16 12:43:53 +08001/*
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -06002 * Vulkan
Chia-I Wu69abeac2014-08-16 12:43:53 +08003 *
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>
David Pinedo0257fbf2015-02-02 18:02:40 -070034#if defined(PLATFORM_LINUX)
Chia-I Wu7af7b9c2014-12-19 14:39:08 +080035#include <strings.h> /* for ffs() */
David Pinedo0257fbf2015-02-02 18:02:40 -070036#endif
Chia-I Wu69abeac2014-08-16 12:43:53 +080037#include "icd.h"
38
Chia-I Wu00405912015-02-11 13:23:57 -070039#if defined(NDEBUG) && defined(__GNUC__)
40#define U_ASSERT_ONLY __attribute__((unused))
41#else
42#define U_ASSERT_ONLY
43#endif
44
Chia-I Wuaecf1fe2015-02-17 14:20:58 -070045#if defined(__GNUC__)
46#define likely(x) __builtin_expect(!!(x), 1)
47#define unlikely(x) __builtin_expect(!!(x), 0)
48#else
49#define likely(x) (x)
50#define unlikely(x) (x)
51#endif
52
Chia-I Wu69abeac2014-08-16 12:43:53 +080053#define ARRAY_SIZE(a) (sizeof(a) / sizeof(a[0]))
54
GregF8a814422014-11-10 17:26:41 -070055#define u_popcount(u) __builtin_popcount(u)
GregF8cd81832014-11-18 18:01:01 -070056#define u_popcountll(u) __builtin_popcountll(u)
GregF8a814422014-11-10 17:26:41 -070057
Chia-I Wu69abeac2014-08-16 12:43:53 +080058#define STATIC_ASSERT(expr) do { \
59 (void) sizeof(char[1 - 2 * !(expr)]); \
60} while (0)
61
Chia-I Wud3e77a62014-08-18 14:39:31 +080062#define U_CLAMP(val, min, max) \
63 ((val) < (min) ? (min) : (val) > (max)? (max) : (val))
64
Chia-I Wu69abeac2014-08-16 12:43:53 +080065/**
66 * Return true if val is power of two, or zero.
67 */
Ian Elliotta742a622015-02-18 12:38:04 -070068static inline bool u_is_pow2(unsigned int val)
Chia-I Wu69abeac2014-08-16 12:43:53 +080069{
70 return ((val & (val - 1)) == 0);
71}
72
Ian Elliotta742a622015-02-18 12:38:04 -070073static inline int u_ffs(int val)
Chia-I Wu7af7b9c2014-12-19 14:39:08 +080074{
David Pinedo0257fbf2015-02-02 18:02:40 -070075#if defined(PLATFORM_LINUX)
76 return ffs(val);
77#else
78 return __lzcnt(val) + 1;
79#endif
Chia-I Wu7af7b9c2014-12-19 14:39:08 +080080}
81
Ian Elliotta742a622015-02-18 12:38:04 -070082static inline unsigned int u_align(unsigned int val, unsigned alignment)
Chia-I Wu69abeac2014-08-16 12:43:53 +080083{
84 assert(alignment && u_is_pow2(alignment));
85 return (val + alignment - 1) & ~(alignment - 1);
86}
87
Ian Elliotta742a622015-02-18 12:38:04 -070088static inline unsigned int u_minify(unsigned int val, unsigned level)
Chia-I Wu69abeac2014-08-16 12:43:53 +080089{
90 return (val >> level) ? val >> level : 1;
91}
92
Ian Elliotta742a622015-02-18 12:38:04 -070093static inline uint32_t u_fui(float f)
Chia-I Wud3e77a62014-08-18 14:39:31 +080094{
95 union {
96 float f;
97 uint32_t ui;
98 } u = { .f = f };
99
100 return u.ui;
101}
102
Ian Elliotta742a622015-02-18 12:38:04 -0700103static inline float u_uif(uint32_t ui)
Chia-I Wub3a3ccf2014-09-09 00:23:07 +0800104{
105 union {
106 float f;
107 uint32_t ui;
108 } u = { .ui = ui };
109
110 return u.f;
111}
112
Ian Elliotta742a622015-02-18 12:38:04 -0700113static inline int u_iround(float f)
Chia-I Wud3e77a62014-08-18 14:39:31 +0800114{
115 if (f >= 0.0f)
116 return (int) (f + 0.5f);
117 else
118 return (int) (f - 0.5f);
119}
120
121uint16_t u_float_to_half(float f);
122
Chia-I Wu69abeac2014-08-16 12:43:53 +0800123#endif /* ICD_UTILS_H */