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. |
| 23 | */ |
| 24 | |
| 25 | #ifndef ICD_UTILS_H |
| 26 | #define ICD_UTILS_H |
| 27 | |
Chia-I Wu | d3e77a6 | 2014-08-18 14:39:31 +0800 | [diff] [blame^] | 28 | #include <stdbool.h> |
| 29 | #include <stdint.h> |
Chia-I Wu | 69abeac | 2014-08-16 12:43:53 +0800 | [diff] [blame] | 30 | #include <assert.h> |
| 31 | #include "icd.h" |
| 32 | |
| 33 | #define ARRAY_SIZE(a) (sizeof(a) / sizeof(a[0])) |
| 34 | |
| 35 | #define STATIC_ASSERT(expr) do { \ |
| 36 | (void) sizeof(char[1 - 2 * !(expr)]); \ |
| 37 | } while (0) |
| 38 | |
Chia-I Wu | d3e77a6 | 2014-08-18 14:39:31 +0800 | [diff] [blame^] | 39 | #define U_CLAMP(val, min, max) \ |
| 40 | ((val) < (min) ? (min) : (val) > (max)? (max) : (val)) |
| 41 | |
Chia-I Wu | 69abeac | 2014-08-16 12:43:53 +0800 | [diff] [blame] | 42 | /** |
| 43 | * Return true if val is power of two, or zero. |
| 44 | */ |
| 45 | static inline bool u_is_pow2(unsigned int val) |
| 46 | { |
| 47 | return ((val & (val - 1)) == 0); |
| 48 | } |
| 49 | |
| 50 | static inline unsigned int u_align(unsigned int val, unsigned alignment) |
| 51 | { |
| 52 | assert(alignment && u_is_pow2(alignment)); |
| 53 | return (val + alignment - 1) & ~(alignment - 1); |
| 54 | } |
| 55 | |
| 56 | static inline unsigned int u_minify(unsigned int val, unsigned level) |
| 57 | { |
| 58 | return (val >> level) ? val >> level : 1; |
| 59 | } |
| 60 | |
Chia-I Wu | d3e77a6 | 2014-08-18 14:39:31 +0800 | [diff] [blame^] | 61 | static inline uint32_t u_fui(float f) |
| 62 | { |
| 63 | union { |
| 64 | float f; |
| 65 | uint32_t ui; |
| 66 | } u = { .f = f }; |
| 67 | |
| 68 | return u.ui; |
| 69 | } |
| 70 | |
| 71 | static inline int u_iround(float f) |
| 72 | { |
| 73 | if (f >= 0.0f) |
| 74 | return (int) (f + 0.5f); |
| 75 | else |
| 76 | return (int) (f - 0.5f); |
| 77 | } |
| 78 | |
| 79 | uint16_t u_float_to_half(float f); |
| 80 | |
Chia-I Wu | 69abeac | 2014-08-16 12:43:53 +0800 | [diff] [blame] | 81 | #endif /* ICD_UTILS_H */ |