1/* 2 * Copyright (c) 2014 Advanced Micro Devices, Inc. 3 * 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: 10 * 11 * The above copyright notice and this permission notice shall be included in 12 * all copies or substantial portions of the Software. 13 * 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. 21 */ 22 23#include <clc/clc.h> 24 25_CLC_OVERLOAD _CLC_DEF float length(float p) { 26 return fabs(p); 27} 28 29#define V_FLENGTH(p) \ 30 float l2 = dot(p, p); \ 31 \ 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 } \ 39 \ 40 return sqrt(l2); 41 42_CLC_OVERLOAD _CLC_DEF float length(float2 p) { 43 V_FLENGTH(p); 44} 45 46_CLC_OVERLOAD _CLC_DEF float length(float3 p) { 47 V_FLENGTH(p); 48} 49 50_CLC_OVERLOAD _CLC_DEF float length(float4 p) { 51 V_FLENGTH(p); 52} 53 54#ifdef cl_khr_fp64 55#pragma OPENCL EXTENSION cl_khr_fp64 : enable 56 57_CLC_OVERLOAD _CLC_DEF double length(double p){ 58 return fabs(p); 59} 60 61#define V_DLENGTH(p) \ 62 double l2 = dot(p, p); \ 63 \ 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 } \ 71 \ 72 return sqrt(l2); 73 74_CLC_OVERLOAD _CLC_DEF double length(double2 p) { 75 V_DLENGTH(p); 76} 77 78_CLC_OVERLOAD _CLC_DEF double length(double3 p) { 79 V_DLENGTH(p); 80} 81 82_CLC_OVERLOAD _CLC_DEF double length(double4 p) { 83 V_DLENGTH(p); 84} 85 86#endif 87 88#ifdef cl_khr_fp16 89#pragma OPENCL EXTENSION cl_khr_fp16 : enable 90 91_CLC_OVERLOAD _CLC_DEF half length(half p){ 92 return fabs(p); 93} 94 95// Only available in CLC1.2 96#ifndef HALF_MIN 97#define HALF_MIN 0x1.0p-14h 98#endif 99 100#define V_HLENGTH(p) \ 101 half l2 = dot(p, p); \ 102 \ 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; \ 109 } \ 110 \ 111 return sqrt(l2); 112 113_CLC_OVERLOAD _CLC_DEF half length(half2 p) { 114 V_HLENGTH(p); 115} 116 117_CLC_OVERLOAD _CLC_DEF half length(half3 p) { 118 V_HLENGTH(p); 119} 120 121_CLC_OVERLOAD _CLC_DEF half length(half4 p) { 122 V_HLENGTH(p); 123} 124 125#endif 126