Chris Dalton | c3cb099 | 2020-11-02 10:52:26 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2020 Google LLC. |
| 3 | * |
| 4 | * Use of this source code is governed by a BSD-style license that can be |
| 5 | * found in the LICENSE file. |
| 6 | */ |
| 7 | |
| 8 | #ifndef GrVx_DEFINED |
| 9 | #define GrVx_DEFINED |
| 10 | |
Chris Dalton | d461f6e | 2020-11-23 13:51:06 -0700 | [diff] [blame] | 11 | #include "include/core/SkTypes.h" |
Chris Dalton | c3cb099 | 2020-11-02 10:52:26 -0700 | [diff] [blame] | 12 | #include "include/private/SkVx.h" |
| 13 | |
| 14 | // grvx is Ganesh's addendum to skvx, Skia's SIMD library. Here we introduce functions that are |
| 15 | // approximate and/or have LSB differences from platform to platform (e.g., by using hardware FMAs |
| 16 | // when available). When a function is approximate, its error range is well documented and tested. |
| 17 | namespace grvx { |
| 18 | |
| 19 | // Allow floating point contraction. e.g., allow a*x + y to be compiled to a single FMA even though |
| 20 | // it introduces LSB differences on platforms that don't have an FMA instruction. |
| 21 | #if defined(__clang__) |
| 22 | #pragma STDC FP_CONTRACT ON |
| 23 | #endif |
| 24 | |
| 25 | // Use familiar type names and functions from SkSL and GLSL. |
| 26 | template<int N> using vec = skvx::Vec<N, float>; |
| 27 | using float2 = vec<2>; |
| 28 | using float4 = vec<4>; |
| 29 | |
| 30 | template<int N> using ivec = skvx::Vec<N, int32_t>; |
| 31 | using int2 = ivec<2>; |
| 32 | using int4 = ivec<4>; |
| 33 | |
| 34 | template<int N> using uvec = skvx::Vec<N, uint32_t>; |
| 35 | using uint2 = uvec<2>; |
| 36 | using uint4 = uvec<4>; |
| 37 | |
Chris Dalton | d461f6e | 2020-11-23 13:51:06 -0700 | [diff] [blame] | 38 | static SK_ALWAYS_INLINE float dot(float2 a, float2 b) { |
Chris Dalton | c3cb099 | 2020-11-02 10:52:26 -0700 | [diff] [blame] | 39 | float2 ab = a*b; |
| 40 | return ab[0] + ab[1]; |
| 41 | } |
| 42 | |
Chris Dalton | d461f6e | 2020-11-23 13:51:06 -0700 | [diff] [blame] | 43 | static SK_ALWAYS_INLINE float cross(float2 a, float2 b) { |
Chris Dalton | c3cb099 | 2020-11-02 10:52:26 -0700 | [diff] [blame] | 44 | float2 x = a*skvx::shuffle<1,0>(b); |
| 45 | return x[0] - x[1]; |
| 46 | } |
| 47 | |
Chris Dalton | c3cb099 | 2020-11-02 10:52:26 -0700 | [diff] [blame] | 48 | #if defined(__clang__) |
| 49 | #pragma STDC FP_CONTRACT DEFAULT |
| 50 | #endif |
| 51 | |
| 52 | }; // namespace grvx |
| 53 | |
| 54 | #endif |