rs_core.c revision 5a47020542c52af3e879c1cd67674ca979ff0a18
1#include "rs_core.rsh"
2#include "rs_graphics.rsh"
3#include "rs_structs.h"
4
5/* Function declarations from libRS */
6extern float4 __attribute__((overloadable)) convert_float4(uchar4 c);
7
8/* Implementation of Core Runtime */
9
10/*
11extern 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
21extern 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
31extern 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
39extern 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
48extern float4 rsUnpackColor8888(uchar4 c)
49{
50    return convert_float4(c) * 0.003921569f;
51}
52
53
54extern 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
58extern 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
62extern int32_t __attribute__((overloadable)) rsAtomicInc(volatile int32_t *ptr) {
63    return __sync_fetch_and_add(ptr, 1);
64}
65
66extern int32_t __attribute__((overloadable)) rsAtomicDec(volatile int32_t *ptr) {
67    return __sync_fetch_and_sub(ptr, 1);
68}
69
70extern int32_t __attribute__((overloadable)) rsAtomicAdd(volatile int32_t *ptr, int32_t value) {
71    return __sync_fetch_and_add(ptr, value);
72}
73
74extern int32_t __attribute__((overloadable)) rsAtomicSub(volatile int32_t *ptr, int32_t value) {
75    return __sync_fetch_and_sub(ptr, value);
76}
77
78extern int32_t __attribute__((overloadable)) rsAtomicAnd(volatile int32_t *ptr, int32_t value) {
79    return __sync_fetch_and_and(ptr, value);
80}
81
82extern int32_t __attribute__((overloadable)) rsAtomicOr(volatile int32_t *ptr, int32_t value) {
83    return __sync_fetch_and_or(ptr, value);
84}
85
86extern int32_t __attribute__((overloadable)) rsAtomicXor(volatile int32_t *ptr, int32_t value) {
87    return __sync_fetch_and_xor(ptr, value);
88}
89
90extern uint32_t __attribute__((overloadable)) min(uint32_t, uint32_t);
91extern int32_t __attribute__((overloadable)) min(int32_t, int32_t);
92extern uint32_t __attribute__((overloadable)) max(uint32_t, uint32_t);
93extern int32_t __attribute__((overloadable)) max(int32_t, int32_t);
94
95extern 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
105extern 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
115extern 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
125extern 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
137extern int32_t rand();
138#define RAND_MAX 0x7fffffff
139
140
141
142extern 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
150extern 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
158extern int __attribute__((overloadable)) rsRand(int max) {
159    return (int)rsRand((float)max);
160}
161
162extern int __attribute__((overloadable)) rsRand(int min, int max) {
163    return (int)rsRand((float)min, (float)max);
164}
165
166#define PRIM_DEBUG(T)                               \
167extern void __attribute__((overloadable)) rsDebug(const char *, const T *);     \
168void __attribute__((overloadable)) rsDebug(const char *txt, T val) {            \
169    rsDebug(txt, &val);                                                         \
170}
171
172PRIM_DEBUG(char2)
173PRIM_DEBUG(char3)
174PRIM_DEBUG(char4)
175PRIM_DEBUG(uchar2)
176PRIM_DEBUG(uchar3)
177PRIM_DEBUG(uchar4)
178PRIM_DEBUG(short2)
179PRIM_DEBUG(short3)
180PRIM_DEBUG(short4)
181PRIM_DEBUG(ushort2)
182PRIM_DEBUG(ushort3)
183PRIM_DEBUG(ushort4)
184PRIM_DEBUG(int2)
185PRIM_DEBUG(int3)
186PRIM_DEBUG(int4)
187PRIM_DEBUG(uint2)
188PRIM_DEBUG(uint3)
189PRIM_DEBUG(uint4)
190PRIM_DEBUG(long2)
191PRIM_DEBUG(long3)
192PRIM_DEBUG(long4)
193PRIM_DEBUG(ulong2)
194PRIM_DEBUG(ulong3)
195PRIM_DEBUG(ulong4)
196PRIM_DEBUG(float2)
197PRIM_DEBUG(float3)
198PRIM_DEBUG(float4)
199PRIM_DEBUG(double2)
200PRIM_DEBUG(double3)
201PRIM_DEBUG(double4)
202
203#undef PRIM_DEBUG
204
205