Allan MacKinnon | 4359d52 | 2018-06-19 13:57:04 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2017 Google Inc. |
| 3 | * |
| 4 | * Use of this source code is governed by a BSD-style license that can |
| 5 | * be found in the LICENSE file. |
| 6 | * |
| 7 | */ |
| 8 | |
| 9 | #ifndef SKC_ONCE_MACROS |
| 10 | #define SKC_ONCE_MACROS |
| 11 | |
| 12 | // |
| 13 | // |
| 14 | // |
| 15 | |
| 16 | #include "types.h" |
| 17 | |
| 18 | // |
| 19 | // |
| 20 | // |
| 21 | |
| 22 | #define SKC_RESTRICT __restrict |
| 23 | |
| 24 | // |
| 25 | // |
| 26 | // |
| 27 | |
| 28 | #define SKC_CONSTANT __constant |
| 29 | #define SKC_GLOBAL __global |
| 30 | |
| 31 | // |
| 32 | // |
| 33 | // |
| 34 | |
| 35 | #define SKC_CALLBACK __stdcall |
| 36 | |
| 37 | // |
| 38 | // |
| 39 | // |
| 40 | |
| 41 | #define SKC_TRUE 1 |
| 42 | #define SKC_FALSE 0 |
| 43 | |
| 44 | // |
| 45 | // |
| 46 | // |
| 47 | |
| 48 | #define SKC_EMPTY |
| 49 | #define SKC_COMMA , |
| 50 | |
| 51 | // |
| 52 | // INDEX, SUFFIX, COMPONENT, PUNCTUATION, RGBA |
| 53 | // |
| 54 | |
| 55 | #define SKC_EXPAND_1() \ |
| 56 | SKC_EXPAND_X(0,SKC_EMPTY,SKC_EMPTY,SKC_EMPTY,SKC_EMPTY) |
| 57 | |
| 58 | #define SKC_EXPAND_2() \ |
| 59 | SKC_EXPAND_X(0, 0,.s0,SKC_COMMA,.even) \ |
| 60 | SKC_EXPAND_X(1, 1,.s1,SKC_EMPTY,.odd) |
| 61 | |
| 62 | #define SKC_EXPAND_4() \ |
| 63 | SKC_EXPAND_X(0, 0,.s0,SKC_COMMA,.even.even) \ |
| 64 | SKC_EXPAND_X(1, 1,.s1,SKC_COMMA,.odd.even) \ |
| 65 | SKC_EXPAND_X(2, 2,.s2,SKC_COMMA,.even.odd) \ |
| 66 | SKC_EXPAND_X(3, 3,.s3,SKC_EMPTY,.odd.odd) |
| 67 | |
| 68 | #define SKC_EXPAND_8() \ |
| 69 | SKC_EXPAND_X(0, 0,.s0,SKC_COMMA,SKC_EMPTY) \ |
| 70 | SKC_EXPAND_X(1, 1,.s1,SKC_COMMA,SKC_EMPTY) \ |
| 71 | SKC_EXPAND_X(2, 2,.s2,SKC_COMMA,SKC_EMPTY) \ |
| 72 | SKC_EXPAND_X(3, 3,.s3,SKC_COMMA,SKC_EMPTY) \ |
| 73 | SKC_EXPAND_X(4, 4,.s4,SKC_COMMA,SKC_EMPTY) \ |
| 74 | SKC_EXPAND_X(5, 5,.s5,SKC_COMMA,SKC_EMPTY) \ |
| 75 | SKC_EXPAND_X(6, 6,.s6,SKC_COMMA,SKC_EMPTY) \ |
| 76 | SKC_EXPAND_X(7, 7,.s7,SKC_EMPTY,SKC_EMPTY) |
| 77 | |
| 78 | #define SKC_EXPAND_16() \ |
| 79 | SKC_EXPAND_X(0, 0,.s0,SKC_COMMA,SKC_EMPTY) \ |
| 80 | SKC_EXPAND_X(1, 1,.s1,SKC_COMMA,SKC_EMPTY) \ |
| 81 | SKC_EXPAND_X(2, 2,.s2,SKC_COMMA,SKC_EMPTY) \ |
| 82 | SKC_EXPAND_X(3, 3,.s3,SKC_COMMA,SKC_EMPTY) \ |
| 83 | SKC_EXPAND_X(4, 4,.s4,SKC_COMMA,SKC_EMPTY) \ |
| 84 | SKC_EXPAND_X(5, 5,.s5,SKC_COMMA,SKC_EMPTY) \ |
| 85 | SKC_EXPAND_X(6, 6,.s6,SKC_COMMA,SKC_EMPTY) \ |
| 86 | SKC_EXPAND_X(7, 7,.s7,SKC_COMMA,SKC_EMPTY) \ |
| 87 | SKC_EXPAND_X(8, 8,.s8,SKC_COMMA,SKC_EMPTY) \ |
| 88 | SKC_EXPAND_X(9, 9,.s9,SKC_COMMA,SKC_EMPTY) \ |
| 89 | SKC_EXPAND_X(10,A,.sA,SKC_COMMA,SKC_EMPTY) \ |
| 90 | SKC_EXPAND_X(11,B,.sB,SKC_COMMA,SKC_EMPTY) \ |
| 91 | SKC_EXPAND_X(12,C,.sC,SKC_COMMA,SKC_EMPTY) \ |
| 92 | SKC_EXPAND_X(13,D,.sD,SKC_COMMA,SKC_EMPTY) \ |
| 93 | SKC_EXPAND_X(14,E,.sE,SKC_COMMA,SKC_EMPTY) \ |
| 94 | SKC_EXPAND_X(15,F,.sF,SKC_EMPTY,SKC_EMPTY) |
| 95 | |
| 96 | // |
| 97 | // |
| 98 | // |
| 99 | |
| 100 | #define SKC_BITS_TO_MASK(n) (((skc_uint)1<<(n))-1) |
| 101 | #define SKC_BITS_TO_MASK_64(n) (((skc_ulong)1<<(n))-1) |
| 102 | |
| 103 | #define SKC_BITS_TO_MASK_AT(n,b) (SKC_BITS_TO_MASK(n)<<(b)) |
| 104 | #define SKC_BITS_TO_MASK_AT_64(n,b) (SKC_BITS_TO_MASK_64(n)<<(b)) |
| 105 | |
| 106 | // |
| 107 | // IF BFE IS SUPPORTED BY THE PLATFORM THEN IMPLEMENT DIFFERENTLY |
| 108 | // |
| 109 | |
| 110 | #define SKC_BFE(x,n,i) (((x) & SKC_BITS_TO_MASK_AT(n,i)) >> (i)) |
| 111 | |
| 112 | // |
| 113 | // IF BFI IS SUPPORTED BY THE PLATFORM THEN IMPLEMENT DIFFERENTLY |
| 114 | // |
| 115 | // Note this BFI assumes the destination bits were already set to zero |
| 116 | // |
| 117 | |
| 118 | #define SKC_BFI(d,n,i,v) ((((v) & SKC_BITS_TO_MASK(n)) << (i)) | (d)) |
| 119 | |
| 120 | // |
| 121 | // |
| 122 | // |
| 123 | |
| 124 | #define SKC_STRINGIFY2(a) #a |
| 125 | #define SKC_STRINGIFY(a) SKC_STRINGIFY2(a) |
| 126 | |
| 127 | // |
| 128 | // |
| 129 | // |
| 130 | |
| 131 | #define SKC_EVAL(x) x |
| 132 | #define SKC_CONCAT(a,b) SKC_EVAL(a)##SKC_EVAL(b) |
| 133 | |
| 134 | // |
| 135 | // |
| 136 | // |
| 137 | |
| 138 | #define SKC_MAX_MACRO(a,b) (((a) > (b)) ? (a) : (b)) |
| 139 | #define SKC_MIN_MACRO(a,b) (((a) < (b)) ? (a) : (b)) |
| 140 | #define SKC_GTE_MACRO(a,b) ((a) >= (b)) |
| 141 | #define SKC_LT_MACRO(a,b) ((a) < (b)) |
| 142 | |
| 143 | // |
| 144 | // |
| 145 | // |
| 146 | |
| 147 | #define SKC_COUNT_OF(x) (sizeof(x) / sizeof(x)[0]) |
| 148 | #define SKC_OFFSET_OF(t,m) ((size_t)&(((t*)0)->m)) |
| 149 | #define SKC_MEMBER_SIZE(t,m) sizeof(((t*)0)->m) |
| 150 | |
| 151 | |
| 152 | // |
| 153 | // Returns rounded up next power-of-2 for non-zero -- via bit |
| 154 | // twiddling hacks, etc. |
| 155 | // |
| 156 | |
| 157 | #define SKC_P2_OR_RS(n,x) ((x)|(x)>>n) |
| 158 | #define SKC_POW2_RU_U32(x) (1+SKC_P2_OR_RS(16,SKC_P2_OR_RS(8,SKC_P2_OR_RS(4,SKC_P2_OR_RS(2,SKC_P2_OR_RS(1,x-1)))))) |
| 159 | |
| 160 | // |
| 161 | // Round up |
| 162 | // |
| 163 | |
| 164 | #define SKC_ROUND_DOWN(v,q) (((v) / (q)) * (q)) |
| 165 | #define SKC_ROUND_UP(v,q) ((((v) + (q) - 1) / (q)) * (q)) |
| 166 | |
| 167 | #define SKC_ROUND_DOWN_POW2(v,q) ((v) & ~((q) - 1)) |
| 168 | #define SKC_ROUND_UP_POW2(v,q) SKC_ROUND_DOWN_POW2((v) + (q) - 1,q) |
| 169 | |
| 170 | // |
| 171 | // |
| 172 | // |
| 173 | |
| 174 | #if !defined(__OPENCL_C_VERSION__) |
| 175 | |
| 176 | #define SKC_STATIC_ASSERT(p) static_assert((p),#p) |
| 177 | |
| 178 | #else |
| 179 | |
| 180 | #define SKC_STATIC_ASSERT(p) |
| 181 | |
| 182 | #endif |
| 183 | |
| 184 | // |
| 185 | // Returns 1-based bit position of MSB |
| 186 | // |
| 187 | |
| 188 | #define SKC_MSB_4(x,n) (((x>>(n-0))&1)+((x>>(n-1))&1)+((x>>(n-2))&1)+((x>>(n-3))&1)) |
| 189 | #define SKC_MSB_7654(x) SKC_MSB_4(x,31)+SKC_MSB_4(x,27)+SKC_MSB_4(x,23)+SKC_MSB_4(x,19) |
| 190 | #define SKC_MSB_3210(x) SKC_MSB_4(x,15)+SKC_MSB_4(x,11)+SKC_MSB_4(x, 7)+SKC_MSB_4(x, 3) |
| 191 | #define SKC_MSB_U32(x) ( SKC_MSB_7654(((skc_uint)x)) + SKC_MSB_3210(((skc_uint)x)) ) |
| 192 | |
| 193 | // |
| 194 | // FIXME -- handle other compilers besides MSVC/x86 |
| 195 | // |
| 196 | |
| 197 | #define SKC_LZCNT_32(v) __lzcnt(v) |
| 198 | |
| 199 | // |
| 200 | // |
| 201 | // |
| 202 | |
| 203 | #endif |
| 204 | |