1#ifndef SLANG_PRELUDE_CPP_TYPES_CORE_H
2#define SLANG_PRELUDE_CPP_TYPES_CORE_H
4#ifndef SLANG_PRELUDE_ASSERT
5#ifdef SLANG_PRELUDE_ENABLE_ASSERT
6#define SLANG_PRELUDE_ASSERT(VALUE) assert(VALUE)
8#define SLANG_PRELUDE_ASSERT(VALUE)
18#define SLANG_BOUND_ASSERT(index, count) SLANG_PRELUDE_ASSERT(index < count);
19#define SLANG_BOUND_ASSERT_BYTE_ADDRESS(index, elemSize, sizeInBytes) \
20 SLANG_PRELUDE_ASSERT(index <= (sizeInBytes - elemSize) && (index & 3) == 0);
23#define SLANG_BOUND_ZERO_INDEX(index, count) index = (index < count) ? index : 0;
24#define SLANG_BOUND_ZERO_INDEX_BYTE_ADDRESS(index, elemSize, sizeInBytes) \
25 index = (index <= (sizeInBytes - elemSize)) ? index : 0;
29#ifdef SLANG_ENABLE_BOUND_ZERO_INDEX
30#define SLANG_BOUND_FIX(index, count) SLANG_BOUND_ZERO_INDEX(index, count)
31#define SLANG_BOUND_FIX_BYTE_ADDRESS(index, elemSize, sizeInBytes) \
32 SLANG_BOUND_ZERO_INDEX_BYTE_ADDRESS(index, elemSize, sizeInBytes)
33#define SLANG_BOUND_FIX_FIXED_ARRAY(index, count) SLANG_BOUND_ZERO_INDEX(index, count)
35#define SLANG_BOUND_FIX(index, count)
36#define SLANG_BOUND_FIX_BYTE_ADDRESS(index, elemSize, sizeInBytes)
37#define SLANG_BOUND_FIX_FIXED_ARRAY(index, count)
40#ifndef SLANG_BOUND_CHECK
41#define SLANG_BOUND_CHECK(index, count) \
42 SLANG_BOUND_ASSERT(index, count) SLANG_BOUND_FIX(index, count)
45#ifndef SLANG_BOUND_CHECK_BYTE_ADDRESS
46#define SLANG_BOUND_CHECK_BYTE_ADDRESS(index, elemSize, sizeInBytes) \
47 SLANG_BOUND_ASSERT_BYTE_ADDRESS(index, elemSize, sizeInBytes) \
48 SLANG_BOUND_FIX_BYTE_ADDRESS(index, elemSize, sizeInBytes)
51#ifndef SLANG_BOUND_CHECK_FIXED_ARRAY
52#define SLANG_BOUND_CHECK_FIXED_ARRAY(index, count) \
53 SLANG_BOUND_ASSERT(index, count) SLANG_BOUND_FIX_FIXED_ARRAY(index, count)
61template<
typename T,
size_t SIZE>
102template<
typename T,
int COUNT>
111 operator T()
const {
return x; }
119 template<
typename U,
int otherSize>
123 if (otherSize < minSize)
125 for (
int i = 0; i < minSize; i++)
126 (*
this)[i] = (T)other[i];
134 const T&
operator[](
size_t index)
const {
return index == 0 ? x : y; }
149 template<
typename U,
int otherSize>
153 if (otherSize < minSize)
155 for (
int i = 0; i < minSize; i++)
156 (*
this)[i] = (T)other[i];
164 const T&
operator[](
size_t index)
const {
return *((T*)(
this) + index); }
165 T&
operator[](
size_t index) {
return *((T*)(
this) + index); }
182 template<
typename U,
int otherSize>
186 if (otherSize < minSize)
188 for (
int i = 0; i < minSize; i++)
189 (*
this)[i] = (T)other[i];
198 const T&
operator[](
size_t index)
const {
return *((T*)(
this) + index); }
199 T&
operator[](
size_t index) {
return *((T*)(
this) + index); }
201 Vector(T scalar) { x = y = z = w = scalar; }
209 template<
typename U,
int otherSize>
213 if (otherSize < minSize)
215 for (
int i = 0; i < minSize; i++)
216 (*
this)[i] = (T)other[i];
220template<
typename T,
int N>
227 for (
int i = 0; i < N; i++)
229 result[i] = condition[i] ? v0[i] : v1[i];
237 return condition ? v0 : v1;
240template<
typename T,
int N>
246template<
typename T,
int N>
252template<
typename T,
int N>
255 return &((*x)[index]);
258template<
typename T,
int n,
typename OtherT,
int m>
262 for (
int i = 0; i < n; i++)
264 OtherT otherElement = T(0);
274#define SLANG_VECTOR_BINARY_OP(T, op) \
276 SLANG_FORCE_INLINE Vector<T, n> operator op( \
277 const Vector<T, n>& thisVal, \
278 const Vector<T, n>& other) \
280 Vector<T, n> result; \
281 for (int i = 0; i < n; i++) \
282 result[i] = thisVal[i] op other[i]; \
285#define SLANG_VECTOR_BINARY_COMPARE_OP(T, op) \
287 SLANG_FORCE_INLINE Vector<bool, n> operator op( \
288 const Vector<T, n>& thisVal, \
289 const Vector<T, n>& other) \
291 Vector<bool, n> result; \
292 for (int i = 0; i < n; i++) \
293 result[i] = thisVal[i] op other[i]; \
297#define SLANG_VECTOR_UNARY_OP(T, op) \
299 SLANG_FORCE_INLINE Vector<T, n> operator op(const Vector<T, n>& thisVal) \
301 Vector<T, n> result; \
302 for (int i = 0; i < n; i++) \
303 result[i] = op thisVal[i]; \
306#define SLANG_INT_VECTOR_OPS(T) \
307 SLANG_VECTOR_BINARY_OP(T, +) \
308 SLANG_VECTOR_BINARY_OP(T, -) \
309 SLANG_VECTOR_BINARY_OP(T, *) \
310 SLANG_VECTOR_BINARY_OP(T, /) \
311 SLANG_VECTOR_BINARY_OP(T, &) \
312 SLANG_VECTOR_BINARY_OP(T, |) \
313 SLANG_VECTOR_BINARY_OP(T, &&) \
314 SLANG_VECTOR_BINARY_OP(T, ||) \
315 SLANG_VECTOR_BINARY_OP(T, ^) \
316 SLANG_VECTOR_BINARY_OP(T, %) \
317 SLANG_VECTOR_BINARY_OP(T, >>) \
318 SLANG_VECTOR_BINARY_OP(T, <<) \
319 SLANG_VECTOR_BINARY_COMPARE_OP(T, >) \
320 SLANG_VECTOR_BINARY_COMPARE_OP(T, <) \
321 SLANG_VECTOR_BINARY_COMPARE_OP(T, >=) \
322 SLANG_VECTOR_BINARY_COMPARE_OP(T, <=) \
323 SLANG_VECTOR_BINARY_COMPARE_OP(T, ==) \
324 SLANG_VECTOR_BINARY_COMPARE_OP(T, !=) \
325 SLANG_VECTOR_UNARY_OP(T, !) \
326 SLANG_VECTOR_UNARY_OP(T, ~)
327#define SLANG_FLOAT_VECTOR_OPS(T) \
328 SLANG_VECTOR_BINARY_OP(T, +) \
329 SLANG_VECTOR_BINARY_OP(T, -) \
330 SLANG_VECTOR_BINARY_OP(T, *) \
331 SLANG_VECTOR_BINARY_OP(T, /) \
332 SLANG_VECTOR_UNARY_OP(T, -) \
333 SLANG_VECTOR_BINARY_COMPARE_OP(T, >) \
334 SLANG_VECTOR_BINARY_COMPARE_OP(T, <) \
335 SLANG_VECTOR_BINARY_COMPARE_OP(T, >=) \
336 SLANG_VECTOR_BINARY_COMPARE_OP(T, <=) \
337 SLANG_VECTOR_BINARY_COMPARE_OP(T, ==) \
338 SLANG_VECTOR_BINARY_COMPARE_OP(T, !=)
349#if SLANG_INTPTR_TYPE_IS_DISTINCT
357#define SLANG_VECTOR_INT_NEG_OP(T) \
359 Vector<T, N> operator-(const Vector<T, N>& thisVal) \
361 Vector<T, N> result; \
362 for (int i = 0; i < N; i++) \
363 result[i] = 0 - thisVal[i]; \
374#if SLANG_INTPTR_TYPE_IS_DISTINCT
379#define SLANG_FLOAT_VECTOR_MOD(T) \
381 Vector<T, N> operator%(const Vector<T, N>& left, const Vector<T, N>& right) \
383 Vector<T, N> result; \
384 for (int i = 0; i < N; i++) \
385 result[i] = _slang_fmod(left[i], right[i]); \
391#undef SLANG_FLOAT_VECTOR_MOD
392#undef SLANG_VECTOR_BINARY_OP
393#undef SLANG_VECTOR_UNARY_OP
394#undef SLANG_INT_VECTOR_OPS
395#undef SLANG_FLOAT_VECTOR_OPS
396#undef SLANG_VECTOR_INT_NEG_OP
397#undef SLANG_FLOAT_VECTOR_MOD
399template<
typename T,
int ROWS,
int COLS>
408 for (
int i = 0; i < ROWS; i++)
434 template<
typename U,
int otherRow,
int otherCol>
439 if (minRow > otherRow)
441 if (minCol > otherCol)
443 for (
int i = 0; i < minRow; i++)
444 for (
int j = 0; j < minCol; j++)
454 Matrix(T v0, T v1, T v2, T v3, T v4, T v5)
475 Matrix(T v0, T v1, T v2, T v3, T v4, T v5, T v6, T v7)
500 Matrix(T v0, T v1, T v2, T v3, T v4, T v5, T v6, T v7, T v8)
512 Matrix(T v0, T v1, T v2, T v3, T v4, T v5, T v6, T v7, T v8, T v9, T v10, T v11)
582#define SLANG_MATRIX_BINARY_OP(T, op) \
583 template<int R, int C> \
584 Matrix<T, R, C> operator op(const Matrix<T, R, C>& thisVal, const Matrix<T, R, C>& other) \
586 Matrix<T, R, C> result; \
587 for (int i = 0; i < R; i++) \
588 for (int j = 0; j < C; j++) \
589 result.rows[i][j] = thisVal.rows[i][j] op other.rows[i][j]; \
593#define SLANG_MATRIX_BINARY_COMPARE_OP(T, op) \
594 template<int R, int C> \
595 Matrix<bool, R, C> operator op(const Matrix<T, R, C>& thisVal, const Matrix<T, R, C>& other) \
597 Matrix<bool, R, C> result; \
598 for (int i = 0; i < R; i++) \
599 for (int j = 0; j < C; j++) \
600 result.rows[i][j] = thisVal.rows[i][j] op other.rows[i][j]; \
604#define SLANG_MATRIX_UNARY_OP(T, op) \
605 template<int R, int C> \
606 Matrix<T, R, C> operator op(const Matrix<T, R, C>& thisVal) \
608 Matrix<T, R, C> result; \
609 for (int i = 0; i < R; i++) \
610 for (int j = 0; j < C; j++) \
611 result[i].rows[i][j] = op thisVal.rows[i][j]; \
615#define SLANG_INT_MATRIX_OPS(T) \
616 SLANG_MATRIX_BINARY_OP(T, +) \
617 SLANG_MATRIX_BINARY_OP(T, -) \
618 SLANG_MATRIX_BINARY_OP(T, *) \
619 SLANG_MATRIX_BINARY_OP(T, /) \
620 SLANG_MATRIX_BINARY_OP(T, &) \
621 SLANG_MATRIX_BINARY_OP(T, |) \
622 SLANG_MATRIX_BINARY_OP(T, &&) \
623 SLANG_MATRIX_BINARY_OP(T, ||) \
624 SLANG_MATRIX_BINARY_OP(T, ^) \
625 SLANG_MATRIX_BINARY_OP(T, %) \
626 SLANG_MATRIX_BINARY_COMPARE_OP(T, >) \
627 SLANG_MATRIX_BINARY_COMPARE_OP(T, <) \
628 SLANG_MATRIX_BINARY_COMPARE_OP(T, >=) \
629 SLANG_MATRIX_BINARY_COMPARE_OP(T, <=) \
630 SLANG_MATRIX_BINARY_COMPARE_OP(T, ==) \
631 SLANG_MATRIX_BINARY_COMPARE_OP(T, !=) \
632 SLANG_MATRIX_UNARY_OP(T, !) \
633 SLANG_MATRIX_UNARY_OP(T, ~)
634#define SLANG_FLOAT_MATRIX_OPS(T) \
635 SLANG_MATRIX_BINARY_OP(T, +) \
636 SLANG_MATRIX_BINARY_OP(T, -) \
637 SLANG_MATRIX_BINARY_OP(T, *) \
638 SLANG_MATRIX_BINARY_OP(T, /) \
639 SLANG_MATRIX_UNARY_OP(T, -) \
640 SLANG_MATRIX_BINARY_COMPARE_OP(T, >) \
641 SLANG_MATRIX_BINARY_COMPARE_OP(T, <) \
642 SLANG_MATRIX_BINARY_COMPARE_OP(T, >=) \
643 SLANG_MATRIX_BINARY_COMPARE_OP(T, <=) \
644 SLANG_MATRIX_BINARY_COMPARE_OP(T, ==) \
645 SLANG_MATRIX_BINARY_COMPARE_OP(T, !=)
654#if SLANG_INTPTR_TYPE_IS_DISTINCT
662#define SLANG_MATRIX_INT_NEG_OP(T) \
663 template<int R, int C> \
664 SLANG_FORCE_INLINE Matrix<T, R, C> operator-(Matrix<T, R, C> thisVal) \
666 Matrix<T, R, C> result; \
667 for (int i = 0; i < R; i++) \
668 for (int j = 0; j < C; j++) \
669 result.rows[i][j] = 0 - thisVal.rows[i][j]; \
680#if SLANG_INTPTR_TYPE_IS_DISTINCT
685#define SLANG_FLOAT_MATRIX_MOD(T) \
686 template<int R, int C> \
687 SLANG_FORCE_INLINE Matrix<T, R, C> operator%(Matrix<T, R, C> left, Matrix<T, R, C> right) \
689 Matrix<T, R, C> result; \
690 for (int i = 0; i < R; i++) \
691 for (int j = 0; j < C; j++) \
692 result.rows[i][j] = _slang_fmod(left.rows[i][j], right.rows[i][j]); \
698#undef SLANG_FLOAT_MATRIX_MOD
699#undef SLANG_MATRIX_BINARY_OP
700#undef SLANG_MATRIX_UNARY_OP
701#undef SLANG_INT_MATRIX_OPS
702#undef SLANG_FLOAT_MATRIX_OPS
703#undef SLANG_MATRIX_INT_NEG_OP
704#undef SLANG_FLOAT_MATRIX_MOD
706template<
typename TResult,
typename TInput>
709 return *(TResult*)(&val);
#define SLANG_FORCE_INLINE
Definition slang-cpp-prelude.h:286
TResult slang_bit_cast(TInput val)
Definition slang-cpp-types-core.h:707
uint32_t uint
Definition slang-cpp-types-core.h:272
#define SLANG_MATRIX_INT_NEG_OP(T)
Definition slang-cpp-types-core.h:662
#define SLANG_INT_MATRIX_OPS(T)
Definition slang-cpp-types-core.h:615
#define SLANG_FLOAT_MATRIX_OPS(T)
Definition slang-cpp-types-core.h:634
SLANG_FORCE_INLINE Vector< T, n > _slang_vector_reshape(const Vector< OtherT, m > other)
Definition slang-cpp-types-core.h:259
#define SLANG_INT_VECTOR_OPS(T)
Definition slang-cpp-types-core.h:306
SLANG_FORCE_INLINE Vector< T, N > _slang_select(Vector< bool, N > condition, Vector< T, N > v0, Vector< T, N > v1)
Definition slang-cpp-types-core.h:221
SLANG_FORCE_INLINE T _slang_vector_get_element(Vector< T, N > x, int index)
Definition slang-cpp-types-core.h:241
SLANG_FORCE_INLINE const T * _slang_vector_get_element_ptr(const Vector< T, N > *x, int index)
Definition slang-cpp-types-core.h:247
#define SLANG_VECTOR_INT_NEG_OP(T)
Definition slang-cpp-types-core.h:357
#define SLANG_FLOAT_VECTOR_OPS(T)
Definition slang-cpp-types-core.h:327
#define SLANG_FLOAT_MATRIX_MOD(T)
Definition slang-cpp-types-core.h:685
#define SLANG_FLOAT_VECTOR_MOD(T)
Definition slang-cpp-types-core.h:379
#define SLANG_BOUND_CHECK_FIXED_ARRAY(index, count)
Definition slang-cuda-prelude.h:117
#define SLANG_BOUND_CHECK(index, count)
Definition slang-cuda-prelude.h:106
__INTPTR_TYPE__ intptr_t
Definition slang-llvm.h:146
__UINTPTR_TYPE__ uintptr_t
Definition slang-llvm.h:153
Definition slang-cpp-types-core.h:82
const T & operator[](size_t index) const
Definition slang-cpp-types-core.h:83
T & operator[](size_t index)
Definition slang-cpp-types-core.h:88
T * data
Definition slang-cpp-types-core.h:94
size_t count
Definition slang-cpp-types-core.h:95
Definition slang-cpp-types-core.h:63
const T & operator[](size_t index) const
Definition slang-cpp-types-core.h:64
T m_data[SIZE]
Definition slang-cpp-types-core.h:75
T & operator[](size_t index)
Definition slang-cpp-types-core.h:69
Definition slang-cpp-types-core.h:401
Matrix(const Vector< T, COLS > &row0)
Definition slang-cpp-types-core.h:411
Matrix(T v0, T v1, T v2, T v3, T v4, T v5, T v6, T v7, T v8, T v9, T v10, T v11)
Definition slang-cpp-types-core.h:512
Matrix(const Vector< T, COLS > &row0, const Vector< T, COLS > &row1, const Vector< T, COLS > &row2, const Vector< T, COLS > &row3)
Definition slang-cpp-types-core.h:423
Matrix(T v0, T v1, T v2, T v3, T v4, T v5, T v6, T v7)
Definition slang-cpp-types-core.h:475
const Vector< T, COLS > & operator[](size_t index) const
Definition slang-cpp-types-core.h:403
Matrix(const Matrix< U, otherRow, otherCol > &other)
Definition slang-cpp-types-core.h:435
Matrix(T v0, T v1, T v2, T v3, T v4, T v5, T v6, T v7, T v8)
Definition slang-cpp-types-core.h:500
Matrix(const Vector< T, COLS > &row0, const Vector< T, COLS > &row1)
Definition slang-cpp-types-core.h:412
Matrix(T scalar)
Definition slang-cpp-types-core.h:406
Matrix(T v0, T v1, T v2, T v3)
Definition slang-cpp-types-core.h:447
Matrix(T v0, T v1, T v2, T v3, T v4, T v5)
Definition slang-cpp-types-core.h:454
Vector< T, COLS > & operator[](size_t index)
Definition slang-cpp-types-core.h:404
Matrix(T v0, T v1, T v2, T v3, T v4, T v5, T v6, T v7, T v8, T v9, T v10, T v11, T v12, T v13, T v14, T v15)
Definition slang-cpp-types-core.h:545
Matrix(const Vector< T, COLS > &row0, const Vector< T, COLS > &row1, const Vector< T, COLS > &row2)
Definition slang-cpp-types-core.h:417
Vector< T, COLS > rows[ROWS]
Definition slang-cpp-types-core.h:402
Definition slang-cpp-types-core.h:57
size_t typeSize
Definition slang-cpp-types-core.h:58
Vector(T scalar)
Definition slang-cpp-types-core.h:113
T x
Definition slang-cpp-types-core.h:108
T & operator[](size_t)
Definition slang-cpp-types-core.h:110
Vector(Vector< U, otherSize > other)
Definition slang-cpp-types-core.h:120
Vector(Vector< U, 1 > other)
Definition slang-cpp-types-core.h:115
const T & operator[](size_t) const
Definition slang-cpp-types-core.h:109
const T & operator[](size_t index) const
Definition slang-cpp-types-core.h:134
Vector(Vector< U, otherSize > other)
Definition slang-cpp-types-core.h:150
T x
Definition slang-cpp-types-core.h:133
T & operator[](size_t index)
Definition slang-cpp-types-core.h:135
Vector(T _x, T _y)
Definition slang-cpp-types-core.h:138
Vector(T scalar)
Definition slang-cpp-types-core.h:137
Vector(Vector< U, 2 > other)
Definition slang-cpp-types-core.h:144
const T & operator[](size_t index) const
Definition slang-cpp-types-core.h:164
Vector(T scalar)
Definition slang-cpp-types-core.h:168
Vector(Vector< U, otherSize > other)
Definition slang-cpp-types-core.h:183
T x
Definition slang-cpp-types-core.h:163
Vector(Vector< U, 3 > other)
Definition slang-cpp-types-core.h:176
T & operator[](size_t index)
Definition slang-cpp-types-core.h:165
Vector(T _x, T _y, T _z)
Definition slang-cpp-types-core.h:169
T w
Definition slang-cpp-types-core.h:196
Vector(T scalar)
Definition slang-cpp-types-core.h:201
Vector(Vector< U, otherSize > other)
Definition slang-cpp-types-core.h:210
Vector(T _x, T _y, T _z, T _w)
Definition slang-cpp-types-core.h:202
const T & operator[](size_t index) const
Definition slang-cpp-types-core.h:198
T & operator[](size_t index)
Definition slang-cpp-types-core.h:199
Definition slang-cpp-types-core.h:103