[TTI][RISCV] Remove deduplicate type-based VP costing of VPReduction.NFC (#117708)
[llvm-project.git] / libclc / generic / lib / geometric / length.cl
blob170ec584fcfa482b80e7bf5b3642572bec7c8c07
1 /*
2 * Copyright (c) 2014 Advanced Micro Devices, Inc.
4 * Permission is hereby granted, free of charge, to any person obtaining a copy
5 * of this software and associated documentation files (the "Software"), to deal
6 * in the Software without restriction, including without limitation the rights
7 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8 * copies of the Software, and to permit persons to whom the Software is
9 * furnished to do so, subject to the following conditions:
11 * The above copyright notice and this permission notice shall be included in
12 * all copies or substantial portions of the Software.
14 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
20 * THE SOFTWARE.
23 #include <clc/clc.h>
25 _CLC_OVERLOAD _CLC_DEF float length(float p) {
26 return fabs(p);
29 #define V_FLENGTH(p) \
30 float l2 = dot(p, p); \
32 if (l2 < FLT_MIN) { \
33 p *= 0x1.0p+86F; \
34 return sqrt(dot(p, p)) * 0x1.0p-86F; \
35 } else if (l2 == INFINITY) { \
36 p *= 0x1.0p-65F; \
37 return sqrt(dot(p, p)) * 0x1.0p+65F; \
38 } \
40 return sqrt(l2);
42 _CLC_OVERLOAD _CLC_DEF float length(float2 p) {
43 V_FLENGTH(p);
46 _CLC_OVERLOAD _CLC_DEF float length(float3 p) {
47 V_FLENGTH(p);
50 _CLC_OVERLOAD _CLC_DEF float length(float4 p) {
51 V_FLENGTH(p);
54 #ifdef cl_khr_fp64
55 #pragma OPENCL EXTENSION cl_khr_fp64 : enable
57 _CLC_OVERLOAD _CLC_DEF double length(double p){
58 return fabs(p);
61 #define V_DLENGTH(p) \
62 double l2 = dot(p, p); \
64 if (l2 < DBL_MIN) { \
65 p *= 0x1.0p+563; \
66 return sqrt(dot(p, p)) * 0x1.0p-563; \
67 } else if (l2 == INFINITY) { \
68 p *= 0x1.0p-513; \
69 return sqrt(dot(p, p)) * 0x1.0p+513; \
70 } \
72 return sqrt(l2);
74 _CLC_OVERLOAD _CLC_DEF double length(double2 p) {
75 V_DLENGTH(p);
78 _CLC_OVERLOAD _CLC_DEF double length(double3 p) {
79 V_DLENGTH(p);
82 _CLC_OVERLOAD _CLC_DEF double length(double4 p) {
83 V_DLENGTH(p);
86 #endif
88 #ifdef cl_khr_fp16
89 #pragma OPENCL EXTENSION cl_khr_fp16 : enable
91 _CLC_OVERLOAD _CLC_DEF half length(half p){
92 return fabs(p);
95 // Only available in CLC1.2
96 #ifndef HALF_MIN
97 #define HALF_MIN 0x1.0p-14h
98 #endif
100 #define V_HLENGTH(p) \
101 half l2 = dot(p, p); \
103 if (l2 < HALF_MIN) { \
104 p *= 0x1.0p+12h; \
105 return sqrt(dot(p, p)) * 0x1.0p-12h; \
106 } else if (l2 == INFINITY) { \
107 p *= 0x1.0p-7h; \
108 return sqrt(dot(p, p)) * 0x1.0p+7h; \
111 return sqrt(l2);
113 _CLC_OVERLOAD _CLC_DEF half length(half2 p) {
114 V_HLENGTH(p);
117 _CLC_OVERLOAD _CLC_DEF half length(half3 p) {
118 V_HLENGTH(p);
121 _CLC_OVERLOAD _CLC_DEF half length(half4 p) {
122 V_HLENGTH(p);
125 #endif