15a47020542c52af3e879c1cd67674ca979ff0a18Stephen Hines#include "rs_core.rsh" 25a47020542c52af3e879c1cd67674ca979ff0a18Stephen Hines#include "rs_graphics.rsh" 35a47020542c52af3e879c1cd67674ca979ff0a18Stephen Hines#include "rs_structs.h" 45a47020542c52af3e879c1cd67674ca979ff0a18Stephen Hines 55a47020542c52af3e879c1cd67674ca979ff0a18Stephen Hines/* Function declarations from libRS */ 65a47020542c52af3e879c1cd67674ca979ff0a18Stephen Hinesextern float4 __attribute__((overloadable)) convert_float4(uchar4 c); 75a47020542c52af3e879c1cd67674ca979ff0a18Stephen Hines 85a47020542c52af3e879c1cd67674ca979ff0a18Stephen Hines/* Implementation of Core Runtime */ 95a47020542c52af3e879c1cd67674ca979ff0a18Stephen Hines 105a47020542c52af3e879c1cd67674ca979ff0a18Stephen Hinesextern float4 rsUnpackColor8888(uchar4 c) 115a47020542c52af3e879c1cd67674ca979ff0a18Stephen Hines{ 125a47020542c52af3e879c1cd67674ca979ff0a18Stephen Hines return convert_float4(c) * 0.003921569f; 135a47020542c52af3e879c1cd67674ca979ff0a18Stephen Hines} 145a47020542c52af3e879c1cd67674ca979ff0a18Stephen Hines 155a47020542c52af3e879c1cd67674ca979ff0a18Stephen Hines 165a47020542c52af3e879c1cd67674ca979ff0a18Stephen Hinesextern int32_t __attribute__((overloadable)) rsAtomicCas(volatile int32_t *ptr, int32_t expectedValue, int32_t newValue) { 175a47020542c52af3e879c1cd67674ca979ff0a18Stephen Hines return __sync_val_compare_and_swap(ptr, expectedValue, newValue); 185a47020542c52af3e879c1cd67674ca979ff0a18Stephen Hines} 195a47020542c52af3e879c1cd67674ca979ff0a18Stephen Hines 205a47020542c52af3e879c1cd67674ca979ff0a18Stephen Hinesextern uint32_t __attribute__((overloadable)) rsAtomicCas(volatile uint32_t *ptr, uint32_t expectedValue, uint32_t newValue) { 215a47020542c52af3e879c1cd67674ca979ff0a18Stephen Hines return __sync_val_compare_and_swap((volatile int32_t *)ptr, (int32_t)expectedValue, (int32_t)newValue); 225a47020542c52af3e879c1cd67674ca979ff0a18Stephen Hines} 235a47020542c52af3e879c1cd67674ca979ff0a18Stephen Hines 245a47020542c52af3e879c1cd67674ca979ff0a18Stephen Hinesextern int32_t __attribute__((overloadable)) rsAtomicInc(volatile int32_t *ptr) { 255a47020542c52af3e879c1cd67674ca979ff0a18Stephen Hines return __sync_fetch_and_add(ptr, 1); 265a47020542c52af3e879c1cd67674ca979ff0a18Stephen Hines} 275a47020542c52af3e879c1cd67674ca979ff0a18Stephen Hines 285a47020542c52af3e879c1cd67674ca979ff0a18Stephen Hinesextern int32_t __attribute__((overloadable)) rsAtomicDec(volatile int32_t *ptr) { 295a47020542c52af3e879c1cd67674ca979ff0a18Stephen Hines return __sync_fetch_and_sub(ptr, 1); 305a47020542c52af3e879c1cd67674ca979ff0a18Stephen Hines} 315a47020542c52af3e879c1cd67674ca979ff0a18Stephen Hines 325a47020542c52af3e879c1cd67674ca979ff0a18Stephen Hinesextern int32_t __attribute__((overloadable)) rsAtomicAdd(volatile int32_t *ptr, int32_t value) { 335a47020542c52af3e879c1cd67674ca979ff0a18Stephen Hines return __sync_fetch_and_add(ptr, value); 345a47020542c52af3e879c1cd67674ca979ff0a18Stephen Hines} 355a47020542c52af3e879c1cd67674ca979ff0a18Stephen Hines 365a47020542c52af3e879c1cd67674ca979ff0a18Stephen Hinesextern int32_t __attribute__((overloadable)) rsAtomicSub(volatile int32_t *ptr, int32_t value) { 375a47020542c52af3e879c1cd67674ca979ff0a18Stephen Hines return __sync_fetch_and_sub(ptr, value); 385a47020542c52af3e879c1cd67674ca979ff0a18Stephen Hines} 395a47020542c52af3e879c1cd67674ca979ff0a18Stephen Hines 405a47020542c52af3e879c1cd67674ca979ff0a18Stephen Hinesextern int32_t __attribute__((overloadable)) rsAtomicAnd(volatile int32_t *ptr, int32_t value) { 415a47020542c52af3e879c1cd67674ca979ff0a18Stephen Hines return __sync_fetch_and_and(ptr, value); 425a47020542c52af3e879c1cd67674ca979ff0a18Stephen Hines} 435a47020542c52af3e879c1cd67674ca979ff0a18Stephen Hines 445a47020542c52af3e879c1cd67674ca979ff0a18Stephen Hinesextern int32_t __attribute__((overloadable)) rsAtomicOr(volatile int32_t *ptr, int32_t value) { 455a47020542c52af3e879c1cd67674ca979ff0a18Stephen Hines return __sync_fetch_and_or(ptr, value); 465a47020542c52af3e879c1cd67674ca979ff0a18Stephen Hines} 475a47020542c52af3e879c1cd67674ca979ff0a18Stephen Hines 485a47020542c52af3e879c1cd67674ca979ff0a18Stephen Hinesextern int32_t __attribute__((overloadable)) rsAtomicXor(volatile int32_t *ptr, int32_t value) { 495a47020542c52af3e879c1cd67674ca979ff0a18Stephen Hines return __sync_fetch_and_xor(ptr, value); 505a47020542c52af3e879c1cd67674ca979ff0a18Stephen Hines} 515a47020542c52af3e879c1cd67674ca979ff0a18Stephen Hines 525a47020542c52af3e879c1cd67674ca979ff0a18Stephen Hinesextern uint32_t __attribute__((overloadable)) min(uint32_t, uint32_t); 535a47020542c52af3e879c1cd67674ca979ff0a18Stephen Hinesextern int32_t __attribute__((overloadable)) min(int32_t, int32_t); 545a47020542c52af3e879c1cd67674ca979ff0a18Stephen Hinesextern uint32_t __attribute__((overloadable)) max(uint32_t, uint32_t); 555a47020542c52af3e879c1cd67674ca979ff0a18Stephen Hinesextern int32_t __attribute__((overloadable)) max(int32_t, int32_t); 565a47020542c52af3e879c1cd67674ca979ff0a18Stephen Hines 575a47020542c52af3e879c1cd67674ca979ff0a18Stephen Hinesextern uint32_t __attribute__((overloadable)) rsAtomicMin(volatile uint32_t *ptr, uint32_t value) { 585a47020542c52af3e879c1cd67674ca979ff0a18Stephen Hines uint32_t prev, status; 595a47020542c52af3e879c1cd67674ca979ff0a18Stephen Hines do { 605a47020542c52af3e879c1cd67674ca979ff0a18Stephen Hines prev = *ptr; 615a47020542c52af3e879c1cd67674ca979ff0a18Stephen Hines uint32_t n = min(value, prev); 625a47020542c52af3e879c1cd67674ca979ff0a18Stephen Hines status = rsAtomicCas((volatile int32_t*) ptr, (int32_t) prev, (int32_t)n); 635a47020542c52af3e879c1cd67674ca979ff0a18Stephen Hines } while (status != prev); 645a47020542c52af3e879c1cd67674ca979ff0a18Stephen Hines return prev; 655a47020542c52af3e879c1cd67674ca979ff0a18Stephen Hines} 665a47020542c52af3e879c1cd67674ca979ff0a18Stephen Hines 675a47020542c52af3e879c1cd67674ca979ff0a18Stephen Hinesextern int32_t __attribute__((overloadable)) rsAtomicMin(volatile int32_t *ptr, int32_t value) { 685a47020542c52af3e879c1cd67674ca979ff0a18Stephen Hines int32_t prev, status; 695a47020542c52af3e879c1cd67674ca979ff0a18Stephen Hines do { 705a47020542c52af3e879c1cd67674ca979ff0a18Stephen Hines prev = *ptr; 715a47020542c52af3e879c1cd67674ca979ff0a18Stephen Hines int32_t n = min(value, prev); 725a47020542c52af3e879c1cd67674ca979ff0a18Stephen Hines status = rsAtomicCas(ptr, prev, n); 735a47020542c52af3e879c1cd67674ca979ff0a18Stephen Hines } while (status != prev); 745a47020542c52af3e879c1cd67674ca979ff0a18Stephen Hines return prev; 755a47020542c52af3e879c1cd67674ca979ff0a18Stephen Hines} 765a47020542c52af3e879c1cd67674ca979ff0a18Stephen Hines 775a47020542c52af3e879c1cd67674ca979ff0a18Stephen Hinesextern uint32_t __attribute__((overloadable)) rsAtomicMax(volatile uint32_t *ptr, uint32_t value) { 785a47020542c52af3e879c1cd67674ca979ff0a18Stephen Hines uint32_t prev, status; 795a47020542c52af3e879c1cd67674ca979ff0a18Stephen Hines do { 805a47020542c52af3e879c1cd67674ca979ff0a18Stephen Hines prev = *ptr; 815a47020542c52af3e879c1cd67674ca979ff0a18Stephen Hines uint32_t n = max(value, prev); 825a47020542c52af3e879c1cd67674ca979ff0a18Stephen Hines status = rsAtomicCas((volatile int32_t*) ptr, (int32_t) prev, (int32_t) n); 835a47020542c52af3e879c1cd67674ca979ff0a18Stephen Hines } while (status != prev); 845a47020542c52af3e879c1cd67674ca979ff0a18Stephen Hines return prev; 855a47020542c52af3e879c1cd67674ca979ff0a18Stephen Hines} 865a47020542c52af3e879c1cd67674ca979ff0a18Stephen Hines 875a47020542c52af3e879c1cd67674ca979ff0a18Stephen Hinesextern int32_t __attribute__((overloadable)) rsAtomicMax(volatile int32_t *ptr, int32_t value) { 885a47020542c52af3e879c1cd67674ca979ff0a18Stephen Hines int32_t prev, status; 895a47020542c52af3e879c1cd67674ca979ff0a18Stephen Hines do { 905a47020542c52af3e879c1cd67674ca979ff0a18Stephen Hines prev = *ptr; 915a47020542c52af3e879c1cd67674ca979ff0a18Stephen Hines int32_t n = max(value, prev); 925a47020542c52af3e879c1cd67674ca979ff0a18Stephen Hines status = rsAtomicCas(ptr, prev, n); 935a47020542c52af3e879c1cd67674ca979ff0a18Stephen Hines } while (status != prev); 945a47020542c52af3e879c1cd67674ca979ff0a18Stephen Hines return prev; 955a47020542c52af3e879c1cd67674ca979ff0a18Stephen Hines} 965a47020542c52af3e879c1cd67674ca979ff0a18Stephen Hines 975a47020542c52af3e879c1cd67674ca979ff0a18Stephen Hines 985a47020542c52af3e879c1cd67674ca979ff0a18Stephen Hines 995a47020542c52af3e879c1cd67674ca979ff0a18Stephen Hinesextern int32_t rand(); 1005a47020542c52af3e879c1cd67674ca979ff0a18Stephen Hines#define RAND_MAX 0x7fffffff 1015a47020542c52af3e879c1cd67674ca979ff0a18Stephen Hines 1025a47020542c52af3e879c1cd67674ca979ff0a18Stephen Hines 1035a47020542c52af3e879c1cd67674ca979ff0a18Stephen Hines 1045a47020542c52af3e879c1cd67674ca979ff0a18Stephen Hinesextern float __attribute__((overloadable)) rsRand(float min, float max);/* { 1055a47020542c52af3e879c1cd67674ca979ff0a18Stephen Hines float r = (float)rand(); 1065a47020542c52af3e879c1cd67674ca979ff0a18Stephen Hines r /= RAND_MAX; 1075a47020542c52af3e879c1cd67674ca979ff0a18Stephen Hines r = r * (max - min) + min; 1085a47020542c52af3e879c1cd67674ca979ff0a18Stephen Hines return r; 1095a47020542c52af3e879c1cd67674ca979ff0a18Stephen Hines} 1105a47020542c52af3e879c1cd67674ca979ff0a18Stephen Hines*/ 1115a47020542c52af3e879c1cd67674ca979ff0a18Stephen Hines 1125a47020542c52af3e879c1cd67674ca979ff0a18Stephen Hinesextern float __attribute__((overloadable)) rsRand(float max) { 1135a47020542c52af3e879c1cd67674ca979ff0a18Stephen Hines return rsRand(0.f, max); 1145a47020542c52af3e879c1cd67674ca979ff0a18Stephen Hines //float r = (float)rand(); 1155a47020542c52af3e879c1cd67674ca979ff0a18Stephen Hines //r *= max; 1165a47020542c52af3e879c1cd67674ca979ff0a18Stephen Hines //r /= RAND_MAX; 1175a47020542c52af3e879c1cd67674ca979ff0a18Stephen Hines //return r; 1185a47020542c52af3e879c1cd67674ca979ff0a18Stephen Hines} 1195a47020542c52af3e879c1cd67674ca979ff0a18Stephen Hines 1205a47020542c52af3e879c1cd67674ca979ff0a18Stephen Hinesextern int __attribute__((overloadable)) rsRand(int max) { 1215a47020542c52af3e879c1cd67674ca979ff0a18Stephen Hines return (int)rsRand((float)max); 1225a47020542c52af3e879c1cd67674ca979ff0a18Stephen Hines} 1235a47020542c52af3e879c1cd67674ca979ff0a18Stephen Hines 1245a47020542c52af3e879c1cd67674ca979ff0a18Stephen Hinesextern int __attribute__((overloadable)) rsRand(int min, int max) { 1255a47020542c52af3e879c1cd67674ca979ff0a18Stephen Hines return (int)rsRand((float)min, (float)max); 1265a47020542c52af3e879c1cd67674ca979ff0a18Stephen Hines} 1275a47020542c52af3e879c1cd67674ca979ff0a18Stephen Hines 1285a47020542c52af3e879c1cd67674ca979ff0a18Stephen Hines#define PRIM_DEBUG(T) \ 1295a47020542c52af3e879c1cd67674ca979ff0a18Stephen Hinesextern void __attribute__((overloadable)) rsDebug(const char *, const T *); \ 1305a47020542c52af3e879c1cd67674ca979ff0a18Stephen Hinesvoid __attribute__((overloadable)) rsDebug(const char *txt, T val) { \ 1315a47020542c52af3e879c1cd67674ca979ff0a18Stephen Hines rsDebug(txt, &val); \ 1325a47020542c52af3e879c1cd67674ca979ff0a18Stephen Hines} 1335a47020542c52af3e879c1cd67674ca979ff0a18Stephen Hines 1345a47020542c52af3e879c1cd67674ca979ff0a18Stephen HinesPRIM_DEBUG(char2) 1355a47020542c52af3e879c1cd67674ca979ff0a18Stephen HinesPRIM_DEBUG(char3) 1365a47020542c52af3e879c1cd67674ca979ff0a18Stephen HinesPRIM_DEBUG(char4) 1375a47020542c52af3e879c1cd67674ca979ff0a18Stephen HinesPRIM_DEBUG(uchar2) 1385a47020542c52af3e879c1cd67674ca979ff0a18Stephen HinesPRIM_DEBUG(uchar3) 1395a47020542c52af3e879c1cd67674ca979ff0a18Stephen HinesPRIM_DEBUG(uchar4) 1405a47020542c52af3e879c1cd67674ca979ff0a18Stephen HinesPRIM_DEBUG(short2) 1415a47020542c52af3e879c1cd67674ca979ff0a18Stephen HinesPRIM_DEBUG(short3) 1425a47020542c52af3e879c1cd67674ca979ff0a18Stephen HinesPRIM_DEBUG(short4) 1435a47020542c52af3e879c1cd67674ca979ff0a18Stephen HinesPRIM_DEBUG(ushort2) 1445a47020542c52af3e879c1cd67674ca979ff0a18Stephen HinesPRIM_DEBUG(ushort3) 1455a47020542c52af3e879c1cd67674ca979ff0a18Stephen HinesPRIM_DEBUG(ushort4) 1465a47020542c52af3e879c1cd67674ca979ff0a18Stephen HinesPRIM_DEBUG(int2) 1475a47020542c52af3e879c1cd67674ca979ff0a18Stephen HinesPRIM_DEBUG(int3) 1485a47020542c52af3e879c1cd67674ca979ff0a18Stephen HinesPRIM_DEBUG(int4) 1495a47020542c52af3e879c1cd67674ca979ff0a18Stephen HinesPRIM_DEBUG(uint2) 1505a47020542c52af3e879c1cd67674ca979ff0a18Stephen HinesPRIM_DEBUG(uint3) 1515a47020542c52af3e879c1cd67674ca979ff0a18Stephen HinesPRIM_DEBUG(uint4) 1525a47020542c52af3e879c1cd67674ca979ff0a18Stephen HinesPRIM_DEBUG(long2) 1535a47020542c52af3e879c1cd67674ca979ff0a18Stephen HinesPRIM_DEBUG(long3) 1545a47020542c52af3e879c1cd67674ca979ff0a18Stephen HinesPRIM_DEBUG(long4) 1555a47020542c52af3e879c1cd67674ca979ff0a18Stephen HinesPRIM_DEBUG(ulong2) 1565a47020542c52af3e879c1cd67674ca979ff0a18Stephen HinesPRIM_DEBUG(ulong3) 1575a47020542c52af3e879c1cd67674ca979ff0a18Stephen HinesPRIM_DEBUG(ulong4) 1585a47020542c52af3e879c1cd67674ca979ff0a18Stephen HinesPRIM_DEBUG(float2) 1595a47020542c52af3e879c1cd67674ca979ff0a18Stephen HinesPRIM_DEBUG(float3) 1605a47020542c52af3e879c1cd67674ca979ff0a18Stephen HinesPRIM_DEBUG(float4) 1615a47020542c52af3e879c1cd67674ca979ff0a18Stephen HinesPRIM_DEBUG(double2) 1625a47020542c52af3e879c1cd67674ca979ff0a18Stephen HinesPRIM_DEBUG(double3) 1635a47020542c52af3e879c1cd67674ca979ff0a18Stephen HinesPRIM_DEBUG(double4) 1645a47020542c52af3e879c1cd67674ca979ff0a18Stephen Hines 1655a47020542c52af3e879c1cd67674ca979ff0a18Stephen Hines#undef PRIM_DEBUG 1665a47020542c52af3e879c1cd67674ca979ff0a18Stephen Hines 167