[clang][modules] Don't prevent translation of FW_Private includes when explicitly...
[llvm-project.git] / libclc / generic / lib / geometric / normalize.cl
blobf61ac9444b001c38514e7b2df7476651202c0f63
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 normalize(float p) {
26 return sign(p);
29 _CLC_OVERLOAD _CLC_DEF float2 normalize(float2 p) {
30 if (all(p == (float2)0.0F))
31 return p;
33 float l2 = dot(p, p);
35 if (l2 < FLT_MIN) {
36 p *= 0x1.0p+86F;
37 l2 = dot(p, p);
38 } else if (l2 == INFINITY) {
39 p *= 0x1.0p-65f;
40 l2 = dot(p, p);
41 if (l2 == INFINITY) {
42 p = copysign(select((float2)0.0F, (float2)1.0F, isinf(p)), p);
43 l2 = dot(p, p);
46 return p * rsqrt(l2);
49 _CLC_OVERLOAD _CLC_DEF float3 normalize(float3 p) {
50 if (all(p == (float3)0.0F))
51 return p;
53 float l2 = dot(p, p);
55 if (l2 < FLT_MIN) {
56 p *= 0x1.0p+86F;
57 l2 = dot(p, p);
58 } else if (l2 == INFINITY) {
59 p *= 0x1.0p-66f;
60 l2 = dot(p, p);
61 if (l2 == INFINITY) {
62 p = copysign(select((float3)0.0F, (float3)1.0F, isinf(p)), p);
63 l2 = dot(p, p);
66 return p * rsqrt(l2);
69 _CLC_OVERLOAD _CLC_DEF float4 normalize(float4 p) {
70 if (all(p == (float4)0.0F))
71 return p;
73 float l2 = dot(p, p);
75 if (l2 < FLT_MIN) {
76 p *= 0x1.0p+86F;
77 l2 = dot(p, p);
78 } else if (l2 == INFINITY) {
79 p *= 0x1.0p-66f;
80 l2 = dot(p, p);
81 if (l2 == INFINITY) {
82 p = copysign(select((float4)0.0F, (float4)1.0F, isinf(p)), p);
83 l2 = dot(p, p);
86 return p * rsqrt(l2);
89 #ifdef cl_khr_fp64
91 #pragma OPENCL EXTENSION cl_khr_fp64 : enable
93 _CLC_OVERLOAD _CLC_DEF double normalize(double p) {
94 return sign(p);
97 _CLC_OVERLOAD _CLC_DEF double2 normalize(double2 p) {
98 if (all(p == (double2)0.0))
99 return p;
101 double l2 = dot(p, p);
103 if (l2 < DBL_MIN) {
104 p *= 0x1.0p+563;
105 l2 = dot(p, p);
106 } else if (l2 == INFINITY) {
107 p *= 0x1.0p-513;
108 l2 = dot(p, p);
109 if (l2 == INFINITY) {
110 p = copysign(select((double2)0.0, (double2)1.0, isinf(p)), p);
111 l2 = dot(p, p);
114 return p * rsqrt(l2);
117 _CLC_OVERLOAD _CLC_DEF double3 normalize(double3 p) {
118 if (all(p == (double3)0.0))
119 return p;
121 double l2 = dot(p, p);
123 if (l2 < DBL_MIN) {
124 p *= 0x1.0p+563;
125 l2 = dot(p, p);
126 } else if (l2 == INFINITY) {
127 p *= 0x1.0p-514;
128 l2 = dot(p, p);
129 if (l2 == INFINITY) {
130 p = copysign(select((double3)0.0, (double3)1.0, isinf(p)), p);
131 l2 = dot(p, p);
134 return p * rsqrt(l2);
137 _CLC_OVERLOAD _CLC_DEF double4 normalize(double4 p) {
138 if (all(p == (double4)0.0))
139 return p;
141 double l2 = dot(p, p);
143 if (l2 < DBL_MIN) {
144 p *= 0x1.0p+563;
145 l2 = dot(p, p);
146 } else if (l2 == INFINITY) {
147 p *= 0x1.0p-514;
148 l2 = dot(p, p);
149 if (l2 == INFINITY) {
150 p = copysign(select((double4)0.0, (double4)1.0, isinf(p)), p);
151 l2 = dot(p, p);
154 return p * rsqrt(l2);
157 #endif