blob: 1cfedf3d66503bee41a846f2d8eb0e7717fa647e [file] [log] [blame]
Chris Daltonc3cb0992020-11-02 10:52:26 -07001/*
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 Daltond461f6e2020-11-23 13:51:06 -070011#include "include/core/SkTypes.h"
Chris Daltonc3cb0992020-11-02 10:52:26 -070012#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.
17namespace 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.
26template<int N> using vec = skvx::Vec<N, float>;
27using float2 = vec<2>;
28using float4 = vec<4>;
29
30template<int N> using ivec = skvx::Vec<N, int32_t>;
31using int2 = ivec<2>;
32using int4 = ivec<4>;
33
34template<int N> using uvec = skvx::Vec<N, uint32_t>;
35using uint2 = uvec<2>;
36using uint4 = uvec<4>;
37
Chris Daltond461f6e2020-11-23 13:51:06 -070038static SK_ALWAYS_INLINE float dot(float2 a, float2 b) {
Chris Daltonc3cb0992020-11-02 10:52:26 -070039 float2 ab = a*b;
40 return ab[0] + ab[1];
41}
42
Chris Daltond461f6e2020-11-23 13:51:06 -070043static SK_ALWAYS_INLINE float cross(float2 a, float2 b) {
Chris Daltonc3cb0992020-11-02 10:52:26 -070044 float2 x = a*skvx::shuffle<1,0>(b);
45 return x[0] - x[1];
46}
47
Chris Daltonc3cb0992020-11-02 10:52:26 -070048#if defined(__clang__)
49 #pragma STDC FP_CONTRACT DEFAULT
50#endif
51
52}; // namespace grvx
53
54#endif