Chia-I Wu | 69abeac | 2014-08-16 12:43:53 +0800 | [diff] [blame] | 1 | /* |
Courtney Goeltzenleuchter | 9cc421e | 2015-04-08 15:36:08 -0600 | [diff] [blame] | 2 | * Vulkan |
Chia-I Wu | 69abeac | 2014-08-16 12:43:53 +0800 | [diff] [blame] | 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 Wu | 44e4236 | 2014-09-02 08:32:09 +0800 | [diff] [blame] | 23 | * |
| 24 | * Authors: |
| 25 | * Chia-I Wu <olv@lunarg.com> |
Chia-I Wu | 69abeac | 2014-08-16 12:43:53 +0800 | [diff] [blame] | 26 | */ |
| 27 | |
| 28 | #ifndef ICD_UTILS_H |
| 29 | #define ICD_UTILS_H |
| 30 | |
Chia-I Wu | d3e77a6 | 2014-08-18 14:39:31 +0800 | [diff] [blame] | 31 | #include <stdbool.h> |
| 32 | #include <stdint.h> |
Chia-I Wu | 69abeac | 2014-08-16 12:43:53 +0800 | [diff] [blame] | 33 | #include <assert.h> |
David Pinedo | 0257fbf | 2015-02-02 18:02:40 -0700 | [diff] [blame] | 34 | #if defined(PLATFORM_LINUX) |
Chia-I Wu | 7af7b9c | 2014-12-19 14:39:08 +0800 | [diff] [blame] | 35 | #include <strings.h> /* for ffs() */ |
David Pinedo | 0257fbf | 2015-02-02 18:02:40 -0700 | [diff] [blame] | 36 | #endif |
Chia-I Wu | 69abeac | 2014-08-16 12:43:53 +0800 | [diff] [blame] | 37 | #include "icd.h" |
| 38 | |
Chia-I Wu | 0040591 | 2015-02-11 13:23:57 -0700 | [diff] [blame] | 39 | #if defined(NDEBUG) && defined(__GNUC__) |
| 40 | #define U_ASSERT_ONLY __attribute__((unused)) |
| 41 | #else |
| 42 | #define U_ASSERT_ONLY |
| 43 | #endif |
| 44 | |
Chia-I Wu | aecf1fe | 2015-02-17 14:20:58 -0700 | [diff] [blame] | 45 | #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 Wu | 69abeac | 2014-08-16 12:43:53 +0800 | [diff] [blame] | 53 | #define ARRAY_SIZE(a) (sizeof(a) / sizeof(a[0])) |
| 54 | |
GregF | 8a81442 | 2014-11-10 17:26:41 -0700 | [diff] [blame] | 55 | #define u_popcount(u) __builtin_popcount(u) |
GregF | 8cd8183 | 2014-11-18 18:01:01 -0700 | [diff] [blame] | 56 | #define u_popcountll(u) __builtin_popcountll(u) |
GregF | 8a81442 | 2014-11-10 17:26:41 -0700 | [diff] [blame] | 57 | |
Chia-I Wu | 69abeac | 2014-08-16 12:43:53 +0800 | [diff] [blame] | 58 | #define STATIC_ASSERT(expr) do { \ |
| 59 | (void) sizeof(char[1 - 2 * !(expr)]); \ |
| 60 | } while (0) |
| 61 | |
Chia-I Wu | d3e77a6 | 2014-08-18 14:39:31 +0800 | [diff] [blame] | 62 | #define U_CLAMP(val, min, max) \ |
| 63 | ((val) < (min) ? (min) : (val) > (max)? (max) : (val)) |
| 64 | |
Chia-I Wu | 69abeac | 2014-08-16 12:43:53 +0800 | [diff] [blame] | 65 | /** |
| 66 | * Return true if val is power of two, or zero. |
| 67 | */ |
Ian Elliott | a742a62 | 2015-02-18 12:38:04 -0700 | [diff] [blame] | 68 | static inline bool u_is_pow2(unsigned int val) |
Chia-I Wu | 69abeac | 2014-08-16 12:43:53 +0800 | [diff] [blame] | 69 | { |
| 70 | return ((val & (val - 1)) == 0); |
| 71 | } |
| 72 | |
Ian Elliott | a742a62 | 2015-02-18 12:38:04 -0700 | [diff] [blame] | 73 | static inline int u_ffs(int val) |
Chia-I Wu | 7af7b9c | 2014-12-19 14:39:08 +0800 | [diff] [blame] | 74 | { |
David Pinedo | 0257fbf | 2015-02-02 18:02:40 -0700 | [diff] [blame] | 75 | #if defined(PLATFORM_LINUX) |
| 76 | return ffs(val); |
| 77 | #else |
| 78 | return __lzcnt(val) + 1; |
| 79 | #endif |
Chia-I Wu | 7af7b9c | 2014-12-19 14:39:08 +0800 | [diff] [blame] | 80 | } |
| 81 | |
Ian Elliott | a742a62 | 2015-02-18 12:38:04 -0700 | [diff] [blame] | 82 | static inline unsigned int u_align(unsigned int val, unsigned alignment) |
Chia-I Wu | 69abeac | 2014-08-16 12:43:53 +0800 | [diff] [blame] | 83 | { |
| 84 | assert(alignment && u_is_pow2(alignment)); |
| 85 | return (val + alignment - 1) & ~(alignment - 1); |
| 86 | } |
| 87 | |
Ian Elliott | a742a62 | 2015-02-18 12:38:04 -0700 | [diff] [blame] | 88 | static inline unsigned int u_minify(unsigned int val, unsigned level) |
Chia-I Wu | 69abeac | 2014-08-16 12:43:53 +0800 | [diff] [blame] | 89 | { |
| 90 | return (val >> level) ? val >> level : 1; |
| 91 | } |
| 92 | |
Ian Elliott | a742a62 | 2015-02-18 12:38:04 -0700 | [diff] [blame] | 93 | static inline uint32_t u_fui(float f) |
Chia-I Wu | d3e77a6 | 2014-08-18 14:39:31 +0800 | [diff] [blame] | 94 | { |
| 95 | union { |
| 96 | float f; |
| 97 | uint32_t ui; |
| 98 | } u = { .f = f }; |
| 99 | |
| 100 | return u.ui; |
| 101 | } |
| 102 | |
Ian Elliott | a742a62 | 2015-02-18 12:38:04 -0700 | [diff] [blame] | 103 | static inline float u_uif(uint32_t ui) |
Chia-I Wu | b3a3ccf | 2014-09-09 00:23:07 +0800 | [diff] [blame] | 104 | { |
| 105 | union { |
| 106 | float f; |
| 107 | uint32_t ui; |
| 108 | } u = { .ui = ui }; |
| 109 | |
| 110 | return u.f; |
| 111 | } |
| 112 | |
Ian Elliott | a742a62 | 2015-02-18 12:38:04 -0700 | [diff] [blame] | 113 | static inline int u_iround(float f) |
Chia-I Wu | d3e77a6 | 2014-08-18 14:39:31 +0800 | [diff] [blame] | 114 | { |
| 115 | if (f >= 0.0f) |
| 116 | return (int) (f + 0.5f); |
| 117 | else |
| 118 | return (int) (f - 0.5f); |
| 119 | } |
| 120 | |
| 121 | uint16_t u_float_to_half(float f); |
| 122 | |
Chia-I Wu | 69abeac | 2014-08-16 12:43:53 +0800 | [diff] [blame] | 123 | #endif /* ICD_UTILS_H */ |