1 #include "rs_core.rsh" 2 #include "rs_structs.h" 3 4 /* Function declarations from libRS */ 5 extern float4 __attribute__((overloadable)) convert_float4(uchar4 c); 6 7 /* Implementation of Core Runtime */ 8 9 extern float4 rsUnpackColor8888(uchar4 c) 10 { 11 return convert_float4(c) * 0.003921569f; 12 } 13 14 15 extern float __attribute__((overloadable)) rsClamp(float v, float l, float h) { 16 return clamp(v, l, h); 17 } 18 extern char __attribute__((overloadable)) rsClamp(char v, char l, char h) { 19 return clamp(v, l, h); 20 } 21 extern uchar __attribute__((overloadable)) rsClamp(uchar v, uchar l, uchar h) { 22 return clamp(v, l, h); 23 } 24 extern short __attribute__((overloadable)) rsClamp(short v, short l, short h) { 25 return clamp(v, l, h); 26 } 27 extern ushort __attribute__((overloadable)) rsClamp(ushort v, ushort l, ushort h) { 28 return clamp(v, l, h); 29 } 30 extern int __attribute__((overloadable)) rsClamp(int v, int l, int h) { 31 return clamp(v, l, h); 32 } 33 extern uint __attribute__((overloadable)) rsClamp(uint v, uint l, uint h) { 34 return clamp(v, l, h); 35 } 36 37 extern int32_t __attribute__((overloadable)) rsAtomicCas(volatile int32_t *ptr, int32_t expectedValue, int32_t newValue) { 38 return __sync_val_compare_and_swap(ptr, expectedValue, newValue); 39 } 40 41 extern uint32_t __attribute__((overloadable)) rsAtomicCas(volatile uint32_t *ptr, uint32_t expectedValue, uint32_t newValue) { 42 return __sync_val_compare_and_swap(ptr, expectedValue, newValue); 43 } 44 45 extern int32_t __attribute__((overloadable)) rsAtomicInc(volatile int32_t *ptr) { 46 return __sync_fetch_and_add(ptr, 1); 47 } 48 49 extern int32_t __attribute__((overloadable)) rsAtomicInc(volatile uint32_t *ptr) { 50 return __sync_fetch_and_add(ptr, 1); 51 } 52 53 extern int32_t __attribute__((overloadable)) rsAtomicDec(volatile int32_t *ptr) { 54 return __sync_fetch_and_sub(ptr, 1); 55 } 56 57 extern int32_t __attribute__((overloadable)) rsAtomicDec(volatile uint32_t *ptr) { 58 return __sync_fetch_and_sub(ptr, 1); 59 } 60 61 extern int32_t __attribute__((overloadable)) rsAtomicAdd(volatile int32_t *ptr, int32_t value) { 62 return __sync_fetch_and_add(ptr, value); 63 } 64 65 extern int32_t __attribute__((overloadable)) rsAtomicAdd(volatile uint32_t *ptr, uint32_t value) { 66 return __sync_fetch_and_add(ptr, value); 67 } 68 69 extern int32_t __attribute__((overloadable)) rsAtomicSub(volatile int32_t *ptr, int32_t value) { 70 return __sync_fetch_and_sub(ptr, value); 71 } 72 73 extern int32_t __attribute__((overloadable)) rsAtomicSub(volatile uint32_t *ptr, uint32_t value) { 74 return __sync_fetch_and_sub(ptr, value); 75 } 76 77 extern int32_t __attribute__((overloadable)) rsAtomicAnd(volatile int32_t *ptr, int32_t value) { 78 return __sync_fetch_and_and(ptr, value); 79 } 80 81 extern int32_t __attribute__((overloadable)) rsAtomicAnd(volatile uint32_t *ptr, uint32_t value) { 82 return __sync_fetch_and_and(ptr, value); 83 } 84 85 extern int32_t __attribute__((overloadable)) rsAtomicOr(volatile int32_t *ptr, int32_t value) { 86 return __sync_fetch_and_or(ptr, value); 87 } 88 89 extern int32_t __attribute__((overloadable)) rsAtomicOr(volatile uint32_t *ptr, uint32_t value) { 90 return __sync_fetch_and_or(ptr, value); 91 } 92 93 extern int32_t __attribute__((overloadable)) rsAtomicXor(volatile int32_t *ptr, int32_t value) { 94 return __sync_fetch_and_xor(ptr, value); 95 } 96 97 extern int32_t __attribute__((overloadable)) rsAtomicXor(volatile uint32_t *ptr, uint32_t value) { 98 return __sync_fetch_and_xor(ptr, value); 99 } 100 101 extern uint32_t __attribute__((overloadable)) min(uint32_t, uint32_t); 102 extern int32_t __attribute__((overloadable)) min(int32_t, int32_t); 103 extern uint32_t __attribute__((overloadable)) max(uint32_t, uint32_t); 104 extern int32_t __attribute__((overloadable)) max(int32_t, int32_t); 105 106 extern uint32_t __attribute__((overloadable)) rsAtomicMin(volatile uint32_t *ptr, uint32_t value) { 107 uint32_t prev, status; 108 do { 109 prev = *ptr; 110 uint32_t n = min(value, prev); 111 status = __sync_val_compare_and_swap(ptr, prev, n); 112 } while (status != prev); 113 return prev; 114 } 115 116 extern int32_t __attribute__((overloadable)) rsAtomicMin(volatile int32_t *ptr, int32_t value) { 117 int32_t prev, status; 118 do { 119 prev = *ptr; 120 int32_t n = min(value, prev); 121 status = __sync_val_compare_and_swap(ptr, prev, n); 122 } while (status != prev); 123 return prev; 124 } 125 126 extern uint32_t __attribute__((overloadable)) rsAtomicMax(volatile uint32_t *ptr, uint32_t value) { 127 uint32_t prev, status; 128 do { 129 prev = *ptr; 130 uint32_t n = max(value, prev); 131 status = __sync_val_compare_and_swap(ptr, prev, n); 132 } while (status != prev); 133 return prev; 134 } 135 136 extern int32_t __attribute__((overloadable)) rsAtomicMax(volatile int32_t *ptr, int32_t value) { 137 int32_t prev, status; 138 do { 139 prev = *ptr; 140 int32_t n = max(value, prev); 141 status = __sync_val_compare_and_swap(ptr, prev, n); 142 } while (status != prev); 143 return prev; 144 } 145 146 147 148 extern int32_t rand(); 149 #define RAND_MAX 0x7fffffff 150 151 152 153 extern float __attribute__((overloadable)) rsRand(float min, float max);/* { 154 float r = (float)rand(); 155 r /= RAND_MAX; 156 r = r * (max - min) + min; 157 return r; 158 } 159 */ 160 161 extern float __attribute__((overloadable)) rsRand(float max) { 162 return rsRand(0.f, max); 163 //float r = (float)rand(); 164 //r *= max; 165 //r /= RAND_MAX; 166 //return r; 167 } 168 169 extern int __attribute__((overloadable)) rsRand(int max) { 170 return (int)rsRand((float)max); 171 } 172 173 extern int __attribute__((overloadable)) rsRand(int min, int max) { 174 return (int)rsRand((float)min, (float)max); 175 } 176 177 #define PRIM_DEBUG(T) \ 178 extern void __attribute__((overloadable)) rsDebug(const char *, const T *); \ 179 void __attribute__((overloadable)) rsDebug(const char *txt, T val) { \ 180 rsDebug(txt, &val); \ 181 } 182 183 PRIM_DEBUG(char2) 184 PRIM_DEBUG(char3) 185 PRIM_DEBUG(char4) 186 PRIM_DEBUG(uchar2) 187 PRIM_DEBUG(uchar3) 188 PRIM_DEBUG(uchar4) 189 PRIM_DEBUG(short2) 190 PRIM_DEBUG(short3) 191 PRIM_DEBUG(short4) 192 PRIM_DEBUG(ushort2) 193 PRIM_DEBUG(ushort3) 194 PRIM_DEBUG(ushort4) 195 PRIM_DEBUG(int2) 196 PRIM_DEBUG(int3) 197 PRIM_DEBUG(int4) 198 PRIM_DEBUG(uint2) 199 PRIM_DEBUG(uint3) 200 PRIM_DEBUG(uint4) 201 PRIM_DEBUG(long2) 202 PRIM_DEBUG(long3) 203 PRIM_DEBUG(long4) 204 PRIM_DEBUG(ulong2) 205 PRIM_DEBUG(ulong3) 206 PRIM_DEBUG(ulong4) 207 PRIM_DEBUG(float2) 208 PRIM_DEBUG(float3) 209 PRIM_DEBUG(float4) 210 PRIM_DEBUG(double2) 211 PRIM_DEBUG(double3) 212 PRIM_DEBUG(double4) 213 214 #undef PRIM_DEBUG 215 216