Chia-I Wu | 69abeac | 2014-08-16 12:43:53 +0800 | [diff] [blame] | 1 | /* |
| 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 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> |
Chia-I Wu | 7af7b9c | 2014-12-19 14:39:08 +0800 | [diff] [blame^] | 34 | #include <strings.h> /* for ffs() */ |
Chia-I Wu | 69abeac | 2014-08-16 12:43:53 +0800 | [diff] [blame] | 35 | #include "icd.h" |
| 36 | |
| 37 | #define ARRAY_SIZE(a) (sizeof(a) / sizeof(a[0])) |
| 38 | |
GregF | 8a81442 | 2014-11-10 17:26:41 -0700 | [diff] [blame] | 39 | #define u_popcount(u) __builtin_popcount(u) |
GregF | 8cd8183 | 2014-11-18 18:01:01 -0700 | [diff] [blame] | 40 | #define u_popcountll(u) __builtin_popcountll(u) |
GregF | 8a81442 | 2014-11-10 17:26:41 -0700 | [diff] [blame] | 41 | |
Chia-I Wu | 69abeac | 2014-08-16 12:43:53 +0800 | [diff] [blame] | 42 | #define STATIC_ASSERT(expr) do { \ |
| 43 | (void) sizeof(char[1 - 2 * !(expr)]); \ |
| 44 | } while (0) |
| 45 | |
Chia-I Wu | d3e77a6 | 2014-08-18 14:39:31 +0800 | [diff] [blame] | 46 | #define U_CLAMP(val, min, max) \ |
| 47 | ((val) < (min) ? (min) : (val) > (max)? (max) : (val)) |
| 48 | |
Chia-I Wu | 69abeac | 2014-08-16 12:43:53 +0800 | [diff] [blame] | 49 | /** |
| 50 | * Return true if val is power of two, or zero. |
| 51 | */ |
| 52 | static inline bool u_is_pow2(unsigned int val) |
| 53 | { |
| 54 | return ((val & (val - 1)) == 0); |
| 55 | } |
| 56 | |
Chia-I Wu | 7af7b9c | 2014-12-19 14:39:08 +0800 | [diff] [blame^] | 57 | static inline int u_ffs(int val) |
| 58 | { |
| 59 | return ffs(val); |
| 60 | } |
| 61 | |
Chia-I Wu | 69abeac | 2014-08-16 12:43:53 +0800 | [diff] [blame] | 62 | static inline unsigned int u_align(unsigned int val, unsigned alignment) |
| 63 | { |
| 64 | assert(alignment && u_is_pow2(alignment)); |
| 65 | return (val + alignment - 1) & ~(alignment - 1); |
| 66 | } |
| 67 | |
| 68 | static inline unsigned int u_minify(unsigned int val, unsigned level) |
| 69 | { |
| 70 | return (val >> level) ? val >> level : 1; |
| 71 | } |
| 72 | |
Chia-I Wu | d3e77a6 | 2014-08-18 14:39:31 +0800 | [diff] [blame] | 73 | static inline uint32_t u_fui(float f) |
| 74 | { |
| 75 | union { |
| 76 | float f; |
| 77 | uint32_t ui; |
| 78 | } u = { .f = f }; |
| 79 | |
| 80 | return u.ui; |
| 81 | } |
| 82 | |
Chia-I Wu | b3a3ccf | 2014-09-09 00:23:07 +0800 | [diff] [blame] | 83 | static inline float u_uif(uint32_t ui) |
| 84 | { |
| 85 | union { |
| 86 | float f; |
| 87 | uint32_t ui; |
| 88 | } u = { .ui = ui }; |
| 89 | |
| 90 | return u.f; |
| 91 | } |
| 92 | |
Chia-I Wu | d3e77a6 | 2014-08-18 14:39:31 +0800 | [diff] [blame] | 93 | static inline int u_iround(float f) |
| 94 | { |
| 95 | if (f >= 0.0f) |
| 96 | return (int) (f + 0.5f); |
| 97 | else |
| 98 | return (int) (f - 0.5f); |
| 99 | } |
| 100 | |
| 101 | uint16_t u_float_to_half(float f); |
| 102 | |
Chia-I Wu | 69abeac | 2014-08-16 12:43:53 +0800 | [diff] [blame] | 103 | #endif /* ICD_UTILS_H */ |