Add vec in cliuent deps
This commit is contained in:
10 files changed
+2374
No files matched your search
@@ -0,0 +1,21 @@
|
||||
MIT License
|
||||
|
||||
Copyright (c) 2017 Alfred Klomp
|
||||
|
||||
Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
of this software and associated documentation files (the "Software"), to deal
|
||||
in the Software without restriction, including without limitation the rights
|
||||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
copies of the Software, and to permit persons to whom the Software is
|
||||
furnished to do so, subject to the following conditions:
|
||||
|
||||
The above copyright notice and this permission notice shall be included in all
|
||||
copies or substantial portions of the Software.
|
||||
|
||||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||
SOFTWARE.
|
||||
@@ -0,0 +1,374 @@
|
||||
# vec
|
||||
|
||||
Source: https://github.com/aklomp/vec
|
||||
|
||||
[](https://github.com/aklomp/vec/actions/workflows/test.yml)
|
||||
|
||||
A small, header-only, cross-platform SIMD vector library for C11. Provides an
|
||||
abstraction of 128-bit vectors divided into four 32-bit elements, which can be
|
||||
floating or integer. Does this using a clever `union` type. Oriented mainly
|
||||
towards 3D graphics, but can be used for any type of 32×4 bit vector.
|
||||
|
||||
## Usage
|
||||
|
||||
This library is header-only, meaning that there is no code to compile, just a
|
||||
single header file to include. This header file is `include/vec/vec.h`. Include
|
||||
that file into your source code and you can use the library.
|
||||
|
||||
(That file will conditionally pull in a number of platform-specific files from
|
||||
a subdirectory, but that all happens invisibly in the background.)
|
||||
|
||||
In general, this library will be included in other projects as a Git
|
||||
subrepository. The project's build environment should be configured to add the
|
||||
following search path to `CFLAGS`:
|
||||
|
||||
```make
|
||||
CFLAGS += -I path/to/vec/include
|
||||
```
|
||||
|
||||
Now you can include the library as:
|
||||
|
||||
```c
|
||||
#include <vec/vec.h>
|
||||
```
|
||||
|
||||
The library's backend has support for the following vector formats, in order of
|
||||
preference from highest to lowest:
|
||||
|
||||
- On x86: SSE4 SIMD operations.
|
||||
- On x86: SSE2 SIMD operations.
|
||||
- On ARM: NEON SIMD operations (NEON32) using inline assembly.
|
||||
- On ARM: NEON SIMD operations (NEON32) using intrinsics.
|
||||
- On GCC or Clang, if new enough: GCC-style vector extensions. These are
|
||||
"generic" cross-platform vector types that are not tied to any one hardware
|
||||
implementation. The compiler turns these into appropriate SIMD instructions
|
||||
for the platform.
|
||||
- The old-fashioned way: all operations are done separately on each of the four
|
||||
elements of the union. This uses only plain C and should work on all
|
||||
platforms.
|
||||
|
||||
Support for these vector formats is detected at compile time by testing
|
||||
predefined compiler macros. The backends with higher preference are "allowed"
|
||||
to define a given function first. Those with lower preference fill in the
|
||||
functions that haven't yet been defined. The generic backend acts as a backstop
|
||||
and defines all functions that have not been defined.
|
||||
|
||||
## Data structure
|
||||
|
||||
The basic data structure is the `union vec`. As the name implies, this is a C
|
||||
union that wraps a 128-bit data structure which is internally divided into four
|
||||
32-bit elements. Depending on context, these elements can be interpreted as
|
||||
32-bit floats, 32-bit signed ints, 32-bit unsigned ints, or as a single 128-bit
|
||||
SIMD vector with four independent lanes.
|
||||
|
||||
The charm of the `union vec` is that it allows us to access the same underlying
|
||||
bits *as if* it's any of these data types, in a relatively typesafe way. In the
|
||||
same line of code, the `union vec` can be treated as an array of floating point
|
||||
elements, or as a single SIMD register.
|
||||
|
||||
The abstraction is invisible and seamless to the programmer, because the
|
||||
compiler will transparently handle the different views of memory. Aliasing
|
||||
memory through a union is quite cheap because no type conversion is performed.
|
||||
We merely look at the same bits in memory through different glasses. Compilers
|
||||
and processors have good support for that kind of access.
|
||||
|
||||
`union vec` provides the following ways of interpreting 16 bytes of memory:
|
||||
|
||||
```c
|
||||
// Declare a variable for demo purposes:
|
||||
union vec vec;
|
||||
|
||||
// As a series of four named 32-bit floating-point variables,
|
||||
// named to match 3D graphics vectors (the common use case):
|
||||
float vec.x, vec.y, vec.z, vec.w;
|
||||
|
||||
// As a series of four named 32-bit signed integer variables:
|
||||
int32_t vec.xi, vec.yi, vec.zi, vec.wi;
|
||||
|
||||
// As a series of four named 32-bit unsigned integer variables:
|
||||
uint32_t vec.xu, vec.yu, vec.zu, vec.wu;
|
||||
|
||||
// As arrays of four 32-bit floating-point, signed integer
|
||||
// and unsigned integer variables:
|
||||
float vec.elem.f[4];
|
||||
int32_t vec.elem.i[4];
|
||||
uint32_t vec.elem.u[4];
|
||||
|
||||
// If GCC-style portable vector extensions are available, we
|
||||
// define vector extension typedefs called 'vec4f', 'vec4i'
|
||||
// and 'vec4u', and define the following extra aliases:
|
||||
vec4f vec.gcc.f;
|
||||
vec4i vec.gcc.i;
|
||||
vec4u vec.gcc.u;
|
||||
|
||||
// If x86_64 SSE2 is available, the following extra aliases
|
||||
// are available (i and u are functionally identical):
|
||||
__m128 vec.sse.f;
|
||||
__m128i vec.sse.i;
|
||||
__m128i vec.sse.u;
|
||||
|
||||
// If ARM NEON is available, the following extra aliases are
|
||||
// available:
|
||||
float32x4_t vec.neon.f;
|
||||
int32x4_t vec.neon.i;
|
||||
uint32x4_t vec.neon.u;
|
||||
```
|
||||
|
||||
## Instantiation
|
||||
|
||||
Macros are available for compile-time static initialization of vectors. Use
|
||||
these for hard-coded, static values.
|
||||
|
||||
```c
|
||||
// Create a floating-point vector:
|
||||
union vec a = VEC(1.0f, 2.0f, 3.0f, 4.0f);
|
||||
|
||||
// Alternatively, for floating-point, it is possible to use union
|
||||
// initialization syntax directly:
|
||||
union vec b = { { 1.0f, 2.0f, 3.0f, 4.0f } };
|
||||
|
||||
// Create a signed integer vector:
|
||||
union vec c = VEC_I(-2, -1, 0, 1);
|
||||
|
||||
// Create an unsigned integer vector:
|
||||
union vec d = VEC_U(1, 2, 3, 4);
|
||||
```
|
||||
|
||||
To instantiate an `union vec` at runtime from constant literals or other
|
||||
variables, use the provided inline functions.
|
||||
|
||||
```c
|
||||
// Create a floating-point vector:
|
||||
static inline union vec
|
||||
vec (const float x, const float y, const float z, const float w);
|
||||
|
||||
// Create a signed integer vector:
|
||||
static inline union vec
|
||||
vec_i (const int32_t x, const int32_t y, const int32_t z, const int32_t w);
|
||||
|
||||
// Create an unsigned integer vector:
|
||||
static inline union vec
|
||||
vec_u (const uint32_t x, const uint32_t y, const uint32_t z, const uint32_t w);
|
||||
```
|
||||
|
||||
```c
|
||||
// Create a vector set to all zeros, valid for all numeric types:
|
||||
static inline union vec
|
||||
vec_zero (void);
|
||||
```
|
||||
|
||||
```c
|
||||
// Create a floating-point vector with four identical elements:
|
||||
static inline union vec
|
||||
vec_1 (const float val);
|
||||
|
||||
// Create a signed integer vector with four identical elements:
|
||||
static inline union vec
|
||||
vec_i1 (const int32_t val);
|
||||
|
||||
// Create an unsigned integer vector with four identical elements:
|
||||
static inline union vec
|
||||
vec_u1 (const uint32_t val);
|
||||
```
|
||||
|
||||
## Conversion
|
||||
|
||||
Functions are available to convert floats to signed integers and vice versa.
|
||||
|
||||
```c
|
||||
// Convert a float vector to signed integer:
|
||||
static inline union vec
|
||||
vec_to_int (const union vec v);
|
||||
|
||||
// Convert a signed integer vector to float:
|
||||
static inline union vec
|
||||
vec_to_float (const union vec v);
|
||||
```
|
||||
|
||||
## Arithmetic
|
||||
|
||||
Addition, subtraction, multiplication and division are available.
|
||||
Multiplication and division are only supported for floats because SIMD
|
||||
instruction sets have spotty support for these operations on integers. If you
|
||||
need integer multiplications or divisions, convert to float, do the float
|
||||
operation, and convert back.
|
||||
|
||||
```c
|
||||
// Floating-point addition (a + b):
|
||||
static inline union vec
|
||||
vec_add (const union vec a, const union vec b);
|
||||
|
||||
// Signed integer addition (a + b):
|
||||
static inline union vec
|
||||
vec_iadd (const union vec a, const union vec b);
|
||||
|
||||
// Unsigned integer addition (a + b):
|
||||
static inline union vec
|
||||
vec_uadd (const union vec a, const union vec b);
|
||||
```
|
||||
|
||||
```c
|
||||
// Floating-point subtraction (a - b):
|
||||
static inline union vec
|
||||
vec_sub (const union vec a, const union vec b);
|
||||
|
||||
// Signed integer subtraction (a - b):
|
||||
static inline union vec
|
||||
vec_isub (const union vec a, const union vec b);
|
||||
|
||||
// Unsigned integer subtraction (a - b):
|
||||
static inline union vec
|
||||
vec_usub (const union vec a, const union vec b);
|
||||
```
|
||||
|
||||
```c
|
||||
// Floating-point multiplication (a * b):
|
||||
static inline union vec
|
||||
vec_mul (const union vec a, const union vec b);
|
||||
```
|
||||
|
||||
```c
|
||||
// Floating-point division (a / b):
|
||||
static inline union vec
|
||||
vec_div (const union vec a, const union vec b);
|
||||
```
|
||||
|
||||
## Bitwise logic
|
||||
|
||||
The bitwise logical functions `AND`, `NOT`, `OR` and `XOR` are available. These
|
||||
operate on the unsigned integer representation.
|
||||
|
||||
```c
|
||||
// Bitwise a AND b:
|
||||
static inline union vec
|
||||
vec_and (const union vec a, const union vec b);
|
||||
|
||||
// Bitwise a OR b:
|
||||
static inline union vec
|
||||
vec_or (const union vec a, const union vec b);
|
||||
|
||||
// Bitwise a XOR b:
|
||||
static inline union vec
|
||||
vec_xor (const union vec a, const union vec b);
|
||||
|
||||
// Bitwise NOT a:
|
||||
static inline union vec
|
||||
vec_not (const union vec a);
|
||||
```
|
||||
|
||||
## Comparisons
|
||||
|
||||
Comparison functions are available for all operand types. These functions
|
||||
return a `union vec` where each 32-bit lane is all-ones (`0xFFFFFFFF`) if the
|
||||
comparison is true for those lanes in the source vectors, or all-zeros if the
|
||||
comparison is false. This "masking" vector can be used in combination with the
|
||||
bitwise logical functions to construct new vectors.
|
||||
|
||||
```c
|
||||
// Floating-point test of a < b:
|
||||
static inline union vec
|
||||
vec_lt (const union vec a, const union vec b);
|
||||
|
||||
// Signed integer test of a < b:
|
||||
static inline union vec
|
||||
vec_ilt (const union vec a, const union vec b);
|
||||
|
||||
// Unsigned integer test of a < b:
|
||||
static inline union vec
|
||||
vec_ult (const union vec a, const union vec b);
|
||||
```
|
||||
|
||||
```c
|
||||
// Floating-point test of a <= b:
|
||||
static inline union vec
|
||||
vec_le (const union vec a, const union vec b);
|
||||
|
||||
// Signed integer test of a <= b:
|
||||
static inline union vec
|
||||
vec_ile (const union vec a, const union vec b);
|
||||
|
||||
// Unsigned integer test of a <= b:
|
||||
static inline union vec
|
||||
vec_ule (const union vec a, const union vec b);
|
||||
```
|
||||
|
||||
```c
|
||||
// Floating-point test of a == b:
|
||||
static inline union vec
|
||||
vec_eq (const union vec a, const union vec b);
|
||||
|
||||
// Signed integer test of a == b:
|
||||
static inline union vec
|
||||
vec_ieq (const union vec a, const union vec b);
|
||||
|
||||
// Unsigned integer test of a == b:
|
||||
static inline union vec
|
||||
vec_ueq (const union vec a, const union vec b);
|
||||
```
|
||||
|
||||
```c
|
||||
// Floating-point test of a >= b:
|
||||
static inline union vec
|
||||
vec_ge (const union vec a, const union vec b);
|
||||
|
||||
// Signed integer test of a >= b:
|
||||
static inline union vec
|
||||
vec_ige (const union vec a, const union vec b);
|
||||
|
||||
// Unsigned integer test of a >= b:
|
||||
static inline union vec
|
||||
vec_uge (const union vec a, const union vec b);
|
||||
```
|
||||
|
||||
```c
|
||||
// Floating-point test of a > b:
|
||||
static inline union vec
|
||||
vec_gt (const union vec a, const union vec b);
|
||||
|
||||
// Signed integer test of a > b:
|
||||
static inline union vec
|
||||
vec_igt (const union vec a, const union vec b);
|
||||
|
||||
// Unsigned integer test of a > b:
|
||||
static inline union vec
|
||||
vec_ugt (const union vec a, const union vec b);
|
||||
```
|
||||
|
||||
## 3D vector operations
|
||||
|
||||
Dot and cross product functions are available for 3D vectors in (x, y, z, w)
|
||||
format. These functions are only available for floating point operands.
|
||||
|
||||
```c
|
||||
// Dot product of a and b:
|
||||
// ret = ax * bx + ay * by + az * bz
|
||||
static inline float
|
||||
vec_dot (const union vec a, const union vec b);
|
||||
```
|
||||
|
||||
```c
|
||||
// Cross product of a and b:
|
||||
// x = ay * bz - az * by
|
||||
// y = az * bx - ax * bz
|
||||
// z = ax * by - ay * bx
|
||||
// w = 0
|
||||
static inline union vec
|
||||
vec_cross (const union vec a, const union vec b);
|
||||
```
|
||||
|
||||
## Testing
|
||||
|
||||
An extensive test suite can be found in `test/`. Typing `make test` in that
|
||||
directory will compile and run three binaries: one with only the generic
|
||||
per-element routines, one with GCC vector extension routines enabled but not
|
||||
SIMD, and one with SIMD routines enabled. If all tests pass, a binary will run
|
||||
cleanly without any output and return zero. On error, it will print diagnostics
|
||||
and return nonzero. This library has been tested on all supported platforms.
|
||||
|
||||
Automated tests are run on every commit by [Travis CI](https://travis-ci.org/aklomp/vec).
|
||||
|
||||
[](https://travis-ci.org/aklomp/vec)
|
||||
|
||||
## License
|
||||
|
||||
MIT license. See `LICENSE` file for details.
|
||||
@@ -0,0 +1,311 @@
|
||||
// Return float version of a + b:
|
||||
#ifndef VEC_FN_ADD
|
||||
#define VEC_FN_ADD
|
||||
static inline union vec
|
||||
vec_add (const union vec a, const union vec b)
|
||||
{
|
||||
return (union vec) { .gcc.f = a.gcc.f + b.gcc.f };
|
||||
}
|
||||
#endif
|
||||
|
||||
// Return signed integer version of a + b:
|
||||
#ifndef VEC_FN_IADD
|
||||
#define VEC_FN_IADD
|
||||
static inline union vec
|
||||
vec_iadd (const union vec a, const union vec b)
|
||||
{
|
||||
return (union vec) { .gcc.i = a.gcc.i + b.gcc.i };
|
||||
}
|
||||
#endif
|
||||
|
||||
// Return unsigned integer version of a + b:
|
||||
#ifndef VEC_FN_UADD
|
||||
#define VEC_FN_UADD
|
||||
static inline union vec
|
||||
vec_uadd (const union vec a, const union vec b)
|
||||
{
|
||||
return (union vec) { .gcc.u = a.gcc.u + b.gcc.u };
|
||||
}
|
||||
#endif
|
||||
|
||||
// Return float version of a - b:
|
||||
#ifndef VEC_FN_SUB
|
||||
#define VEC_FN_SUB
|
||||
static inline union vec
|
||||
vec_sub (const union vec a, const union vec b)
|
||||
{
|
||||
return (union vec) { .gcc.f = a.gcc.f - b.gcc.f };
|
||||
}
|
||||
#endif
|
||||
|
||||
// Return signed integer version of a - b:
|
||||
#ifndef VEC_FN_ISUB
|
||||
#define VEC_FN_ISUB
|
||||
static inline union vec
|
||||
vec_isub (const union vec a, const union vec b)
|
||||
{
|
||||
return (union vec) { .gcc.i = a.gcc.i - b.gcc.i };
|
||||
}
|
||||
#endif
|
||||
|
||||
// Return unsigned integer version of a - b:
|
||||
#ifndef VEC_FN_USUB
|
||||
#define VEC_FN_USUB
|
||||
static inline union vec
|
||||
vec_usub (const union vec a, const union vec b)
|
||||
{
|
||||
return (union vec) { .gcc.u = a.gcc.u - b.gcc.u };
|
||||
}
|
||||
#endif
|
||||
|
||||
// Return float version of a * b:
|
||||
#ifndef VEC_FN_MUL
|
||||
#define VEC_FN_MUL
|
||||
static inline union vec
|
||||
vec_mul (const union vec a, const union vec b)
|
||||
{
|
||||
return (union vec) { .gcc.f = a.gcc.f * b.gcc.f };
|
||||
}
|
||||
#endif
|
||||
|
||||
// Return float version of a / b:
|
||||
#ifndef VEC_FN_DIV
|
||||
#define VEC_FN_DIV
|
||||
static inline union vec
|
||||
vec_div (const union vec a, const union vec b)
|
||||
{
|
||||
return (union vec) { .gcc.f = a.gcc.f / b.gcc.f };
|
||||
}
|
||||
#endif
|
||||
|
||||
// Return bitwise a AND b:
|
||||
#ifndef VEC_FN_AND
|
||||
#define VEC_FN_AND
|
||||
static inline union vec
|
||||
vec_and (const union vec a, const union vec b)
|
||||
{
|
||||
return (union vec) { .gcc.u = a.gcc.u & b.gcc.u };
|
||||
}
|
||||
#endif
|
||||
|
||||
// Return bitwise a OR b:
|
||||
#ifndef VEC_FN_OR
|
||||
#define VEC_FN_OR
|
||||
static inline union vec
|
||||
vec_or (const union vec a, const union vec b)
|
||||
{
|
||||
return (union vec) { .gcc.u = a.gcc.u | b.gcc.u };
|
||||
}
|
||||
#endif
|
||||
|
||||
// Return bitwise a XOR b:
|
||||
#ifndef VEC_FN_XOR
|
||||
#define VEC_FN_XOR
|
||||
static inline union vec
|
||||
vec_xor (const union vec a, const union vec b)
|
||||
{
|
||||
return (union vec) { .gcc.u = a.gcc.u ^ b.gcc.u };
|
||||
}
|
||||
#endif
|
||||
|
||||
// Return bitwise NOT v:
|
||||
#ifndef VEC_FN_NOT
|
||||
#define VEC_FN_NOT
|
||||
static inline union vec
|
||||
vec_not (const union vec v)
|
||||
{
|
||||
return (union vec) { .gcc.u = ~v.gcc.u };
|
||||
}
|
||||
#endif
|
||||
|
||||
// GCC added support for vector comparisons in version 4.8:
|
||||
#ifdef __GNUC__
|
||||
#if __GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 8)
|
||||
#define VEC_GCC_COMPARE
|
||||
#endif
|
||||
#else
|
||||
|
||||
// Assume we're using clang - it's supported vector comparisons since at
|
||||
// least version 3.0, possibly earlier:
|
||||
#define VEC_GCC_COMPARE
|
||||
#endif
|
||||
|
||||
// Return float version of a == b:
|
||||
#ifdef VEC_GCC_COMPARE
|
||||
#ifndef VEC_FN_EQ
|
||||
#define VEC_FN_EQ
|
||||
static inline union vec
|
||||
vec_eq (const union vec a, const union vec b)
|
||||
{
|
||||
return (union vec) { .gcc.i = a.gcc.f == b.gcc.f };
|
||||
}
|
||||
#endif
|
||||
#endif
|
||||
|
||||
// Return signed integer version of a == b:
|
||||
#ifdef VEC_GCC_COMPARE
|
||||
#ifndef VEC_FN_IEQ
|
||||
#define VEC_FN_IEQ
|
||||
static inline union vec
|
||||
vec_ieq (const union vec a, const union vec b)
|
||||
{
|
||||
return (union vec) { .gcc.i = a.gcc.i == b.gcc.i };
|
||||
}
|
||||
#endif
|
||||
#endif
|
||||
|
||||
// Return float version of a == b:
|
||||
#ifdef VEC_GCC_COMPARE
|
||||
#ifndef VEC_FN_UEQ
|
||||
#define VEC_FN_UEQ
|
||||
static inline union vec
|
||||
vec_ueq (const union vec a, const union vec b)
|
||||
{
|
||||
return (union vec) { .gcc.i = a.gcc.u == b.gcc.u };
|
||||
}
|
||||
#endif
|
||||
#endif
|
||||
|
||||
// Return float version of a < b:
|
||||
#ifdef VEC_GCC_COMPARE
|
||||
#ifndef VEC_FN_LT
|
||||
#define VEC_FN_LT
|
||||
static inline union vec
|
||||
vec_lt (const union vec a, const union vec b)
|
||||
{
|
||||
return (union vec) { .gcc.i = a.gcc.f < b.gcc.f };
|
||||
}
|
||||
#endif
|
||||
#endif
|
||||
|
||||
// Return signed integer version of a < b:
|
||||
#ifdef VEC_GCC_COMPARE
|
||||
#ifndef VEC_FN_ILT
|
||||
#define VEC_FN_ILT
|
||||
static inline union vec
|
||||
vec_ilt (const union vec a, const union vec b)
|
||||
{
|
||||
return (union vec) { .gcc.i = a.gcc.i < b.gcc.i };
|
||||
}
|
||||
#endif
|
||||
#endif
|
||||
|
||||
// Return unsigned integer version of a < b:
|
||||
#ifdef VEC_GCC_COMPARE
|
||||
#ifndef VEC_FN_ULT
|
||||
#define VEC_FN_ULT
|
||||
static inline union vec
|
||||
vec_ult (const union vec a, const union vec b)
|
||||
{
|
||||
return (union vec) { .gcc.i = a.gcc.u < b.gcc.u };
|
||||
}
|
||||
#endif
|
||||
#endif
|
||||
|
||||
// Return float version of a <= b:
|
||||
#ifdef VEC_GCC_COMPARE
|
||||
#ifndef VEC_FN_LE
|
||||
#define VEC_FN_LE
|
||||
static inline union vec
|
||||
vec_le (const union vec a, const union vec b)
|
||||
{
|
||||
return (union vec) { .gcc.i = a.gcc.f <= b.gcc.f };
|
||||
}
|
||||
#endif
|
||||
#endif
|
||||
|
||||
// Return signed integer version of a <= b:
|
||||
#ifdef VEC_GCC_COMPARE
|
||||
#ifndef VEC_FN_ILE
|
||||
#define VEC_FN_ILE
|
||||
static inline union vec
|
||||
vec_ile (const union vec a, const union vec b)
|
||||
{
|
||||
return (union vec) { .gcc.i = a.gcc.i <= b.gcc.i };
|
||||
}
|
||||
#endif
|
||||
#endif
|
||||
|
||||
// Return unsigned integer version of a <= b:
|
||||
#ifdef VEC_GCC_COMPARE
|
||||
#ifndef VEC_FN_ULE
|
||||
#define VEC_FN_ULE
|
||||
static inline union vec
|
||||
vec_ule (const union vec a, const union vec b)
|
||||
{
|
||||
return (union vec) { .gcc.i = a.gcc.u <= b.gcc.u };
|
||||
}
|
||||
#endif
|
||||
#endif
|
||||
|
||||
// Return float version of a > b:
|
||||
#ifdef VEC_GCC_COMPARE
|
||||
#ifndef VEC_FN_GT
|
||||
#define VEC_FN_GT
|
||||
static inline union vec
|
||||
vec_gt (const union vec a, const union vec b)
|
||||
{
|
||||
return (union vec) { .gcc.i = a.gcc.f > b.gcc.f };
|
||||
}
|
||||
#endif
|
||||
#endif
|
||||
|
||||
// Return signed integer version of a > b:
|
||||
#ifdef VEC_GCC_COMPARE
|
||||
#ifndef VEC_FN_IGT
|
||||
#define VEC_FN_IGT
|
||||
static inline union vec
|
||||
vec_igt (const union vec a, const union vec b)
|
||||
{
|
||||
return (union vec) { .gcc.i = a.gcc.i > b.gcc.i };
|
||||
}
|
||||
#endif
|
||||
#endif
|
||||
|
||||
// Return unsigned integer version of a > b:
|
||||
#ifdef VEC_GCC_COMPARE
|
||||
#ifndef VEC_FN_UGT
|
||||
#define VEC_FN_UGT
|
||||
static inline union vec
|
||||
vec_ugt (const union vec a, const union vec b)
|
||||
{
|
||||
return (union vec) { .gcc.i = a.gcc.u > b.gcc.u };
|
||||
}
|
||||
#endif
|
||||
#endif
|
||||
|
||||
// Return float version of a >= b:
|
||||
#ifdef VEC_GCC_COMPARE
|
||||
#ifndef VEC_FN_GE
|
||||
#define VEC_FN_GE
|
||||
static inline union vec
|
||||
vec_ge (const union vec a, const union vec b)
|
||||
{
|
||||
return (union vec) { .gcc.i = a.gcc.f >= b.gcc.f };
|
||||
}
|
||||
#endif
|
||||
#endif
|
||||
|
||||
// Return signed integer version of a >= b:
|
||||
#ifdef VEC_GCC_COMPARE
|
||||
#ifndef VEC_FN_IGE
|
||||
#define VEC_FN_IGE
|
||||
static inline union vec
|
||||
vec_ige (const union vec a, const union vec b)
|
||||
{
|
||||
return (union vec) { .gcc.i = a.gcc.i >= b.gcc.i };
|
||||
}
|
||||
#endif
|
||||
#endif
|
||||
|
||||
// Return unsigned integer version of a >= b:
|
||||
#ifdef VEC_GCC_COMPARE
|
||||
#ifndef VEC_FN_UGE
|
||||
#define VEC_FN_UGE
|
||||
static inline union vec
|
||||
vec_uge (const union vec a, const union vec b)
|
||||
{
|
||||
return (union vec) { .gcc.i = a.gcc.u >= b.gcc.u };
|
||||
}
|
||||
#endif
|
||||
#endif
|
||||
@@ -0,0 +1,539 @@
|
||||
// Instantiate a float vector:
|
||||
#ifndef VEC_FN_
|
||||
#define VEC_FN_
|
||||
static inline union vec
|
||||
vec (const float x, const float y, const float z, const float w)
|
||||
{
|
||||
return (union vec) { .x = x, .y = y, .z = z, .w = w };
|
||||
}
|
||||
#endif
|
||||
|
||||
// Instantiate a signed integer vector:
|
||||
#ifndef VEC_FN_I
|
||||
#define VEC_FN_I
|
||||
static inline union vec
|
||||
vec_i (const int32_t x, const int32_t y, const int32_t z, const int32_t w)
|
||||
{
|
||||
return (union vec) { .xi = x, .yi = y, .zi = z, .wi = w };
|
||||
}
|
||||
#endif
|
||||
|
||||
// Instantiate an unsigned integer vector:
|
||||
#ifndef VEC_FN_U
|
||||
#define VEC_FN_U
|
||||
static inline union vec
|
||||
vec_u (const uint32_t x, const uint32_t y, const uint32_t z, const uint32_t w)
|
||||
{
|
||||
return (union vec) { .xu = x, .yu = y, .zu = z, .wu = w };
|
||||
}
|
||||
#endif
|
||||
|
||||
// Instantiate a float vector with four identical elements:
|
||||
#ifndef VEC_FN_1
|
||||
#define VEC_FN_1
|
||||
static inline union vec
|
||||
vec_1 (const float val)
|
||||
{
|
||||
return vec(val, val, val, val);
|
||||
}
|
||||
#endif
|
||||
|
||||
// Instantiate a signed integer vector with four identical elements:
|
||||
#ifndef VEC_FN_I1
|
||||
#define VEC_FN_I1
|
||||
static inline union vec
|
||||
vec_i1 (const int32_t val)
|
||||
{
|
||||
return vec_i(val, val, val, val);
|
||||
}
|
||||
#endif
|
||||
|
||||
// Instantiate an unsigned integer vector with four identical elements:
|
||||
#ifndef VEC_FN_U1
|
||||
#define VEC_FN_U1
|
||||
static inline union vec
|
||||
vec_u1 (const uint32_t val)
|
||||
{
|
||||
return vec_u(val, val, val, val);
|
||||
}
|
||||
#endif
|
||||
|
||||
// Instantiate a float vector set to all zeros:
|
||||
#ifndef VEC_FN_ZERO
|
||||
#define VEC_FN_ZERO
|
||||
static inline union vec
|
||||
vec_zero (void)
|
||||
{
|
||||
return vec_1(0.0f);
|
||||
}
|
||||
#endif
|
||||
|
||||
// Instantiate a signed integer vector set to all zeros:
|
||||
#ifndef VEC_FN_IZERO
|
||||
#define VEC_FN_IZERO
|
||||
static inline union vec
|
||||
vec_izero (void)
|
||||
{
|
||||
return vec_i1(0);
|
||||
}
|
||||
#endif
|
||||
|
||||
// Instantiate an unsigned integer vector set to all zeros:
|
||||
#ifndef VEC_FN_UZERO
|
||||
#define VEC_FN_UZERO
|
||||
static inline union vec
|
||||
vec_uzero (void)
|
||||
{
|
||||
return vec_u1(0U);
|
||||
}
|
||||
#endif
|
||||
|
||||
// Convert a float vector to signed integer:
|
||||
#ifndef VEC_FN_TO_INT
|
||||
#define VEC_FN_TO_INT
|
||||
static inline union vec
|
||||
vec_to_int (const union vec v)
|
||||
{
|
||||
return (union vec) { .xi = v.x, .yi = v.y, .zi = v.z, .wi = v.w };
|
||||
}
|
||||
#endif
|
||||
|
||||
// Convert a signed integer vector to float:
|
||||
#ifndef VEC_FN_TO_FLOAT
|
||||
#define VEC_FN_TO_FLOAT
|
||||
static inline union vec
|
||||
vec_to_float (const union vec v)
|
||||
{
|
||||
return (union vec) { .x = v.xi, .y = v.yi, .z = v.zi, .w = v.wi };
|
||||
}
|
||||
#endif
|
||||
|
||||
// Return float version of a + b:
|
||||
#ifndef VEC_FN_ADD
|
||||
#define VEC_FN_ADD
|
||||
static inline union vec
|
||||
vec_add (const union vec a, const union vec b)
|
||||
{
|
||||
return (union vec) {
|
||||
.x = a.x + b.x,
|
||||
.y = a.y + b.y,
|
||||
.z = a.z + b.z,
|
||||
.w = a.w + b.w,
|
||||
};
|
||||
}
|
||||
#endif
|
||||
|
||||
// Return signed integer version of a + b:
|
||||
#ifndef VEC_FN_IADD
|
||||
#define VEC_FN_IADD
|
||||
static inline union vec
|
||||
vec_iadd (const union vec a, const union vec b)
|
||||
{
|
||||
return (union vec) {
|
||||
.xi = a.xi + b.xi,
|
||||
.yi = a.yi + b.yi,
|
||||
.zi = a.zi + b.zi,
|
||||
.wi = a.wi + b.wi,
|
||||
};
|
||||
}
|
||||
#endif
|
||||
|
||||
// Return unsigned integer version of a + b:
|
||||
#ifndef VEC_FN_UADD
|
||||
#define VEC_FN_UADD
|
||||
static inline union vec
|
||||
vec_uadd (const union vec a, const union vec b)
|
||||
{
|
||||
return (union vec) {
|
||||
.xu = a.xu + b.xu,
|
||||
.yu = a.yu + b.yu,
|
||||
.zu = a.zu + b.zu,
|
||||
.wu = a.wu + b.wu,
|
||||
};
|
||||
}
|
||||
#endif
|
||||
|
||||
// Return float version of a - b:
|
||||
#ifndef VEC_FN_SUB
|
||||
#define VEC_FN_SUB
|
||||
static inline union vec
|
||||
vec_sub (const union vec a, const union vec b)
|
||||
{
|
||||
return (union vec) {
|
||||
.x = a.x - b.x,
|
||||
.y = a.y - b.y,
|
||||
.z = a.z - b.z,
|
||||
.w = a.w - b.w,
|
||||
};
|
||||
}
|
||||
#endif
|
||||
|
||||
// Return signed integer version of a - b:
|
||||
#ifndef VEC_FN_ISUB
|
||||
#define VEC_FN_ISUB
|
||||
static inline union vec
|
||||
vec_isub (const union vec a, const union vec b)
|
||||
{
|
||||
return (union vec) {
|
||||
.xi = a.xi - b.xi,
|
||||
.yi = a.yi - b.yi,
|
||||
.zi = a.zi - b.zi,
|
||||
.wi = a.wi - b.wi,
|
||||
};
|
||||
}
|
||||
#endif
|
||||
|
||||
// Return unsigned integer version of a - b:
|
||||
#ifndef VEC_FN_USUB
|
||||
#define VEC_FN_USUB
|
||||
static inline union vec
|
||||
vec_usub (const union vec a, const union vec b)
|
||||
{
|
||||
return (union vec) {
|
||||
.xu = a.xu - b.xu,
|
||||
.yu = a.yu - b.yu,
|
||||
.zu = a.zu - b.zu,
|
||||
.wu = a.wu - b.wu,
|
||||
};
|
||||
}
|
||||
#endif
|
||||
|
||||
// Return float version of a * b:
|
||||
#ifndef VEC_FN_MUL
|
||||
#define VEC_FN_MUL
|
||||
static inline union vec
|
||||
vec_mul (const union vec a, const union vec b)
|
||||
{
|
||||
return (union vec) {
|
||||
.x = a.x * b.x,
|
||||
.y = a.y * b.y,
|
||||
.z = a.z * b.z,
|
||||
.w = a.w * b.w,
|
||||
};
|
||||
}
|
||||
#endif
|
||||
|
||||
// Return float version of a / b:
|
||||
#ifndef VEC_FN_DIV
|
||||
#define VEC_FN_DIV
|
||||
static inline union vec
|
||||
vec_div (const union vec a, const union vec b)
|
||||
{
|
||||
return (union vec) {
|
||||
.x = a.x / b.x,
|
||||
.y = a.y / b.y,
|
||||
.z = a.z / b.z,
|
||||
.w = a.w / b.w,
|
||||
};
|
||||
}
|
||||
#endif
|
||||
|
||||
// Return bitwise a AND b:
|
||||
#ifndef VEC_FN_AND
|
||||
#define VEC_FN_AND
|
||||
static inline union vec
|
||||
vec_and (const union vec a, const union vec b)
|
||||
{
|
||||
return (union vec) {
|
||||
.xu = a.xu & b.xu,
|
||||
.yu = a.yu & b.yu,
|
||||
.zu = a.zu & b.zu,
|
||||
.wu = a.wu & b.wu,
|
||||
};
|
||||
}
|
||||
#endif
|
||||
|
||||
// Return bitwise a OR b:
|
||||
#ifndef VEC_FN_OR
|
||||
#define VEC_FN_OR
|
||||
static inline union vec
|
||||
vec_or (const union vec a, const union vec b)
|
||||
{
|
||||
return (union vec) {
|
||||
.xu = a.xu | b.xu,
|
||||
.yu = a.yu | b.yu,
|
||||
.zu = a.zu | b.zu,
|
||||
.wu = a.wu | b.wu,
|
||||
};
|
||||
}
|
||||
#endif
|
||||
|
||||
// Return bitwise a XOR b:
|
||||
#ifndef VEC_FN_XOR
|
||||
#define VEC_FN_XOR
|
||||
static inline union vec
|
||||
vec_xor (const union vec a, const union vec b)
|
||||
{
|
||||
return (union vec) {
|
||||
.xu = a.xu ^ b.xu,
|
||||
.yu = a.yu ^ b.yu,
|
||||
.zu = a.zu ^ b.zu,
|
||||
.wu = a.wu ^ b.wu,
|
||||
};
|
||||
}
|
||||
#endif
|
||||
|
||||
// Return bitwise NOT v:
|
||||
#ifndef VEC_FN_NOT
|
||||
#define VEC_FN_NOT
|
||||
static inline union vec
|
||||
vec_not (const union vec v)
|
||||
{
|
||||
return (union vec) {
|
||||
.xu = ~v.xu,
|
||||
.yu = ~v.yu,
|
||||
.zu = ~v.zu,
|
||||
.wu = ~v.wu,
|
||||
};
|
||||
}
|
||||
#endif
|
||||
|
||||
// Return float version of a == b:
|
||||
#ifndef VEC_FN_EQ
|
||||
#define VEC_FN_EQ
|
||||
static inline union vec
|
||||
vec_eq (const union vec a, const union vec b)
|
||||
{
|
||||
return (union vec) {
|
||||
.xi = a.x == b.x ? -1 : 0,
|
||||
.yi = a.y == b.y ? -1 : 0,
|
||||
.zi = a.z == b.z ? -1 : 0,
|
||||
.wi = a.w == b.w ? -1 : 0,
|
||||
};
|
||||
}
|
||||
#endif
|
||||
|
||||
// Return signed integer version of a == b:
|
||||
#ifndef VEC_FN_IEQ
|
||||
#define VEC_FN_IEQ
|
||||
static inline union vec
|
||||
vec_ieq (const union vec a, const union vec b)
|
||||
{
|
||||
return (union vec) {
|
||||
.xi = a.xi == b.xi ? -1 : 0,
|
||||
.yi = a.yi == b.yi ? -1 : 0,
|
||||
.zi = a.zi == b.zi ? -1 : 0,
|
||||
.wi = a.wi == b.wi ? -1 : 0,
|
||||
};
|
||||
}
|
||||
#endif
|
||||
|
||||
// Return float version of a == b:
|
||||
#ifndef VEC_FN_UEQ
|
||||
#define VEC_FN_UEQ
|
||||
static inline union vec
|
||||
vec_ueq (const union vec a, const union vec b)
|
||||
{
|
||||
return (union vec) {
|
||||
.xi = a.xu == b.xu ? -1 : 0,
|
||||
.yi = a.yu == b.yu ? -1 : 0,
|
||||
.zi = a.zu == b.zu ? -1 : 0,
|
||||
.wi = a.wu == b.wu ? -1 : 0,
|
||||
};
|
||||
}
|
||||
#endif
|
||||
|
||||
// Return float version of a < b:
|
||||
#ifndef VEC_FN_LT
|
||||
#define VEC_FN_LT
|
||||
static inline union vec
|
||||
vec_lt (const union vec a, const union vec b)
|
||||
{
|
||||
return (union vec) {
|
||||
.xi = a.x < b.x ? -1 : 0,
|
||||
.yi = a.y < b.y ? -1 : 0,
|
||||
.zi = a.z < b.z ? -1 : 0,
|
||||
.wi = a.w < b.w ? -1 : 0,
|
||||
};
|
||||
}
|
||||
#endif
|
||||
|
||||
// Return signed integer version of a < b:
|
||||
#ifndef VEC_FN_ILT
|
||||
#define VEC_FN_ILT
|
||||
static inline union vec
|
||||
vec_ilt (const union vec a, const union vec b)
|
||||
{
|
||||
return (union vec) {
|
||||
.xi = a.xi < b.xi ? -1 : 0,
|
||||
.yi = a.yi < b.yi ? -1 : 0,
|
||||
.zi = a.zi < b.zi ? -1 : 0,
|
||||
.wi = a.wi < b.wi ? -1 : 0,
|
||||
};
|
||||
}
|
||||
#endif
|
||||
|
||||
// Return unsigned integer version of a < b:
|
||||
#ifndef VEC_FN_ULT
|
||||
#define VEC_FN_ULT
|
||||
static inline union vec
|
||||
vec_ult (const union vec a, const union vec b)
|
||||
{
|
||||
return (union vec) {
|
||||
.xi = a.xu < b.xu ? -1 : 0,
|
||||
.yi = a.yu < b.yu ? -1 : 0,
|
||||
.zi = a.zu < b.zu ? -1 : 0,
|
||||
.wi = a.wu < b.wu ? -1 : 0,
|
||||
};
|
||||
}
|
||||
#endif
|
||||
|
||||
// Return float version of a <= b:
|
||||
#ifndef VEC_FN_LE
|
||||
#define VEC_FN_LE
|
||||
static inline union vec
|
||||
vec_le (const union vec a, const union vec b)
|
||||
{
|
||||
return (union vec) {
|
||||
.xi = a.x <= b.x ? -1 : 0,
|
||||
.yi = a.y <= b.y ? -1 : 0,
|
||||
.zi = a.z <= b.z ? -1 : 0,
|
||||
.wi = a.w <= b.w ? -1 : 0,
|
||||
};
|
||||
}
|
||||
#endif
|
||||
|
||||
// Return signed integer version of a <= b:
|
||||
#ifndef VEC_FN_ILE
|
||||
#define VEC_FN_ILE
|
||||
static inline union vec
|
||||
vec_ile (const union vec a, const union vec b)
|
||||
{
|
||||
return (union vec) {
|
||||
.xi = a.xi <= b.xi ? -1 : 0,
|
||||
.yi = a.yi <= b.yi ? -1 : 0,
|
||||
.zi = a.zi <= b.zi ? -1 : 0,
|
||||
.wi = a.wi <= b.wi ? -1 : 0,
|
||||
};
|
||||
}
|
||||
#endif
|
||||
|
||||
// Return unsigned integer version of a <= b:
|
||||
#ifndef VEC_FN_ULE
|
||||
#define VEC_FN_ULE
|
||||
static inline union vec
|
||||
vec_ule (const union vec a, const union vec b)
|
||||
{
|
||||
return (union vec) {
|
||||
.xi = a.xu <= b.xu ? -1 : 0,
|
||||
.yi = a.yu <= b.yu ? -1 : 0,
|
||||
.zi = a.zu <= b.zu ? -1 : 0,
|
||||
.wi = a.wu <= b.wu ? -1 : 0,
|
||||
};
|
||||
}
|
||||
#endif
|
||||
|
||||
// Return float version of a > b:
|
||||
#ifndef VEC_FN_GT
|
||||
#define VEC_FN_GT
|
||||
static inline union vec
|
||||
vec_gt (const union vec a, const union vec b)
|
||||
{
|
||||
return (union vec) {
|
||||
.xi = a.x > b.x ? -1 : 0,
|
||||
.yi = a.y > b.y ? -1 : 0,
|
||||
.zi = a.z > b.z ? -1 : 0,
|
||||
.wi = a.w > b.w ? -1 : 0,
|
||||
};
|
||||
}
|
||||
#endif
|
||||
|
||||
// Return signed integer version of a > b:
|
||||
#ifndef VEC_FN_IGT
|
||||
#define VEC_FN_IGT
|
||||
static inline union vec
|
||||
vec_igt (const union vec a, const union vec b)
|
||||
{
|
||||
return (union vec) {
|
||||
.xi = a.xi > b.xi ? -1 : 0,
|
||||
.yi = a.yi > b.yi ? -1 : 0,
|
||||
.zi = a.zi > b.zi ? -1 : 0,
|
||||
.wi = a.wi > b.wi ? -1 : 0,
|
||||
};
|
||||
}
|
||||
#endif
|
||||
|
||||
// Return unsigned integer version of a > b:
|
||||
#ifndef VEC_FN_UGT
|
||||
#define VEC_FN_UGT
|
||||
static inline union vec
|
||||
vec_ugt (const union vec a, const union vec b)
|
||||
{
|
||||
return (union vec) {
|
||||
.xi = a.xu > b.xu ? -1 : 0,
|
||||
.yi = a.yu > b.yu ? -1 : 0,
|
||||
.zi = a.zu > b.zu ? -1 : 0,
|
||||
.wi = a.wu > b.wu ? -1 : 0,
|
||||
};
|
||||
}
|
||||
#endif
|
||||
|
||||
// Return float version of a >= b:
|
||||
#ifndef VEC_FN_GE
|
||||
#define VEC_FN_GE
|
||||
static inline union vec
|
||||
vec_ge (const union vec a, const union vec b)
|
||||
{
|
||||
return (union vec) {
|
||||
.xi = a.x >= b.x ? -1 : 0,
|
||||
.yi = a.y >= b.y ? -1 : 0,
|
||||
.zi = a.z >= b.z ? -1 : 0,
|
||||
.wi = a.w >= b.w ? -1 : 0,
|
||||
};
|
||||
}
|
||||
#endif
|
||||
|
||||
// Return signed integer version of a >= b:
|
||||
#ifndef VEC_FN_IGE
|
||||
#define VEC_FN_IGE
|
||||
static inline union vec
|
||||
vec_ige (const union vec a, const union vec b)
|
||||
{
|
||||
return (union vec) {
|
||||
.xi = a.xi >= b.xi ? -1 : 0,
|
||||
.yi = a.yi >= b.yi ? -1 : 0,
|
||||
.zi = a.zi >= b.zi ? -1 : 0,
|
||||
.wi = a.wi >= b.wi ? -1 : 0,
|
||||
};
|
||||
}
|
||||
#endif
|
||||
|
||||
// Return unsigned integer version of a >= b:
|
||||
#ifndef VEC_FN_UGE
|
||||
#define VEC_FN_UGE
|
||||
static inline union vec
|
||||
vec_uge (const union vec a, const union vec b)
|
||||
{
|
||||
return (union vec) {
|
||||
.xi = a.xu >= b.xu ? -1 : 0,
|
||||
.yi = a.yu >= b.yu ? -1 : 0,
|
||||
.zi = a.zu >= b.zu ? -1 : 0,
|
||||
.wi = a.wu >= b.wu ? -1 : 0,
|
||||
};
|
||||
}
|
||||
#endif
|
||||
|
||||
// Return dot product of two 3D vectors:
|
||||
#ifndef VEC_FN_DOT
|
||||
#define VEC_FN_DOT
|
||||
static inline float
|
||||
vec_dot (const union vec a, const union vec b)
|
||||
{
|
||||
return a.x * b.x + a.y * b.y + a.z * b.z;
|
||||
}
|
||||
#endif
|
||||
|
||||
// Return cross product of two 3D vectors:
|
||||
#ifndef VEC_FN_CROSS
|
||||
#define VEC_FN_CROSS
|
||||
static inline union vec
|
||||
vec_cross (const union vec a, const union vec b)
|
||||
{
|
||||
return (union vec) {
|
||||
.x = a.y * b.z - a.z * b.y,
|
||||
.y = a.z * b.x - a.x * b.z,
|
||||
.z = a.x * b.y - a.y * b.x,
|
||||
.w = 0.0f,
|
||||
};
|
||||
}
|
||||
#endif
|
||||
@@ -0,0 +1,90 @@
|
||||
// Return float version of a / b:
|
||||
#ifndef VEC_FN_DIV
|
||||
#define VEC_FN_DIV
|
||||
static inline union vec
|
||||
vec_div (const union vec a, const union vec b)
|
||||
{
|
||||
#ifdef __aarch64__
|
||||
float32x4_t ret;
|
||||
|
||||
__asm__ (
|
||||
"fdiv %0.4s,%1.4s,%2.4s \n\t"
|
||||
|
||||
: "=&w" (ret) // Outputs: ret=v0
|
||||
: "w" (a.neon.f), "w" (b.neon.f) // Inputs: a=v1, b=v2
|
||||
);
|
||||
#else
|
||||
float32x4_t ret, tmp;
|
||||
|
||||
__asm__ (
|
||||
"vrecpeq.f32 %q0,%q3 \n\t"
|
||||
"vrecpsq.f32 %q1,%q3,%q0 \n\t"
|
||||
"vmulq.f32 %q0,%q1,%q0 \n\t"
|
||||
"vrecpsq.f32 %q1,%q3,%q0 \n\t"
|
||||
"vmulq.f32 %q0,%q1,%q0 \n\t"
|
||||
"vmulq.f32 %q0,%q2,%q0 \n\t"
|
||||
|
||||
: "=&w" (ret), "=&w" (tmp) // Outputs: ret=q0, tmp=q1
|
||||
: "w" (a.neon.f), "w" (b.neon.f) // Inputs: a=q2, b=q3
|
||||
);
|
||||
#endif
|
||||
|
||||
return (union vec) { .neon.f = ret };
|
||||
}
|
||||
#endif
|
||||
|
||||
// Return cross product of two 3D vectors:
|
||||
#ifndef VEC_FN_CROSS
|
||||
#define VEC_FN_CROSS
|
||||
static inline union vec
|
||||
vec_cross (const union vec a, const union vec b)
|
||||
{
|
||||
// Make read/write copies of the inputs:
|
||||
float32x4_t atmp = a.neon.f, btmp = b.neon.f;
|
||||
|
||||
#ifdef __aarch64__
|
||||
float32x4_t ret, t1, t2;
|
||||
|
||||
__asm__ (
|
||||
"zip1 %0.4s,%3.4s,%4.4s \n\t" // v0 := a0 b0 a1 b1
|
||||
"zip2 %1.4s,%3.4s,%4.4s \n\t" // v1 := a2 b2 a3 b3
|
||||
"trn1 %2.4s,%3.4s,%4.4s \n\t" // v2 := a0 b0 a2 b2
|
||||
"trn2 %3.4s,%3.4s,%4.4s \n\t" // v3 := a1 b1 a3 b3
|
||||
"ext %0.16b,%0.16b,%0.16b,#8 \n\t" // v0 := a1 b1 a0 b0
|
||||
"ext %2.16b,%2.16b,%2.16b,#8 \n\t" // v2 := a2 b2 a0 b0
|
||||
"trn2 %4.4s,%0.4s,%1.4s \n\t" // v4 := b1 b2 b0 b3 = byzxw
|
||||
"trn1 %0.4s,%0.4s,%1.4s \n\t" // v0 := a1 a2 a0 a3 = ayzxw
|
||||
"uzp1 %1.4s,%2.4s,%3.4s \n\t" // v1 := a2 a0 a1 a3 = azxyw
|
||||
"uzp2 %2.4s,%2.4s,%3.4s \n\t" // v2 := b2 b0 b1 b3 = bzxyw
|
||||
|
||||
"fmul %0.4s,%0.4s,%2.4s \n\t" // v0 := ayzxw * bzxyw
|
||||
"fmls %0.4s,%1.4s,%4.4s \n\t" // v0 := ayzxw * bzxyw - azxyw * byzxw
|
||||
|
||||
: "=&w" (ret), "=&w" (t1), "=&w" (t2), // Outputs: ret=v0, t1=v1, t2=v2
|
||||
"+w" (atmp), "+w" (btmp) // Outputs (reused inputs): a=v3, b=v4
|
||||
);
|
||||
#else
|
||||
float32x4_t ret, tmp;
|
||||
|
||||
__asm__ (
|
||||
"vext.8 %q0,%q2,%q2,#4 \n\t" // q0 := ayzwx
|
||||
"vext.8 %q1,%q3,%q3,#4 \n\t" // q1 := byzwx
|
||||
"vrev64.32 %f0,%f0 \n\t" // q0 := ayzxw
|
||||
"vrev64.32 %f1,%f1 \n\t" // q1 := byzxw
|
||||
|
||||
"vtrn.32 %e2,%f2 \n\t" // q2 := axzyw
|
||||
"vtrn.32 %e3,%f3 \n\t" // q3 := bxzyw
|
||||
"vrev64.32 %e2,%e2 \n\t" // q2 := azxyw
|
||||
"vrev64.32 %e3,%e3 \n\t" // q3 := bzxyw
|
||||
|
||||
"vmulq.f32 %q0,%q0,%q3 \n\t" // q0 := ayzxw * bzxyw
|
||||
"vmlsq.f32 %q0,%q2,%q1 \n\t" // q0 := ayzxw * bzxyw - azxyw * byzxw
|
||||
|
||||
: "=&w" (ret), "=&w" (tmp), // Outputs: ret=q0, tmp=q1
|
||||
"+w" (atmp), "+w" (btmp) // Outputs (reused inputs): a=q2, b=q3
|
||||
);
|
||||
#endif
|
||||
|
||||
return (union vec) { .neon.f = ret };
|
||||
}
|
||||
#endif
|
||||
@@ -0,0 +1,440 @@
|
||||
// Instantiate a float vector:
|
||||
#ifndef VEC_FN_
|
||||
#define VEC_FN_
|
||||
static inline union vec
|
||||
vec (const float x, const float y, const float z, const float w)
|
||||
{
|
||||
union {
|
||||
float i[2];
|
||||
uint64_t u;
|
||||
} lo = { { x, y } }
|
||||
, hi = { { z, w } };
|
||||
|
||||
return (union vec) { .neon.f = vcombine_f32(vcreate_f32(lo.u), vcreate_f32(hi.u)) };
|
||||
}
|
||||
#endif
|
||||
|
||||
// Instantiate a signed integer vector:
|
||||
#ifndef VEC_FN_I
|
||||
#define VEC_FN_I
|
||||
static inline union vec
|
||||
vec_i (const int32_t x, const int32_t y, const int32_t z, const int32_t w)
|
||||
{
|
||||
union {
|
||||
int32_t i[2];
|
||||
uint64_t u;
|
||||
} lo = { { x, y } }
|
||||
, hi = { { z, w } };
|
||||
|
||||
return (union vec) { .neon.i = vcombine_s32(vcreate_s32(lo.u), vcreate_s32(hi.u)) };
|
||||
}
|
||||
#endif
|
||||
|
||||
// Instantiate an unsigned integer vector:
|
||||
#ifndef VEC_FN_U
|
||||
#define VEC_FN_U
|
||||
static inline union vec
|
||||
vec_u (const uint32_t x, const uint32_t y, const uint32_t z, const uint32_t w)
|
||||
{
|
||||
union {
|
||||
uint32_t i[2];
|
||||
uint64_t u;
|
||||
} lo = { { x, y } }
|
||||
, hi = { { z, w } };
|
||||
|
||||
return (union vec) { .neon.u = vcombine_u32(vcreate_u32(lo.u), vcreate_u32(hi.u)) };
|
||||
}
|
||||
#endif
|
||||
|
||||
// Instantiate a float vector with four identical elements:
|
||||
#ifndef VEC_FN_1
|
||||
#define VEC_FN_1
|
||||
static inline union vec
|
||||
vec_1 (const float val)
|
||||
{
|
||||
return (union vec) { .neon.f = vdupq_n_f32(val) };
|
||||
}
|
||||
#endif
|
||||
|
||||
// Instantiate a signed integer vector with four identical elements:
|
||||
#ifndef VEC_FN_I1
|
||||
#define VEC_FN_I1
|
||||
static inline union vec
|
||||
vec_i1 (const int32_t val)
|
||||
{
|
||||
return (union vec) { .neon.i = vdupq_n_s32(val) };
|
||||
}
|
||||
#endif
|
||||
|
||||
// Instantiate an unsigned integer vector with four identical elements:
|
||||
#ifndef VEC_FN_U1
|
||||
#define VEC_FN_U1
|
||||
static inline union vec
|
||||
vec_u1 (const uint32_t val)
|
||||
{
|
||||
return (union vec) { .neon.u = vdupq_n_u32(val) };
|
||||
}
|
||||
#endif
|
||||
|
||||
// Instantiate a float vector set to all zeros:
|
||||
#ifndef VEC_FN_ZERO
|
||||
#define VEC_FN_ZERO
|
||||
static inline union vec
|
||||
vec_zero (void)
|
||||
{
|
||||
return vec_1(0.0f);
|
||||
}
|
||||
#endif
|
||||
|
||||
// Instantiate a signed integer vector set to all zeros:
|
||||
#ifndef VEC_FN_IZERO
|
||||
#define VEC_FN_IZERO
|
||||
static inline union vec
|
||||
vec_izero (void)
|
||||
{
|
||||
return vec_i1(0);
|
||||
}
|
||||
#endif
|
||||
|
||||
// Instantiate an unsigned integer vector set to all zeros:
|
||||
#ifndef VEC_FN_UZERO
|
||||
#define VEC_FN_UZERO
|
||||
static inline union vec
|
||||
vec_uzero (void)
|
||||
{
|
||||
return vec_u1(0U);
|
||||
}
|
||||
#endif
|
||||
|
||||
// Convert a float vector to signed integer:
|
||||
#ifndef VEC_FN_TO_INT
|
||||
#define VEC_FN_TO_INT
|
||||
static inline union vec
|
||||
vec_to_int (const union vec v)
|
||||
{
|
||||
return (union vec) { .neon.i = vcvtq_s32_f32(v.neon.f) };
|
||||
}
|
||||
#endif
|
||||
|
||||
// Convert a signed integer vector to float:
|
||||
#ifndef VEC_FN_TO_FLOAT
|
||||
#define VEC_FN_TO_FLOAT
|
||||
static inline union vec
|
||||
vec_to_float (const union vec v)
|
||||
{
|
||||
return (union vec) { .neon.f = vcvtq_f32_s32(v.neon.i) };
|
||||
}
|
||||
#endif
|
||||
|
||||
// Return float version of a + b:
|
||||
#ifndef VEC_FN_ADD
|
||||
#define VEC_FN_ADD
|
||||
static inline union vec
|
||||
vec_add (const union vec a, const union vec b)
|
||||
{
|
||||
return (union vec) { .neon.f = vaddq_f32(a.neon.f, b.neon.f) };
|
||||
}
|
||||
#endif
|
||||
|
||||
// Return signed integer version of a + b:
|
||||
#ifndef VEC_FN_IADD
|
||||
#define VEC_FN_IADD
|
||||
static inline union vec
|
||||
vec_iadd (const union vec a, const union vec b)
|
||||
{
|
||||
return (union vec) { .neon.i = vaddq_s32(a.neon.i, b.neon.i) };
|
||||
}
|
||||
#endif
|
||||
|
||||
// Return unsigned integer version of a + b:
|
||||
#ifndef VEC_FN_UADD
|
||||
#define VEC_FN_UADD
|
||||
static inline union vec
|
||||
vec_uadd (const union vec a, const union vec b)
|
||||
{
|
||||
return (union vec) { .neon.u = vaddq_u32(a.neon.u, b.neon.u) };
|
||||
}
|
||||
#endif
|
||||
|
||||
// Return float version of a - b:
|
||||
#ifndef VEC_FN_SUB
|
||||
#define VEC_FN_SUB
|
||||
static inline union vec
|
||||
vec_sub (const union vec a, const union vec b)
|
||||
{
|
||||
return (union vec) { .neon.f = vsubq_f32(a.neon.f, b.neon.f) };
|
||||
}
|
||||
#endif
|
||||
|
||||
// Return signed integer version of a - b:
|
||||
#ifndef VEC_FN_ISUB
|
||||
#define VEC_FN_ISUB
|
||||
static inline union vec
|
||||
vec_isub (const union vec a, const union vec b)
|
||||
{
|
||||
return (union vec) { .neon.i = vsubq_s32(a.neon.i, b.neon.i) };
|
||||
}
|
||||
#endif
|
||||
|
||||
// Return unsigned integer version of a - b:
|
||||
#ifndef VEC_FN_USUB
|
||||
#define VEC_FN_USUB
|
||||
static inline union vec
|
||||
vec_usub (const union vec a, const union vec b)
|
||||
{
|
||||
return (union vec) { .neon.u = vsubq_u32(a.neon.u, b.neon.u) };
|
||||
}
|
||||
#endif
|
||||
|
||||
// Return float version of a * b:
|
||||
#ifndef VEC_FN_MUL
|
||||
#define VEC_FN_MUL
|
||||
static inline union vec
|
||||
vec_mul (const union vec a, const union vec b)
|
||||
{
|
||||
return (union vec) { .neon.f = vmulq_f32(a.neon.f, b.neon.f) };
|
||||
}
|
||||
#endif
|
||||
|
||||
// Return float version of a / b (intrinsics version):
|
||||
#ifndef VEC_FN_DIV
|
||||
#define VEC_FN_DIV
|
||||
static inline union vec
|
||||
vec_div (const union vec a, const union vec b)
|
||||
{
|
||||
// Estimate reciprocal of b:
|
||||
float32x4_t recp = vrecpeq_f32(b.neon.f);
|
||||
|
||||
// Refine the estimate using Newton-Raphson:
|
||||
recp = vmulq_f32(vrecpsq_f32(b.neon.f, recp), recp);
|
||||
recp = vmulq_f32(vrecpsq_f32(b.neon.f, recp), recp);
|
||||
|
||||
// Multiply by the reciprocal to approximate a / b:
|
||||
return (union vec) { .neon.f = vmulq_f32(a.neon.f, recp) };
|
||||
}
|
||||
#endif
|
||||
|
||||
// Return bitwise a AND b:
|
||||
#ifndef VEC_FN_AND
|
||||
#define VEC_FN_AND
|
||||
static inline union vec
|
||||
vec_and (const union vec a, const union vec b)
|
||||
{
|
||||
return (union vec) { .neon.u = vandq_u32(a.neon.u, b.neon.u) };
|
||||
}
|
||||
#endif
|
||||
|
||||
// Return bitwise a OR b:
|
||||
#ifndef VEC_FN_OR
|
||||
#define VEC_FN_OR
|
||||
static inline union vec
|
||||
vec_or (const union vec a, const union vec b)
|
||||
{
|
||||
return (union vec) { .neon.u = vorrq_u32(a.neon.u, b.neon.u) };
|
||||
}
|
||||
#endif
|
||||
|
||||
// Return bitwise a XOR b:
|
||||
#ifndef VEC_FN_XOR
|
||||
#define VEC_FN_XOR
|
||||
static inline union vec
|
||||
vec_xor (const union vec a, const union vec b)
|
||||
{
|
||||
return (union vec) { .neon.u = veorq_u32(a.neon.u, b.neon.u) };
|
||||
}
|
||||
#endif
|
||||
|
||||
// Return bitwise NOT v:
|
||||
#ifndef VEC_FN_NOT
|
||||
#define VEC_FN_NOT
|
||||
static inline union vec
|
||||
vec_not (const union vec v)
|
||||
{
|
||||
return (union vec) { .neon.u = vmvnq_u32(v.neon.u) };
|
||||
}
|
||||
#endif
|
||||
|
||||
// Return float version of a == b:
|
||||
#ifndef VEC_FN_EQ
|
||||
#define VEC_FN_EQ
|
||||
static inline union vec
|
||||
vec_eq (const union vec a, const union vec b)
|
||||
{
|
||||
return (union vec) { .neon.u = vceqq_f32(a.neon.f, b.neon.f) };
|
||||
}
|
||||
#endif
|
||||
|
||||
// Return signed integer version of a == b:
|
||||
#ifndef VEC_FN_IEQ
|
||||
#define VEC_FN_IEQ
|
||||
static inline union vec
|
||||
vec_ieq (const union vec a, const union vec b)
|
||||
{
|
||||
return (union vec) { .neon.u = vceqq_s32(a.neon.i, b.neon.i) };
|
||||
}
|
||||
#endif
|
||||
|
||||
// Return float version of a == b:
|
||||
#ifndef VEC_FN_UEQ
|
||||
#define VEC_FN_UEQ
|
||||
static inline union vec
|
||||
vec_ueq (const union vec a, const union vec b)
|
||||
{
|
||||
return (union vec) { .neon.u = vceqq_u32(a.neon.u, b.neon.u) };
|
||||
}
|
||||
#endif
|
||||
|
||||
// Return float version of a < b:
|
||||
#ifndef VEC_FN_LT
|
||||
#define VEC_FN_LT
|
||||
static inline union vec
|
||||
vec_lt (const union vec a, const union vec b)
|
||||
{
|
||||
return (union vec) { .neon.u = vcltq_f32(a.neon.f, b.neon.f) };
|
||||
}
|
||||
#endif
|
||||
|
||||
// Return signed integer version of a < b:
|
||||
#ifndef VEC_FN_ILT
|
||||
#define VEC_FN_ILT
|
||||
static inline union vec
|
||||
vec_ilt (const union vec a, const union vec b)
|
||||
{
|
||||
return (union vec) { .neon.u = vcltq_s32(a.neon.i, b.neon.i) };
|
||||
}
|
||||
#endif
|
||||
|
||||
// Return unsigned integer version of a < b:
|
||||
#ifndef VEC_FN_ULT
|
||||
#define VEC_FN_ULT
|
||||
static inline union vec
|
||||
vec_ult (const union vec a, const union vec b)
|
||||
{
|
||||
return (union vec) { .neon.u = vcltq_u32(a.neon.u, b.neon.u) };
|
||||
}
|
||||
#endif
|
||||
|
||||
// Return float version of a <= b:
|
||||
#ifndef VEC_FN_LE
|
||||
#define VEC_FN_LE
|
||||
static inline union vec
|
||||
vec_le (const union vec a, const union vec b)
|
||||
{
|
||||
return (union vec) { .neon.u = vcleq_f32(a.neon.f, b.neon.f) };
|
||||
}
|
||||
#endif
|
||||
|
||||
// Return signed integer version of a <= b:
|
||||
#ifndef VEC_FN_ILE
|
||||
#define VEC_FN_ILE
|
||||
static inline union vec
|
||||
vec_ile (const union vec a, const union vec b)
|
||||
{
|
||||
return (union vec) { .neon.u = vcleq_s32(a.neon.i, b.neon.i) };
|
||||
}
|
||||
#endif
|
||||
|
||||
// Return unsigned integer version of a <= b:
|
||||
#ifndef VEC_FN_ULE
|
||||
#define VEC_FN_ULE
|
||||
static inline union vec
|
||||
vec_ule (const union vec a, const union vec b)
|
||||
{
|
||||
return (union vec) { .neon.u = vcleq_u32(a.neon.u, b.neon.u) };
|
||||
}
|
||||
#endif
|
||||
|
||||
// Return float version of a > b:
|
||||
#ifndef VEC_FN_GT
|
||||
#define VEC_FN_GT
|
||||
static inline union vec
|
||||
vec_gt (const union vec a, const union vec b)
|
||||
{
|
||||
return (union vec) { .neon.u = vcgtq_f32(a.neon.f, b.neon.f) };
|
||||
}
|
||||
#endif
|
||||
|
||||
// Return signed integer version of a > b:
|
||||
#ifndef VEC_FN_IGT
|
||||
#define VEC_FN_IGT
|
||||
static inline union vec
|
||||
vec_igt (const union vec a, const union vec b)
|
||||
{
|
||||
return (union vec) { .neon.u = vcgtq_s32(a.neon.i, b.neon.i) };
|
||||
}
|
||||
#endif
|
||||
|
||||
// Return unsigned integer version of a > b:
|
||||
#ifndef VEC_FN_UGT
|
||||
#define VEC_FN_UGT
|
||||
static inline union vec
|
||||
vec_ugt (const union vec a, const union vec b)
|
||||
{
|
||||
return (union vec) { .neon.u = vcgtq_u32(a.neon.u, b.neon.u) };
|
||||
}
|
||||
#endif
|
||||
|
||||
// Return float version of a >= b:
|
||||
#ifndef VEC_FN_GE
|
||||
#define VEC_FN_GE
|
||||
static inline union vec
|
||||
vec_ge (const union vec a, const union vec b)
|
||||
{
|
||||
return (union vec) { .neon.u = vcgeq_f32(a.neon.f, b.neon.f) };
|
||||
}
|
||||
#endif
|
||||
|
||||
// Return signed integer version of a >= b:
|
||||
#ifndef VEC_FN_IGE
|
||||
#define VEC_FN_IGE
|
||||
static inline union vec
|
||||
vec_ige (const union vec a, const union vec b)
|
||||
{
|
||||
return (union vec) { .neon.u = vcgeq_s32(a.neon.i, b.neon.i) };
|
||||
}
|
||||
#endif
|
||||
|
||||
// Return unsigned integer version of a >= b:
|
||||
#ifndef VEC_FN_UGE
|
||||
#define VEC_FN_UGE
|
||||
static inline union vec
|
||||
vec_uge (const union vec a, const union vec b)
|
||||
{
|
||||
return (union vec) { .neon.u = vcgeq_u32(a.neon.u, b.neon.u) };
|
||||
}
|
||||
#endif
|
||||
|
||||
// Return dot product of two 3D vectors:
|
||||
#ifndef VEC_FN_DOT
|
||||
#define VEC_FN_DOT
|
||||
static inline float
|
||||
vec_dot (const union vec a, const union vec b)
|
||||
{
|
||||
const float32x4_t mul = vmulq_f32(a.neon.f, b.neon.f);
|
||||
return vgetq_lane_f32(mul, 0)
|
||||
+ vgetq_lane_f32(mul, 1)
|
||||
+ vgetq_lane_f32(mul, 2);
|
||||
}
|
||||
#endif
|
||||
|
||||
// Return cross product of two 3D vectors (intrinsics version):
|
||||
#ifndef VEC_FN_CROSS
|
||||
#define VEC_FN_CROSS
|
||||
static inline union vec
|
||||
vec_cross (const union vec a, const union vec b)
|
||||
{
|
||||
const float32x4_t ayzwx = (float32x4_t) vextq_u32((uint32x4_t) a.neon.f, (uint32x4_t) a.neon.f, 1);
|
||||
const float32x4_t byzwx = (float32x4_t) vextq_u32((uint32x4_t) b.neon.f, (uint32x4_t) b.neon.f, 1);
|
||||
|
||||
const float32x4_t ayzxw = vcombine_f32(vget_low_f32(ayzwx), vrev64_f32(vget_high_f32(ayzwx)));
|
||||
const float32x4_t byzxw = vcombine_f32(vget_low_f32(byzwx), vrev64_f32(vget_high_f32(byzwx)));
|
||||
|
||||
const float32x2x2_t azxwy_pair = vtrn_f32(vget_high_f32(a.neon.f), vget_low_f32(a.neon.f));
|
||||
const float32x2x2_t bzxwy_pair = vtrn_f32(vget_high_f32(b.neon.f), vget_low_f32(b.neon.f));
|
||||
|
||||
const float32x4_t azxyw = vcombine_f32(azxwy_pair.val[0], vrev64_f32(azxwy_pair.val[1]));
|
||||
const float32x4_t bzxyw = vcombine_f32(bzxwy_pair.val[0], vrev64_f32(bzxwy_pair.val[1]));
|
||||
|
||||
return (union vec) { .neon.f = vmlsq_f32(vmulq_f32(ayzxw, bzxyw), byzxw, azxyw) };
|
||||
}
|
||||
#endif
|
||||
@@ -0,0 +1,433 @@
|
||||
// Instantiate a float vector:
|
||||
#ifndef VEC_FN_
|
||||
#define VEC_FN_
|
||||
static inline union vec
|
||||
vec (const float x, const float y, const float z, const float w)
|
||||
{
|
||||
return (union vec) { .sse.f = _mm_setr_ps(x, y, z, w) };
|
||||
}
|
||||
#endif
|
||||
|
||||
// Instantiate a signed integer vector:
|
||||
#ifndef VEC_FN_I
|
||||
#define VEC_FN_I
|
||||
static inline union vec
|
||||
vec_i (const int32_t x, const int32_t y, const int32_t z, const int32_t w)
|
||||
{
|
||||
return (union vec) { .sse.i = _mm_setr_epi32(x, y, z, w) };
|
||||
}
|
||||
#endif
|
||||
|
||||
// Instantiate an unsigned integer vector:
|
||||
#ifndef VEC_FN_U
|
||||
#define VEC_FN_U
|
||||
static inline union vec
|
||||
vec_u (const uint32_t x, const uint32_t y, const uint32_t z, const uint32_t w)
|
||||
{
|
||||
return vec_i((int32_t) x, (int32_t) y, (int32_t) z, (int32_t) w);
|
||||
}
|
||||
#endif
|
||||
|
||||
// Instantiate a float vector with four identical elements:
|
||||
#ifndef VEC_FN_1
|
||||
#define VEC_FN_1
|
||||
static inline union vec
|
||||
vec_1 (const float val)
|
||||
{
|
||||
return (union vec) { .sse.f = _mm_set1_ps(val) };
|
||||
}
|
||||
#endif
|
||||
|
||||
// Instantiate a signed integer vector with four identical elements:
|
||||
#ifndef VEC_FN_I1
|
||||
#define VEC_FN_I1
|
||||
static inline union vec
|
||||
vec_i1 (const int32_t val)
|
||||
{
|
||||
return (union vec) { .sse.i = _mm_set1_epi32(val) };
|
||||
}
|
||||
#endif
|
||||
|
||||
// Instantiate an unsigned integer vector with four identical elements:
|
||||
#ifndef VEC_FN_U1
|
||||
#define VEC_FN_U1
|
||||
static inline union vec
|
||||
vec_u1 (const uint32_t val)
|
||||
{
|
||||
return vec_i1((int32_t) val);
|
||||
}
|
||||
#endif
|
||||
|
||||
// Instantiate a float vector set to all zeros:
|
||||
#ifndef VEC_FN_ZERO
|
||||
#define VEC_FN_ZERO
|
||||
static inline union vec
|
||||
vec_zero (void)
|
||||
{
|
||||
return (union vec) { .sse.f = _mm_setzero_ps() };
|
||||
}
|
||||
#endif
|
||||
|
||||
// Instantiate a signed integer vector set to all zeros:
|
||||
#ifndef VEC_FN_IZERO
|
||||
#define VEC_FN_IZERO
|
||||
static inline union vec
|
||||
vec_izero (void)
|
||||
{
|
||||
return (union vec) { .sse.i = _mm_setzero_si128() };
|
||||
}
|
||||
#endif
|
||||
|
||||
// Instantiate an unsigned integer vector set to all zeros:
|
||||
#ifndef VEC_FN_UZERO
|
||||
#define VEC_FN_UZERO
|
||||
static inline union vec
|
||||
vec_uzero (void)
|
||||
{
|
||||
return vec_izero();
|
||||
}
|
||||
#endif
|
||||
|
||||
// Convert a float vector to signed integer:
|
||||
#ifndef VEC_FN_TO_INT
|
||||
#define VEC_FN_TO_INT
|
||||
static inline union vec
|
||||
vec_to_int (const union vec v)
|
||||
{
|
||||
return (union vec) { .sse.i = _mm_cvttps_epi32(v.sse.f) };
|
||||
}
|
||||
#endif
|
||||
|
||||
// Convert a signed integer vector to float:
|
||||
#ifndef VEC_FN_TO_FLOAT
|
||||
#define VEC_FN_TO_FLOAT
|
||||
static inline union vec
|
||||
vec_to_float (const union vec v)
|
||||
{
|
||||
return (union vec) { .sse.f = _mm_cvtepi32_ps(v.sse.i) };
|
||||
}
|
||||
#endif
|
||||
|
||||
// Return float version of a + b:
|
||||
#ifndef VEC_FN_ADD
|
||||
#define VEC_FN_ADD
|
||||
static inline union vec
|
||||
vec_add (const union vec a, const union vec b)
|
||||
{
|
||||
return (union vec) { .sse.f = _mm_add_ps(a.sse.f, b.sse.f) };
|
||||
}
|
||||
#endif
|
||||
|
||||
// Return signed integer version of a + b:
|
||||
#ifndef VEC_FN_IADD
|
||||
#define VEC_FN_IADD
|
||||
static inline union vec
|
||||
vec_iadd (const union vec a, const union vec b)
|
||||
{
|
||||
return (union vec) { .sse.i = _mm_add_epi32(a.sse.i, b.sse.i) };
|
||||
}
|
||||
#endif
|
||||
|
||||
// Return unsigned integer version of a + b:
|
||||
#ifndef VEC_FN_UADD
|
||||
#define VEC_FN_UADD
|
||||
static inline union vec
|
||||
vec_uadd (const union vec a, const union vec b)
|
||||
{
|
||||
return vec_iadd(a, b);
|
||||
}
|
||||
#endif
|
||||
|
||||
// Return float version of a - b:
|
||||
#ifndef VEC_FN_SUB
|
||||
#define VEC_FN_SUB
|
||||
static inline union vec
|
||||
vec_sub (const union vec a, const union vec b)
|
||||
{
|
||||
return (union vec) { .sse.f = _mm_sub_ps(a.sse.f, b.sse.f) };
|
||||
}
|
||||
#endif
|
||||
|
||||
// Return signed integer version of a - b:
|
||||
#ifndef VEC_FN_ISUB
|
||||
#define VEC_FN_ISUB
|
||||
static inline union vec
|
||||
vec_isub (const union vec a, const union vec b)
|
||||
{
|
||||
return (union vec) { .sse.i = _mm_sub_epi32(a.sse.i, b.sse.i) };
|
||||
}
|
||||
#endif
|
||||
|
||||
// Return unsigned integer version of a - b:
|
||||
#ifndef VEC_FN_USUB
|
||||
#define VEC_FN_USUB
|
||||
static inline union vec
|
||||
vec_usub (const union vec a, const union vec b)
|
||||
{
|
||||
return vec_isub(a, b);
|
||||
}
|
||||
#endif
|
||||
|
||||
// Return float version of a * b:
|
||||
#ifndef VEC_FN_MUL
|
||||
#define VEC_FN_MUL
|
||||
static inline union vec
|
||||
vec_mul (const union vec a, const union vec b)
|
||||
{
|
||||
return (union vec) { .sse.f = _mm_mul_ps(a.sse.f, b.sse.f) };
|
||||
}
|
||||
#endif
|
||||
|
||||
// Return float version of a / b:
|
||||
#ifndef VEC_FN_DIV
|
||||
#define VEC_FN_DIV
|
||||
static inline union vec
|
||||
vec_div (const union vec a, const union vec b)
|
||||
{
|
||||
return (union vec) { .sse.f = _mm_div_ps(a.sse.f, b.sse.f) };
|
||||
}
|
||||
#endif
|
||||
|
||||
// Return bitwise a AND b:
|
||||
#ifndef VEC_FN_AND
|
||||
#define VEC_FN_AND
|
||||
static inline union vec
|
||||
vec_and (const union vec a, const union vec b)
|
||||
{
|
||||
return (union vec) { .sse.u = _mm_and_si128(a.sse.u, b.sse.u) };
|
||||
}
|
||||
#endif
|
||||
|
||||
// Return bitwise a OR b:
|
||||
#ifndef VEC_FN_OR
|
||||
#define VEC_FN_OR
|
||||
static inline union vec
|
||||
vec_or (const union vec a, const union vec b)
|
||||
{
|
||||
return (union vec) { .sse.u = _mm_or_si128(a.sse.u, b.sse.u) };
|
||||
}
|
||||
#endif
|
||||
|
||||
// Return bitwise a XOR b:
|
||||
#ifndef VEC_FN_XOR
|
||||
#define VEC_FN_XOR
|
||||
static inline union vec
|
||||
vec_xor (const union vec a, const union vec b)
|
||||
{
|
||||
return (union vec) { .sse.u = _mm_xor_si128(a.sse.u, b.sse.u) };
|
||||
}
|
||||
#endif
|
||||
|
||||
// Return bitwise NOT v:
|
||||
#ifndef VEC_FN_NOT
|
||||
#define VEC_FN_NOT
|
||||
static inline union vec
|
||||
vec_not (const union vec v)
|
||||
{
|
||||
const __m128i allset = _mm_set1_epi32(INT32_C(0xFFFFFFFF));
|
||||
|
||||
return (union vec) { .sse.u = _mm_xor_si128(v.sse.u, allset) };
|
||||
}
|
||||
#endif
|
||||
|
||||
// Return float version of a == b:
|
||||
#ifndef VEC_FN_EQ
|
||||
#define VEC_FN_EQ
|
||||
static inline union vec
|
||||
vec_eq (const union vec a, const union vec b)
|
||||
{
|
||||
return (union vec) { .sse.f = _mm_cmpeq_ps(a.sse.f, b.sse.f) };
|
||||
}
|
||||
#endif
|
||||
|
||||
// Return signed integer version of a == b:
|
||||
#ifndef VEC_FN_IEQ
|
||||
#define VEC_FN_IEQ
|
||||
static inline union vec
|
||||
vec_ieq (const union vec a, const union vec b)
|
||||
{
|
||||
return (union vec) { .sse.i = _mm_cmpeq_epi32(a.sse.i, b.sse.i) };
|
||||
}
|
||||
#endif
|
||||
|
||||
// Return float version of a == b:
|
||||
#ifndef VEC_FN_UEQ
|
||||
#define VEC_FN_UEQ
|
||||
static inline union vec
|
||||
vec_ueq (const union vec a, const union vec b)
|
||||
{
|
||||
return (union vec) { .sse.u = _mm_cmpeq_epi32(a.sse.u, b.sse.u) };
|
||||
}
|
||||
#endif
|
||||
|
||||
// Return float version of a < b:
|
||||
#ifndef VEC_FN_LT
|
||||
#define VEC_FN_LT
|
||||
static inline union vec
|
||||
vec_lt (const union vec a, const union vec b)
|
||||
{
|
||||
return (union vec) { .sse.f = _mm_cmplt_ps(a.sse.f, b.sse.f) };
|
||||
}
|
||||
#endif
|
||||
|
||||
// Return signed integer version of a < b:
|
||||
#ifndef VEC_FN_ILT
|
||||
#define VEC_FN_ILT
|
||||
static inline union vec
|
||||
vec_ilt (const union vec a, const union vec b)
|
||||
{
|
||||
return (union vec) { .sse.i = _mm_cmplt_epi32(a.sse.i, b.sse.i) };
|
||||
}
|
||||
#endif
|
||||
|
||||
// Return unsigned integer version of a < b:
|
||||
#ifndef VEC_FN_ULT
|
||||
#define VEC_FN_ULT
|
||||
static inline union vec
|
||||
vec_ult (const union vec a, const union vec b)
|
||||
{
|
||||
const __m128i bias = _mm_set1_epi32(INT32_C(0x80000000));
|
||||
|
||||
return (union vec) { .sse.i = _mm_cmplt_epi32(
|
||||
_mm_sub_epi32(a.sse.i, bias),
|
||||
_mm_sub_epi32(b.sse.i, bias)) };
|
||||
}
|
||||
#endif
|
||||
|
||||
// Return float version of a <= b:
|
||||
#ifndef VEC_FN_LE
|
||||
#define VEC_FN_LE
|
||||
static inline union vec
|
||||
vec_le (const union vec a, const union vec b)
|
||||
{
|
||||
return (union vec) { .sse.f = _mm_cmple_ps(a.sse.f, b.sse.f) };
|
||||
}
|
||||
#endif
|
||||
|
||||
// Return signed integer version of a <= b:
|
||||
#ifndef VEC_FN_ILE
|
||||
#define VEC_FN_ILE
|
||||
static inline union vec
|
||||
vec_ile (const union vec a, const union vec b)
|
||||
{
|
||||
return (union vec) { .sse.i = _mm_or_si128(
|
||||
_mm_cmplt_epi32(a.sse.i, b.sse.i),
|
||||
_mm_cmpeq_epi32(a.sse.i, b.sse.i)) };
|
||||
}
|
||||
#endif
|
||||
|
||||
// Return unsigned integer version of a <= b:
|
||||
#ifndef VEC_FN_ULE
|
||||
#define VEC_FN_ULE
|
||||
static inline union vec
|
||||
vec_ule (const union vec a, const union vec b)
|
||||
{
|
||||
return vec_or(vec_ult(a, b), vec_ueq(a, b));
|
||||
}
|
||||
#endif
|
||||
|
||||
// Return float version of a > b:
|
||||
#ifndef VEC_FN_GT
|
||||
#define VEC_FN_GT
|
||||
static inline union vec
|
||||
vec_gt (const union vec a, const union vec b)
|
||||
{
|
||||
return (union vec) { .sse.f = _mm_cmpgt_ps(a.sse.f, b.sse.f) };
|
||||
}
|
||||
#endif
|
||||
|
||||
// Return signed integer version of a > b:
|
||||
#ifndef VEC_FN_IGT
|
||||
#define VEC_FN_IGT
|
||||
static inline union vec
|
||||
vec_igt (const union vec a, const union vec b)
|
||||
{
|
||||
return (union vec) { .sse.i = _mm_cmpgt_epi32(a.sse.i, b.sse.i) };
|
||||
}
|
||||
#endif
|
||||
|
||||
// Return unsigned integer version of a > b:
|
||||
#ifndef VEC_FN_UGT
|
||||
#define VEC_FN_UGT
|
||||
static inline union vec
|
||||
vec_ugt (const union vec a, const union vec b)
|
||||
{
|
||||
const __m128i bias = _mm_set1_epi32(INT32_C(0x80000000));
|
||||
|
||||
return (union vec) { .sse.i = _mm_cmpgt_epi32(
|
||||
_mm_sub_epi32(a.sse.i, bias),
|
||||
_mm_sub_epi32(b.sse.i, bias)) };
|
||||
}
|
||||
#endif
|
||||
|
||||
// Return float version of a >= b:
|
||||
#ifndef VEC_FN_GE
|
||||
#define VEC_FN_GE
|
||||
static inline union vec
|
||||
vec_ge (const union vec a, const union vec b)
|
||||
{
|
||||
return (union vec) { .sse.f = _mm_cmpge_ps(a.sse.f, b.sse.f) };
|
||||
}
|
||||
#endif
|
||||
|
||||
// Return signed integer version of a >= b:
|
||||
#ifndef VEC_FN_IGE
|
||||
#define VEC_FN_IGE
|
||||
static inline union vec
|
||||
vec_ige (const union vec a, const union vec b)
|
||||
{
|
||||
return (union vec) { .sse.i = _mm_or_si128(
|
||||
_mm_cmpgt_epi32(a.sse.i, b.sse.i),
|
||||
_mm_cmpeq_epi32(a.sse.i, b.sse.i)) };
|
||||
}
|
||||
#endif
|
||||
|
||||
// Return unsigned integer version of a >= b:
|
||||
#ifndef VEC_FN_UGE
|
||||
#define VEC_FN_UGE
|
||||
static inline union vec
|
||||
vec_uge (const union vec a, const union vec b)
|
||||
{
|
||||
return vec_or(vec_ugt(a, b), vec_ueq(a, b));
|
||||
}
|
||||
#endif
|
||||
|
||||
// Return dot product of two 3D vectors:
|
||||
#ifndef VEC_FN_DOT
|
||||
#define VEC_FN_DOT
|
||||
static inline float
|
||||
vec_dot (const union vec a, const union vec b)
|
||||
{
|
||||
const __m128 mulx = _mm_mul_ps(a.sse.f, b.sse.f);
|
||||
const __m128 muly = _mm_shuffle_ps(mulx, mulx, _MM_SHUFFLE(0, 0, 0, 1));
|
||||
const __m128 mulz = _mm_shuffle_ps(mulx, mulx, _MM_SHUFFLE(0, 0, 0, 2));
|
||||
const __m128 sum = _mm_add_ps(mulx, _mm_add_ps(muly, mulz));
|
||||
|
||||
// Extract first element:
|
||||
return ((union vec) { .xi = _mm_cvtsi128_si32(_mm_castps_si128(sum)) }).x;
|
||||
}
|
||||
#endif
|
||||
|
||||
// Return cross product of two 3D vectors:
|
||||
#ifndef VEC_FN_CROSS
|
||||
#define VEC_FN_CROSS
|
||||
static inline union vec
|
||||
vec_cross (const union vec a, const union vec b)
|
||||
{
|
||||
// Shuffle a:
|
||||
const __m128 ayzx = _mm_shuffle_ps(a.sse.f, a.sse.f, _MM_SHUFFLE(0, 0, 2, 1));
|
||||
const __m128 azxy = _mm_shuffle_ps(a.sse.f, a.sse.f, _MM_SHUFFLE(0, 1, 0, 2));
|
||||
|
||||
// Shuffle b:
|
||||
const __m128 byzx = _mm_shuffle_ps(b.sse.f, b.sse.f, _MM_SHUFFLE(0, 0, 2, 1));
|
||||
const __m128 bzxy = _mm_shuffle_ps(b.sse.f, b.sse.f, _MM_SHUFFLE(0, 1, 0, 2));
|
||||
|
||||
// (ay * bz), (az * bx), (ax * by), (ax * bx):
|
||||
const __m128 mul1 = _mm_mul_ps(ayzx, bzxy);
|
||||
|
||||
// (az * by), (ax * bz), (ay * bx), (ax * bx):
|
||||
const __m128 mul2 = _mm_mul_ps(azxy, byzx);
|
||||
|
||||
return (union vec) { .sse.f = _mm_sub_ps(mul1, mul2) };
|
||||
}
|
||||
#endif
|
||||
@@ -0,0 +1,12 @@
|
||||
// Return dot product of two 3D vectors:
|
||||
#ifndef VEC_FN_DOT
|
||||
#define VEC_FN_DOT
|
||||
static inline float
|
||||
vec_dot (const union vec a, const union vec b)
|
||||
{
|
||||
const __m128 dot = _mm_dp_ps(a.sse.f, b.sse.f, 0x7F);
|
||||
|
||||
// Extract first element:
|
||||
return ((union vec) { .xi = _mm_cvtsi128_si32(_mm_castps_si128(dot)) }).x;
|
||||
}
|
||||
#endif
|
||||
@@ -0,0 +1,146 @@
|
||||
#pragma once
|
||||
|
||||
#include <stdint.h>
|
||||
|
||||
// GCC-style vector extensions work with Clang >= 3 or GCC >= 4.0:
|
||||
#if !defined(VEC_SUPPRESS_GCC)
|
||||
#if __GNUC__ >= 4 || __clang_major__ >= 3
|
||||
#define VEC_GCC
|
||||
#endif
|
||||
#endif
|
||||
|
||||
// For testing purposes, allow the user to bypass any platform-specific support
|
||||
// and to use only the generic functions:
|
||||
#if !defined(VEC_SUPPRESS_HW)
|
||||
|
||||
// Check for SSE support:
|
||||
#if defined(__SSE2__)
|
||||
#define VEC_SSE2
|
||||
#include <emmintrin.h>
|
||||
#endif
|
||||
|
||||
// Check for SSE4 support:
|
||||
#if defined(__SSE4_1__)
|
||||
#define VEC_SSE41
|
||||
#include <nmmintrin.h>
|
||||
#endif
|
||||
|
||||
// Check for ARM NEON support:
|
||||
#if defined(__ARM_NEON__) || defined(__ARM_NEON)
|
||||
#define VEC_NEON
|
||||
#include <arm_neon.h>
|
||||
|
||||
// GCC and Clang support inline assembly:
|
||||
#if defined(__GNUC__) || defined(__clang_major__)
|
||||
#define VEC_NEON_ASM
|
||||
#endif
|
||||
#endif
|
||||
#endif
|
||||
|
||||
// Define architecture-independent vector types.
|
||||
// This syntax is valid for GCC and LLVM/Clang:
|
||||
#if defined(VEC_GCC)
|
||||
typedef float vec4f __attribute__ ((vector_size (16), aligned (16)));
|
||||
typedef int32_t vec4i __attribute__ ((vector_size (16), aligned (16)));
|
||||
typedef uint32_t vec4u __attribute__ ((vector_size (16), aligned (16)));
|
||||
#endif
|
||||
|
||||
// Union to represent a 4x32-bit (128-bit) vector in various ways:
|
||||
union vec {
|
||||
|
||||
// Named member representation in float form, assuming a 3D vector:
|
||||
struct {
|
||||
float x;
|
||||
float y;
|
||||
float z;
|
||||
float w;
|
||||
};
|
||||
|
||||
// Named member representation in signed integer form:
|
||||
struct {
|
||||
int32_t xi;
|
||||
int32_t yi;
|
||||
int32_t zi;
|
||||
int32_t wi;
|
||||
};
|
||||
|
||||
// Named member representation in unsigned integer form:
|
||||
struct {
|
||||
uint32_t xu;
|
||||
uint32_t yu;
|
||||
uint32_t zu;
|
||||
uint32_t wu;
|
||||
};
|
||||
|
||||
// Representation as array of separate elements:
|
||||
union {
|
||||
float f[4];
|
||||
int32_t i[4];
|
||||
uint32_t u[4];
|
||||
} elem;
|
||||
|
||||
// Representation as GCC-style vector extensions:
|
||||
#if defined(VEC_GCC)
|
||||
union {
|
||||
vec4f f;
|
||||
vec4i i;
|
||||
vec4u u;
|
||||
} gcc;
|
||||
#endif
|
||||
|
||||
// SSE representation:
|
||||
#if defined(VEC_SSE2)
|
||||
union {
|
||||
__m128 f;
|
||||
__m128i i;
|
||||
__m128i u;
|
||||
} sse;
|
||||
#endif
|
||||
|
||||
// NEON representation:
|
||||
#if defined(VEC_NEON)
|
||||
union {
|
||||
float32x4_t f;
|
||||
int32x4_t i;
|
||||
uint32x4_t u;
|
||||
} neon;
|
||||
#endif
|
||||
};
|
||||
|
||||
// Compile-time constant instantiation macros:
|
||||
#define VEC(a, b, c, d) \
|
||||
((union vec) { .elem.f = { (a), (b), (c), (d) } })
|
||||
|
||||
#define VEC_I(a, b, c, d) \
|
||||
((union vec) { .elem.i = { (a), (b), (c), (d) } })
|
||||
|
||||
#define VEC_U(a, b, c, d) \
|
||||
((union vec) { .elem.u = { (a), (b), (c), (d) } })
|
||||
|
||||
// Include platform-specific function definitions. The most "powerful"
|
||||
// definitions come first and get a chance to declare functions. What they
|
||||
// cannot define will eventually be defined by a "less powerful" file. The
|
||||
// generic file acts as a catch-all in case none of the more powerful methods
|
||||
// are available.
|
||||
|
||||
#if defined(VEC_SSE41)
|
||||
#include "private/sse41.h"
|
||||
#endif
|
||||
|
||||
#if defined(VEC_SSE2)
|
||||
#include "private/sse2.h"
|
||||
#endif
|
||||
|
||||
#if defined(VEC_NEON_ASM)
|
||||
#include "private/neon_asm.h"
|
||||
#endif
|
||||
|
||||
#if defined(VEC_NEON)
|
||||
#include "private/neon_intrin.h"
|
||||
#endif
|
||||
|
||||
#if defined(VEC_GCC)
|
||||
#include "private/gcc.h"
|
||||
#endif
|
||||
|
||||
#include "private/generic.h"
|
||||
Reference in new issue
Block a user