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