File: short_vec.isph

package info (click to toggle)
ispc 1.28.2-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 97,620 kB
  • sloc: cpp: 77,067; python: 8,303; yacc: 3,337; lex: 1,126; ansic: 631; sh: 475; makefile: 17
file content (183 lines) | stat: -rw-r--r-- 10,614 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
// -*- mode: c++ -*-
// Copyright (c) 2025, Intel Corporation
// SPDX-License-Identifier: BSD-3-Clause

// @file short_vec.isph
// @brief Portion of the ISPC standard library supporting short vector types.

// Unlike stdlib.isph which is included by default, this file is not implicitly
// included. The user must explicitly include it in their ISPC code to use the
// functions defined here. We do this to avoid polluting every single
// compilation with extra processing of quite a few template functions.

#pragma once

// Below the place for the real implementations of the functions using templates

// These are macro generators for template functions that extend element-wise
// standard library functions by adding support for short vectors. They take a
// function name as an argument and help reduce copy-paste. There are two
// versions of the macro: one for uniform and one for varying short vectors.
// The number of function arguments in the macros depends on their suffix,
// e.g., ARG2 indicates that the function takes two arguments.

#define SHORT_VEC_UNIFORM_ARG1(FUNC)                                                                                   \
    template <typename T, uint N> uniform T<N> FUNC(uniform T<N> a) {                                                  \
        uniform T<N> result;                                                                                           \
        foreach (i = 0 ... N) {                                                                                        \
            result[i] = FUNC(a[i]);                                                                                    \
        }                                                                                                              \
        return result;                                                                                                 \
    }

#define SHORT_VEC_VARYING_ARG1(FUNC)                                                                                   \
    template <typename T, uint N> varying T<N> FUNC(varying T<N> a) {                                                  \
        varying T<N> result;                                                                                           \
        for (uniform int i = 0; i < N; i++) {                                                                          \
            result[i] = FUNC(a[i]);                                                                                    \
        }                                                                                                              \
        return result;                                                                                                 \
    }

#define SHORT_VEC_UNIFORM_ARG1_BOOLRET(FUNC)                                                                           \
    template <typename T, uint N> uniform bool<N> FUNC(uniform T<N> a) {                                               \
        uniform bool<N> result;                                                                                        \
        foreach (i = 0 ... N) {                                                                                        \
            result[i] = FUNC(a[i]);                                                                                    \
        }                                                                                                              \
        return result;                                                                                                 \
    }

#define SHORT_VEC_VARYING_ARG1_BOOLRET(FUNC)                                                                           \
    template <typename T, uint N> varying bool<N> FUNC(varying T<N> a) {                                               \
        varying bool<N> result;                                                                                        \
        for (uniform int i = 0; i < N; i++) {                                                                          \
            result[i] = FUNC(a[i]);                                                                                    \
        }                                                                                                              \
        return result;                                                                                                 \
    }

#define SHORT_VEC_UNIFORM_ARG2(FUNC)                                                                                   \
    template <typename T, uint N> uniform T<N> FUNC(uniform T<N> a, uniform T<N> b) {                                  \
        uniform T<N> result;                                                                                           \
        foreach (i = 0 ... N) {                                                                                        \
            result[i] = FUNC(a[i], b[i]);                                                                              \
        }                                                                                                              \
        return result;                                                                                                 \
    }

#define SHORT_VEC_VARYING_ARG2(FUNC)                                                                                   \
    template <typename T, uint N> varying T<N> FUNC(varying T<N> a, varying T<N> b) {                                  \
        varying T<N> result;                                                                                           \
        for (uniform int i = 0; i < N; i++) {                                                                          \
            result[i] = FUNC(a[i], b[i]);                                                                              \
        }                                                                                                              \
        return result;                                                                                                 \
    }

#define SHORT_VEC_UNIFORM_ARG3(FUNC)                                                                                   \
    template <typename T, uint N> uniform T<N> FUNC(uniform T<N> a, uniform T<N> b, uniform T<N> c) {                  \
        uniform T<N> result;                                                                                           \
        foreach (i = 0 ... N) {                                                                                        \
            result[i] = FUNC(a[i], b[i], c[i]);                                                                        \
        }                                                                                                              \
        return result;                                                                                                 \
    }

#define SHORT_VEC_VARYING_ARG3(FUNC)                                                                                   \
    template <typename T, uint N> varying T<N> FUNC(varying T<N> a, varying T<N> b, varying T<N> c) {                  \
        varying T<N> result;                                                                                           \
        for (uniform int i = 0; i < N; i++) {                                                                          \
            result[i] = FUNC(a[i], b[i], c[i]);                                                                        \
        }                                                                                                              \
        return result;                                                                                                 \
    }

#define SHORT_VEC_ARG1(FUNC)                                                                                           \
    SHORT_VEC_UNIFORM_ARG1(FUNC)                                                                                       \
    SHORT_VEC_VARYING_ARG1(FUNC)

#define SHORT_VEC_ARG1_BOOLRET(FUNC)                                                                                   \
    SHORT_VEC_UNIFORM_ARG1_BOOLRET(FUNC)                                                                               \
    SHORT_VEC_VARYING_ARG1_BOOLRET(FUNC)

#define SHORT_VEC_ARG2(FUNC)                                                                                           \
    SHORT_VEC_UNIFORM_ARG2(FUNC)                                                                                       \
    SHORT_VEC_VARYING_ARG2(FUNC)

#define SHORT_VEC_ARG3(FUNC)                                                                                           \
    SHORT_VEC_UNIFORM_ARG3(FUNC)                                                                                       \
    SHORT_VEC_VARYING_ARG3(FUNC)

// Generate the template functions for short vectors

SHORT_VEC_ARG1(abs)
SHORT_VEC_ARG2(min)
SHORT_VEC_ARG2(max)

SHORT_VEC_ARG3(clamp)

SHORT_VEC_ARG1_BOOLRET(isnan)
SHORT_VEC_ARG1_BOOLRET(isinf)
SHORT_VEC_ARG1_BOOLRET(isfinite)

SHORT_VEC_ARG1(round)
SHORT_VEC_ARG1(floor)
SHORT_VEC_ARG1(ceil)
SHORT_VEC_ARG1(trunc)
SHORT_VEC_ARG1(rcp)
SHORT_VEC_ARG1(rcp_fast)

SHORT_VEC_ARG1(sqrt)
SHORT_VEC_ARG1(rsqrt)
SHORT_VEC_ARG1(rsqrt_fast)
SHORT_VEC_ARG1(sin)
SHORT_VEC_ARG1(asin)
SHORT_VEC_ARG1(cos)
SHORT_VEC_ARG1(acos)
SHORT_VEC_ARG1(tan)
SHORT_VEC_ARG1(atan)
SHORT_VEC_ARG1(exp)
SHORT_VEC_ARG1(log)
SHORT_VEC_ARG1(cbrt)

SHORT_VEC_ARG2(atan2)
SHORT_VEC_ARG2(pow)
SHORT_VEC_ARG2(fmod)

// Previous macro are not needed anymore

#undef SHORT_VEC_UNIFORM_ARG1
#undef SHORT_VEC_VARYING_ARG1
#undef SHORT_VEC_UNIFORM_ARG1_BOOLRET
#undef SHORT_VEC_VARYING_ARG1_BOOLRET
#undef SHORT_VEC_UNIFORM_ARG2
#undef SHORT_VEC_VARYING_ARG2
#undef SHORT_VEC_UNIFORM_ARG3
#undef SHORT_VEC_VARYING_ARG3
#undef SHORT_VEC_ARG1
#undef SHORT_VEC_ARG1_BOOLRET
#undef SHORT_VEC_ARG2
#undef SHORT_VEC_ARG3

// Short vector functions with a custom implementation

template <typename T, uint N> inline uniform T<N> select(uniform bool cond, uniform T<N> t, uniform T<N> f) {
    uniform T<N> result;

    foreach (i = 0 ... N) {
        result[i] = select(cond, t[i], f[i]);
    }

    return result;
}

template <typename T, uint N> inline uniform T<N> select(uniform bool<N> cond, uniform T<N> t, uniform T<N> f) {
    uniform T<N> result;

    foreach (i = 0 ... N) {
        result[i] = select(cond[i], t[i], f[i]);
    }

    return result;
}