9 # error This is a userspace-only header, not allowed by the current build.
12 #include <sifteo/abi.h>
13 #include <sifteo/macros.h>
29 #define M_E 2.71828182845904523536028747135266250
30 #define M_LOG2E 1.44269504088896340735992468100189214
31 #define M_LOG10E 0.434294481903251827651128918916605082
32 #define M_LN2 0.693147180559945309417232121458176568
33 #define M_LN10 2.30258509299404568401799145468436421
34 #define M_PI 3.14159265358979323846264338327950288
35 #define M_TAU 6.2831853071795862
36 #define M_PI_2 1.57079632679489661923132169163975144
37 #define M_PI_4 0.785398163397448309615660845819875721
38 #define M_1_PI 0.318309886183790671537767526745028724
39 #define M_2_PI 0.636619772367581343075535053490057448
40 #define M_2_SQRTPI 1.12837916709551257389615890312154517
41 #define M_SQRT2 1.41421356237309504880168872420969808
42 #define M_SQRT1_2 0.707106781186547524400844362104849039
43 #define MAXFLOAT ((float)3.40282346638528860e+38)
44 #define NAN __builtin_nanf("0x7fc00000")
53 template <
typename T>
inline T
clamp(
const T& value,
const T& low,
const T& high)
71 template <
typename T>
inline T
abs(
const T& value)
87 template <
typename T>
inline T
lslc(
const T& value,
int bits)
91 if (bits >=
sizeof(T) * 8)
93 return value << (unsigned)bits;
103 template <
typename T>
inline T
lsrc(
const T& value,
int bits)
107 if (bits >=
sizeof(T) * 8)
109 return value >> (unsigned)bits;
120 template <
typename T>
inline T
bitRange(
int begin,
int end)
122 return lslc((T)-1, begin) & ~
lslc((T)-1, end);
135 inline unsigned clz(uint32_t word)
137 return __builtin_clz(word);
150 inline unsigned ffs(uint32_t word)
153 return 32 - __builtin_clz(word & -word);
157 float inline fmod(
float a,
float b)
159 uint32_t r = _SYS_fmodf(reinterpret_cast<uint32_t&>(a), reinterpret_cast<uint32_t&>(b));
160 return reinterpret_cast<float&
>(r);
164 double inline fmod(
double a,
double b)
166 uint64_t ia =
reinterpret_cast<uint64_t&
>(a);
167 uint64_t ib =
reinterpret_cast<uint64_t&
>(b);
168 uint64_t r = _SYS_fmod(ia, ia >> 32, ib, ib >> 32);
169 return reinterpret_cast<double&
>(r);
173 float inline pow(
float a,
float b)
175 uint32_t r = _SYS_powf(reinterpret_cast<uint32_t&>(a), reinterpret_cast<uint32_t&>(b));
176 return reinterpret_cast<float&
>(r);
180 double inline pow(
double a,
double b)
182 uint64_t ia =
reinterpret_cast<uint64_t&
>(a);
183 uint64_t ib =
reinterpret_cast<uint64_t&
>(b);
184 uint64_t r = _SYS_pow(ia, ia >> 32, ib, ib >> 32);
185 return reinterpret_cast<double&
>(r);
189 unsigned inline umod(
int a,
int b)
198 template <
typename T>
inline T
ceildiv(T numerator, T denominator) {
199 return (numerator + (denominator - 1)) / denominator;
206 template <
typename T>
inline T
roundup(T numerator, T denominator) {
207 return ceildiv(numerator, denominator) * denominator;
213 uint32_t r = _SYS_logf(reinterpret_cast<uint32_t&>(a));
214 return reinterpret_cast<float&
>(r);
218 double inline log(
double a)
220 uint64_t ia =
reinterpret_cast<uint64_t&
>(a);
221 uint64_t r = _SYS_logd(ia, ia >> 32);
222 return reinterpret_cast<double&
>(r);
228 uint32_t r = _SYS_sqrtf(reinterpret_cast<uint32_t&>(a));
229 return reinterpret_cast<float&
>(r);
235 uint64_t ia =
reinterpret_cast<uint64_t&
>(a);
236 uint64_t r = _SYS_sqrt(ia, ia >> 32);
237 return reinterpret_cast<double&
>(r);
248 uint32_t r = _SYS_sinf(reinterpret_cast<uint32_t&>(x));
249 return reinterpret_cast<float&
>(r);
260 uint32_t r = _SYS_cosf(reinterpret_cast<uint32_t&>(x));
261 return reinterpret_cast<float&
>(r);
267 uint32_t r = _SYS_tanf(reinterpret_cast<uint32_t&>(x));
268 return reinterpret_cast<float&
>(r);
274 uint32_t r = _SYS_atanf(reinterpret_cast<uint32_t&>(x));
275 return reinterpret_cast<float&
>(r);
284 float inline atan2(
float a,
float b)
286 uint32_t r = _SYS_atan2f(reinterpret_cast<uint32_t&>(a), reinterpret_cast<uint32_t&>(b));
287 return reinterpret_cast<float&
>(r);
297 void inline sincos(
float x,
float *s,
float *c)
299 _SYS_sincosf(reinterpret_cast<uint32_t&>(x), s, c);
311 uint32_t r = _SYS_tsinf(reinterpret_cast<uint32_t&>(x));
312 return reinterpret_cast<float&
>(r);
324 uint32_t r = _SYS_tcosf(reinterpret_cast<uint32_t&>(x));
325 return reinterpret_cast<float&
>(r);
344 return _SYS_tsini(x);
364 return _SYS_tcosi(x);
378 return __builtin_isunordered(a, b);
392 return __builtin_isunordered(a, b);
407 template <
typename T>
inline long ceil(
const T value)
410 if (result >= 0 && result < value) {
428 template <
typename T>
inline long floor(
const T value)
431 if (result <= 0 && result > value) {
450 template <
typename T>
inline long round(
const T value)
452 return floor(value + 0.5);
459 template <
typename T>
inline bool almostEqual(
const T a,
const T b,
const T epsilon)
461 return abs(a-b) < epsilon;
476 void set(T _x, T _y) {
515 case 0:
return *
this;
550 return (
x *
x +
y *
y );
573 return *
this /
len();
613 operator Vector2<unsigned> ()
const {
return cast<unsigned>(); }
614 operator Vector2<short> ()
const {
return cast<short>(); }
615 operator Vector2<unsigned short> ()
const {
return cast<unsigned short>(); }
616 operator Vector2<int8_t> ()
const {
return cast<int8_t>(); }
617 operator Vector2<uint8_t> ()
const {
return cast<uint8_t>(); }
618 operator Vector2<float> ()
const {
return cast<float>(); }
619 operator Vector2<double> ()
const {
return cast<double>(); }
646 return u.
x * v.
x + u.
y * v.
y;
657 template <
typename T>
inline Vector2<T> operator-(Vector2<T> u) {
return vec<T>(-u.x, -u.y); }
658 template <
typename T>
inline Vector2<T> operator+=(Vector2<T> &u, Vector2<T> v) {
return vec<T>(u.x+=v.x, u.y+=v.y); }
659 template <
typename T>
inline Vector2<T> operator-=(Vector2<T> &u, Vector2<T> v) {
return vec<T>(u.x-=v.x, u.y-=v.y); }
660 template <
typename T>
inline Vector2<T> operator+(Vector2<T> u, Vector2<T> v) {
return vec<T>(u.x+v.x, u.y+v.y); }
661 template <
typename T>
inline Vector2<T> operator-(Vector2<T> u, Vector2<T> v) {
return vec<T>(u.x-v.x, u.y-v.y); }
662 template <
typename T>
inline Vector2<T> operator*(Vector2<T> u, Vector2<T> v) {
return vec<T>(u.x*v.x, u.y*v.y); }
663 template <
typename T>
inline bool operator==(Vector2<T> u, Vector2<T> v) {
return u.x == v.x && u.y == v.y; }
664 template <
typename T>
inline bool operator!=(Vector2<T> u, Vector2<T> v) {
return u.x != v.x || u.y != v.y; }
667 template <
typename T>
inline Int2 operator*(
int k, Vector2<T> v) {
return vec<int>(k*v.x, k*v.y); }
668 template <
typename T>
inline Int2 operator*(Vector2<T> v,
int k) {
return vec<int>(k*v.x, k*v.y); }
669 template <
typename T>
inline Int2 operator/(Vector2<T> v,
int k) {
return vec<int>(v.x/k, v.y/k); }
670 template <
typename T>
inline Int2 operator+=(Vector2<T> &u,
int k) {
return vec<int>(u.x+=k, u.y+=k); }
671 template <
typename T>
inline Int2 operator*=(Vector2<T> &u,
int k) {
return vec<int>(u.x*=k, u.y*=k); }
672 template <
typename T>
inline Int2 operator<<(Vector2<T> u,
int shift) {
return vec<int>(u.x<<shift, u.y<<shift); }
673 template <
typename T>
inline Int2 operator>>(Vector2<T> u,
int shift) {
return vec<int>(u.x>>shift, u.y>>shift); }
676 template <
typename T>
inline Float2 operator*(
float k, Vector2<T> v) {
return vec<float>(k*v.x, k*v.y); }
677 template <
typename T>
inline Float2 operator*(Vector2<T> v,
float k) {
return vec<float>(k*v.x, k*v.y); }
678 template <
typename T>
inline Float2 operator/(Vector2<T> v,
float k) {
return vec<float>(v.x/k, v.y/k); }
679 template <
typename T>
inline Float2 operator+=(Vector2<T> &u,
float k) {
return vec<float>(u.x+=k, u.y+=k); }
680 template <
typename T>
inline Float2 operator*=(Vector2<T> &u,
float k) {
return vec<float>(u.x*=k, u.y*=k); }
683 template <
typename T>
inline Double2 operator*(
double k, Vector2<T> v) {
return vec<double>(k*v.x, k*v.y); }
684 template <
typename T>
inline Double2 operator*(Vector2<T> v,
double k) {
return vec<double>(k*v.x, k*v.y); }
685 template <
typename T>
inline Double2 operator/(Vector2<T> v,
double k) {
return vec<double>(v.x/k, v.y/k); }
686 template <
typename T>
inline Double2 operator+=(Vector2<T> &u,
double k) {
return vec<double>(u.x+=k, u.y+=k); }
687 template <
typename T>
inline Double2 operator*=(Vector2<T> &u,
double k) {
return vec<double>(u.x*=k, u.y*=k); }
690 inline Vector2<float> operator+=(Vector2<float> &u,
Int2 v) {
return vec(u.x+=v.x, u.y+=v.y); }
691 inline Vector2<float> operator-=(Vector2<float> &u,
Int2 v) {
return vec(u.x-=v.x, u.y-=v.y); }
692 inline Vector2<float> operator+(Vector2<float> u,
Int2 v) {
return vec(u.x+v.x, u.y+v.y); }
693 inline Vector2<float> operator-(Vector2<float> u,
Int2 v) {
return vec(u.x-v.x, u.y-v.y); }
694 inline Vector2<float> operator*(Vector2<float> u,
Int2 v) {
return vec(u.x*v.x, u.y*v.y); }
695 inline Vector2<float> operator+(
Int2 u, Vector2<float> v) {
return vec(u.x+v.x, u.y+v.y); }
696 inline Vector2<float> operator-(
Int2 u, Vector2<float> v) {
return vec(u.x-v.x, u.y-v.y); }
697 inline Vector2<float> operator*(
Int2 u, Vector2<float> v) {
return vec(u.x*v.x, u.y*v.y); }
710 void set(T _x, T _y, T _z) {
729 return (
x *
x +
y *
y +
z *
z );
752 return *
this /
len();
787 case 0:
return *
this;
812 operator Vector3<unsigned> ()
const {
return cast<unsigned>(); }
813 operator Vector3<short> ()
const {
return cast<short>(); }
814 operator Vector3<unsigned short> ()
const {
return cast<unsigned short>(); }
815 operator Vector3<int8_t> ()
const {
return cast<int8_t>(); }
816 operator Vector3<uint8_t> ()
const {
return cast<uint8_t>(); }
817 operator Vector3<float> ()
const {
return cast<float>(); }
818 operator Vector3<double> ()
const {
return cast<double>(); }
845 return u.
x * v.
x + u.
y * v.
y + u.
z * v.
z;
849 template <
typename T>
inline T
cross(T u, T v) {
850 return vec(u.y * v.z - u.z * v.y,
851 u.z * v.x - u.x * v.z,
852 u.x * v.y - u.y * v.x);
856 template <
typename T>
inline Vector3<T> operator-(Vector3<T> u) {
return vec<T>(-u.x, -u.y, -u.z); }
857 template <
typename T>
inline Vector3<T> operator+=(Vector3<T> &u, Vector3<T> v) {
return vec<T>(u.x+=v.x, u.y+=v.y, u.z+=v.z); }
858 template <
typename T>
inline Vector3<T> operator-=(Vector3<T> &u, Vector3<T> v) {
return vec<T>(u.x-=v.x, u.y-=v.y, u.z-=v.z); }
859 template <
typename T>
inline Vector3<T> operator+(Vector3<T> u, Vector3<T> v) {
return vec<T>(u.x+v.x, u.y+v.y, u.z+v.z); }
860 template <
typename T>
inline Vector3<T> operator-(Vector3<T> u, Vector3<T> v) {
return vec<T>(u.x-v.x, u.y-v.y, u.z-v.z); }
861 template <
typename T>
inline Vector3<T> operator*(Vector3<T> u, Vector3<T> v) {
return vec<T>(u.x*v.x, u.y*v.y, u.z*v.z); }
862 template <
typename T>
inline bool operator==(Vector3<T> u, Vector3<T> v) {
return u.x == v.x && u.y == v.y && u.z == v.z; }
863 template <
typename T>
inline bool operator!=(Vector3<T> u, Vector3<T> v) {
return u.x != v.x || u.y != v.y || u.z != v.z; }
866 template <
typename T>
inline Int3 operator*(
int k, Vector3<T> v) {
return vec<int>(k*v.x, k*v.y, k*v.z); }
867 template <
typename T>
inline Int3 operator*(Vector3<T> v,
int k) {
return vec<int>(k*v.x, k*v.y, k*v.z); }
868 template <
typename T>
inline Int3 operator/(Vector3<T> v,
int k) {
return vec<int>(v.x/k, v.y/k, v.z/k); }
869 template <
typename T>
inline Int3 operator+=(Vector3<T> &u,
int k) {
return vec<int>(u.x+=k, u.y+=k, u.z+=k); }
870 template <
typename T>
inline Int3 operator*=(Vector3<T> &u,
int k) {
return vec<int>(u.x*=k, u.y*=k, u.z*=k); }
871 template <
typename T>
inline Int3 operator<<(Vector3<T> u,
int shift) {
return vec<int>(u.x<<shift, u.y<<shift, u.z<<shift); }
872 template <
typename T>
inline Int3 operator>>(Vector3<T> u,
int shift) {
return vec<int>(u.x>>shift, u.y>>shift, u.z>>shift); }
875 template <
typename T>
inline Float3 operator*(
float k, Vector3<T> v) {
return vec<float>(k*v.x, k*v.y, k*v.z); }
876 template <
typename T>
inline Float3 operator*(Vector3<T> v,
float k) {
return vec<float>(k*v.x, k*v.y, k*v.z); }
877 template <
typename T>
inline Float3 operator/(Vector3<T> v,
float k) {
return vec<float>(v.x/k, v.y/k, v.z/k); }
878 template <
typename T>
inline Float3 operator+=(Vector3<T> &u,
float k) {
return vec<float>(u.x+=k, u.y+=k, u.z+=k); }
879 template <
typename T>
inline Float3 operator*=(Vector3<T> &u,
float k) {
return vec<float>(u.x*=k, u.y*=k, u.z*=k); }
882 template <
typename T>
inline Double3 operator*(
double k, Vector3<T> v) {
return vec<double>(k*v.x, k*v.y, k*v.z); }
883 template <
typename T>
inline Double3 operator*(Vector3<T> v,
double k) {
return vec<double>(k*v.x, k*v.y, k*v.z); }
884 template <
typename T>
inline Double3 operator/(Vector3<T> v,
double k) {
return vec<double>(v.x/k, v.y/k, v.z/k); }
885 template <
typename T>
inline Double3 operator+=(Vector3<T> &u,
double k) {
return vec<double>(u.x+=k, u.y+=k, u.z+=k); }
886 template <
typename T>
inline Double3 operator*=(Vector3<T> &u,
double k) {
return vec<double>(u.x*=k, u.y*=k, u.z*=k); }
909 _SYSPseudoRandomState state;
927 _SYS_prng_init(&state, s);
936 seed((uint32_t) _SYS_ticks_ns());
941 return _SYS_prng_value(&state);
946 return raw() * (1.0f / 0xFFFFFFFF);
951 return -averageInterval *
log(1.0f -
random());
964 const uint32_t mask = 0x7FFFFFFF;
965 uint32_t threshold = probability * (mask + 1);
966 return (
raw() & mask) < threshold;
975 return a +
raw() * ((b-a) / 0xFFFFFFFF);
982 template <
typename T>
984 return T(a + _SYS_prng_valueBounded(&state, b - a));
991 template <
typename T>
993 return randint<T>(a, T(b - 1));
1002 template <
typename T>
1004 return randrange<T>(T(0), count);
1036 float _xy,
float _yy,
float _cy)
1037 :
cx(_cx),
cy(_cy),
xx(_xx),
1038 xy(_xy),
yx(_yx),
yy(_yy) {}
1048 float inv_s = 1.0f / s;