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