File: fmod.cl

package info (click to toggle)
intel-graphics-compiler2 2.24.13-1
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 113,504 kB
  • sloc: cpp: 812,849; lisp: 288,219; ansic: 102,423; python: 4,010; yacc: 2,588; lex: 1,666; pascal: 318; sh: 162; makefile: 38
file content (292 lines) | stat: -rw-r--r-- 7,621 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
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
/*========================== begin_copyright_notice ============================

Copyright (C) 2017-2021 Intel Corporation

SPDX-License-Identifier: MIT

============================= end_copyright_notice ===========================*/

#include "../include/BiF_Definitions.cl"
#include "../../Headers/spirv.h"


INLINE
float __attribute__((overloadable)) __spirv_ocl_fast_fmod( float xx, float yy )
{
    float result = xx - yy * __spirv_ocl_trunc( xx / yy );
    return result;
}

INLINE
float2 __attribute__((overloadable)) __spirv_ocl_fast_fmod( float2 xx, float2 yy )
{
    float2 temp;
    temp.s0 = __spirv_ocl_fast_fmod(xx.s0, yy.s0);
    temp.s1 = __spirv_ocl_fast_fmod(xx.s1, yy.s1);
    return temp;
}

INLINE
float3 __attribute__((overloadable)) __spirv_ocl_fast_fmod( float3 xx, float3 yy )
{
    float3 temp;
    temp.s0 = __spirv_ocl_fast_fmod(xx.s0, yy.s0);
    temp.s1 = __spirv_ocl_fast_fmod(xx.s1, yy.s1);
    temp.s2 = __spirv_ocl_fast_fmod(xx.s2, yy.s2);
    return temp;
}

INLINE
float4 __attribute__((overloadable)) __spirv_ocl_fast_fmod( float4 xx, float4 yy )
{
    float4 temp;
    temp.s0 = __spirv_ocl_fast_fmod(xx.s0, yy.s0);
    temp.s1 = __spirv_ocl_fast_fmod(xx.s1, yy.s1);
    temp.s2 = __spirv_ocl_fast_fmod(xx.s2, yy.s2);
    temp.s3 = __spirv_ocl_fast_fmod(xx.s3, yy.s3);
    return temp;
}


#if defined(cl_khr_fp16)
INLINE
half __attribute__((overloadable)) __spirv_ocl_fast_fmod( half xx, half yy )
{
    return (half)__spirv_ocl_fast_fmod((float)xx, (float)yy);
}

INLINE
half2 __attribute__((overloadable)) __spirv_ocl_fast_fmod( half2 xx, half2 yy )
{
    half2 temp;
    temp.s0 = __spirv_ocl_fast_fmod(xx.s0, yy.s0);
    temp.s1 = __spirv_ocl_fast_fmod(xx.s1, yy.s1);
    return temp;
}

INLINE
half3 __attribute__((overloadable)) __spirv_ocl_fast_fmod( half3 xx, half3 yy )
{
    half3 temp;
    temp.s0 = __spirv_ocl_fast_fmod(xx.s0, yy.s0);
    temp.s1 = __spirv_ocl_fast_fmod(xx.s1, yy.s1);
    temp.s2 = __spirv_ocl_fast_fmod(xx.s2, yy.s2);
    return temp;
}

INLINE
half4 __attribute__((overloadable)) __spirv_ocl_fast_fmod( half4 xx, half4 yy )
{
    half4 temp;
    temp.s0 = __spirv_ocl_fast_fmod(xx.s0, yy.s0);
    temp.s1 = __spirv_ocl_fast_fmod(xx.s1, yy.s1);
    temp.s2 = __spirv_ocl_fast_fmod(xx.s2, yy.s2);
    temp.s3 = __spirv_ocl_fast_fmod(xx.s3, yy.s3);
    return temp;
}
#endif // cl_khr_fp16


#if defined(cl_khr_fp64)
INLINE
double __attribute__((overloadable)) __spirv_ocl_fast_fmod( double xx, double yy )
{
    return __spirv_ocl_fast_fmod(xx, yy);
}

INLINE
double2 __attribute__((overloadable)) __spirv_ocl_fast_fmod( double2 xx, double2 yy )
{
    double2 temp;
    temp.s0 = __spirv_ocl_fast_fmod(xx.s0, yy.s0);
    temp.s1 = __spirv_ocl_fast_fmod(xx.s1, yy.s1);
    return temp;
}

INLINE
double3 __attribute__((overloadable)) __spirv_ocl_fast_fmod( double3 xx, double3 yy )
{
    double3 temp;
    temp.s0 = __spirv_ocl_fast_fmod(xx.s0, yy.s0);
    temp.s1 = __spirv_ocl_fast_fmod(xx.s1, yy.s1);
    temp.s2 = __spirv_ocl_fast_fmod(xx.s2, yy.s2);
    return temp;
}

INLINE
double4 __attribute__((overloadable)) __spirv_ocl_fast_fmod( double4 xx, double4 yy )
{
    double4 temp;
    temp.s0 = __spirv_ocl_fast_fmod(xx.s0, yy.s0);
    temp.s1 = __spirv_ocl_fast_fmod(xx.s1, yy.s1);
    temp.s2 = __spirv_ocl_fast_fmod(xx.s2, yy.s2);
    temp.s3 = __spirv_ocl_fast_fmod(xx.s3, yy.s3);
    return temp;
}
#endif // cl_khr_fp64

static float __intel_fmod_f32_f32( float xx, float yy, bool doFast )
{
    float result;
    if( BIF_FLAG_CTRL_GET(FastRelaxedMath) && doFast )
    {
        return __spirv_ocl_fast_fmod(xx, yy);
    }

    if( __intel_relaxed_isnan(xx) |
        __intel_relaxed_isnan(yy) |
        __intel_relaxed_isinf(xx) |
        yy == 0.0f)
    {
        result = __spirv_ocl_nan(0);
    }
    else if( __intel_relaxed_isinf(yy) |
             (xx == 0.0f) )
    {
        result = xx;
    }
    else if( __spirv_ocl_fabs(xx) == __spirv_ocl_fabs(yy) )
    {
        result = __spirv_ocl_copysign(0.0f, xx);
    }
    else if (__spirv_ocl_fabs(xx) < __spirv_ocl_fabs(yy))
    {
        result = xx;
    }
    else
    {
        float x = __spirv_ocl_fabs(xx);
        float y = __spirv_ocl_fabs(yy);
        int ex = __spirv_ocl_ilogb( x );
        int ey = __spirv_ocl_ilogb( y );
        float xr = x;
        float yr = y;

        if(ex-ey >= 0)
        {
            yr = __spirv_ocl_ldexp( y, -ey );
            xr = __spirv_ocl_ldexp( x, -ex );

            for(int i = ex-ey; i > 0; i--)
            {
                float s = xr - yr;
                xr = ( xr >= yr ) ? s : xr;
                xr = xr + xr;
            }
            float s = xr - yr;
            xr = ( xr >= yr ) ? s : xr;

            xr = __spirv_ocl_ldexp(xr, ey);
        }

        float m = -xr;
        xr = ( xx < 0.0f ) ? m : xr;

        result = xr;
    }

    return result;
}

float __attribute__((overloadable)) __spirv_ocl_fmod( float xx, float yy )
{
    return __intel_fmod_f32_f32( xx, yy, true );
}

// OpFMod is the core version and is identical to OpenCL_fmod except
// it takes the sign from operand 2
INLINE float __builtin_spirv_OpFMod_f32_f32( float x, float y )
{
    return __spirv_ocl_copysign(__spirv_ocl_fmod(x, y), y);
}

GENERATE_SPIRV_OCL_VECTOR_FUNCTIONS_2ARGS_VV_LOOP( fmod, float, float, float, f32, f32 )
GENERATE_VECTOR_FUNCTIONS_2ARGS_VV_LOOP( __builtin_spirv_OpFMod, float, float, float, f32, f32 )

#if defined(cl_khr_fp64)

double __attribute__((overloadable)) __spirv_ocl_fmod( double xx, double yy )
{
    double result;

    if( __spirv_IsNan(xx) |
        __spirv_IsNan(yy) |
        __spirv_IsInf(xx) |
        yy == 0.0)
    {
        result = __spirv_ocl_nan(0);
    }
    else if( __spirv_IsInf(yy) |
             (xx == 0.0) )
    {
        result = xx;
    }
    else if( __spirv_ocl_fabs(xx) == __spirv_ocl_fabs(yy) )
    {
        result = __spirv_ocl_copysign(0.0, xx);
    }
    else if (__spirv_ocl_fabs(xx) < __spirv_ocl_fabs(yy))
    {
        result = xx;
    }
    else
    {
        double x = __spirv_ocl_fabs(xx);
        double y = __spirv_ocl_fabs(yy);
        int ex = __spirv_ocl_ilogb( x );
        int ey = __spirv_ocl_ilogb( y );
        double xr = x;
        double yr = y;

        if(ex-ey >= 0)
        {
            yr = __spirv_ocl_ldexp( y, -ey );
            xr = __spirv_ocl_ldexp( x, -ex );

            for(int i = ex-ey; i > 0; i--)
            {
                double s = xr - yr;
                xr = ( xr >= yr ) ? s : xr;
                xr = xr + xr;
            }
            double s = xr - yr;
            xr = ( xr >= yr ) ? s : xr;

            xr = __spirv_ocl_ldexp(xr, ey);
        }

        double m = -xr;
        xr = ( xx < 0.0 ) ? m : xr;

        result = xr;
    }

    return result;
}

INLINE double __builtin_spirv_OpFMod_f64_f64( double x, double y )
{
    return __spirv_ocl_copysign(__spirv_ocl_fmod(x, y), y);
}

GENERATE_SPIRV_OCL_VECTOR_FUNCTIONS_2ARGS_VV_LOOP( fmod, double, double, double, f64, f64 )
GENERATE_VECTOR_FUNCTIONS_2ARGS_VV_LOOP( __builtin_spirv_OpFMod, double, double, double, f64, f64 )

#endif // defined(cl_khr_fp64)

#if defined(cl_khr_fp16)

INLINE half __attribute__((overloadable)) __spirv_ocl_fmod( half x, half y )
{
    return __spirv_ocl_fmod((float)x, (float)y);
}

INLINE half __builtin_spirv_OpFMod_f16_f16( half x, half y )
{
    return __spirv_ocl_copysign(__spirv_ocl_fmod(x, y), y);
}

GENERATE_SPIRV_OCL_VECTOR_FUNCTIONS_2ARGS_VV_LOOP( fmod, half, half, half, f16, f16 )
GENERATE_VECTOR_FUNCTIONS_2ARGS_VV_LOOP( __builtin_spirv_OpFMod, half, half, half, f16, f16 )

#endif // defined(cl_khr_fp16)