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#include "config.h" 25#include "../clcmacro.h" 26#include "math.h" 27 28_CLC_DEF _CLC_OVERLOAD float __clc_ldexp(float x, int n) { 29 30 if (!__clc_fp32_subnormals_supported()) { 31 32 // This treats subnormals as zeros 33 int i = as_int(x); 34 int e = (i >> 23) & 0xff; 35 int m = i & 0x007fffff; 36 int s = i & 0x80000000; 37 int v = add_sat(e, n); 38 v = clamp(v, 0, 0xff); 39 int mr = e == 0 | v == 0 | v == 0xff ? 0 : m; 40 int c = e == 0xff; 41 mr = c ? m : mr; 42 int er = c ? e : v; 43 er = e ? er : e; 44 return as_float( s | (er << 23) | mr ); 45 } 46 47 /* supports denormal values */ 48 const int multiplier = 24; 49 float val_f; 50 uint val_ui; 51 uint sign; 52 int exponent; 53 val_ui = as_uint(x); 54 sign = val_ui & 0x80000000; 55 val_ui = val_ui & 0x7fffffff;/* remove the sign bit */ 56 int val_x = val_ui; 57 58 exponent = val_ui >> 23; /* get the exponent */ 59 int dexp = exponent; 60 61 /* denormal support */ 62 int fbh = 127 - (as_uint((float)(as_float(val_ui | 0x3f800000) - 1.0f)) >> 23); 63 int dexponent = 25 - fbh; 64 uint dval_ui = (( (val_ui << fbh) & 0x007fffff) | (dexponent << 23)); 65 int ex = dexponent + n - multiplier; 66 dexponent = ex; 67 uint val = sign | (ex << 23) | (dval_ui & 0x007fffff); 68 int ex1 = dexponent + multiplier; 69 ex1 = -ex1 +25; 70 dval_ui = (((dval_ui & 0x007fffff )| 0x800000) >> ex1); 71 dval_ui = dexponent > 0 ? val :dval_ui; 72 dval_ui = dexponent > 254 ? 0x7f800000 :dval_ui; /*overflow*/ 73 dval_ui = dexponent < -multiplier ? 0 : dval_ui; /*underflow*/ 74 dval_ui = dval_ui | sign; 75 val_f = as_float(dval_ui); 76 77 exponent += n; 78 79 val = sign | (exponent << 23) | (val_ui & 0x007fffff); 80 ex1 = exponent + multiplier; 81 ex1 = -ex1 +25; 82 val_ui = (((val_ui & 0x007fffff )| 0x800000) >> ex1); 83 val_ui = exponent > 0 ? val :val_ui; 84 val_ui = exponent > 254 ? 0x7f800000 :val_ui; /*overflow*/ 85 val_ui = exponent < -multiplier ? 0 : val_ui; /*underflow*/ 86 val_ui = val_ui | sign; 87 88 val_ui = dexp == 0? dval_ui : val_ui; 89 val_f = as_float(val_ui); 90 91 val_f = isnan(x) | isinf(x) | val_x == 0 ? x : val_f; 92 return val_f; 93} 94 95#ifdef cl_khr_fp64 96 97#pragma OPENCL EXTENSION cl_khr_fp64 : enable 98 99_CLC_DEF _CLC_OVERLOAD double __clc_ldexp(double x, int n) { 100 long l = as_ulong(x); 101 int e = (l >> 52) & 0x7ff; 102 long s = l & 0x8000000000000000; 103 104 ulong ux = as_ulong(x * 0x1.0p+53); 105 int de = ((int)(ux >> 52) & 0x7ff) - 53; 106 int c = e == 0; 107 e = c ? de: e; 108 109 ux = c ? ux : l; 110 111 int v = e + n; 112 v = clamp(v, -0x7ff, 0x7ff); 113 114 ux &= ~EXPBITS_DP64; 115 116 double mr = as_double(ux | ((ulong)(v+53) << 52)); 117 mr = mr * 0x1.0p-53; 118 119 mr = v > 0 ? as_double(ux | ((ulong)v << 52)) : mr; 120 121 mr = v == 0x7ff ? as_double(s | PINFBITPATT_DP64) : mr; 122 mr = v < -53 ? as_double(s) : mr; 123 124 mr = ((n == 0) | isinf(x) | (x == 0) ) ? x : mr; 125 return mr; 126} 127 128#endif 129