blob: 0f5ddea2b028213c415956662f8f5bf712138663 [file] [log] [blame]
Aaron Watry88728002016-02-08 17:07:21 +00001/*
2 * Copyright (c) 2014 Advanced Micro Devices, Inc.
3 * Copyright (c) 2016 Aaron Watry
4 *
5 * Permission is hereby granted, free of charge, to any person obtaining a copy
6 * of this software and associated documentation files (the "Software"), to deal
7 * in the Software without restriction, including without limitation the rights
8 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9 * copies of the Software, and to permit persons to whom the Software is
10 * furnished to do so, subject to the following conditions:
11 *
12 * The above copyright notice and this permission notice shall be included in
13 * all copies or substantial portions of the Software.
14 *
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21 * THE SOFTWARE.
22 */
23#if __CLC_FPSIZE == 32
24#ifdef __CLC_SCALAR
25_CLC_OVERLOAD _CLC_DEF __CLC_GENTYPE frexp(float x, private int *ep) {
26 int i = as_int(x);
27 int ai = i & 0x7fffffff;
28 int d = ai > 0 & ai < 0x00800000;
29 // scale subnormal by 2^26 without multiplying
30 float s = as_float(ai | 0x0d800000) - 0x1.0p-100F;
31 ai = d ? as_int(s) : ai;
32 int e = (ai >> 23) - 126 - (d ? 26 : 0);
33 int t = ai == 0 | e == 129;
34 i = (i & 0x80000000) | 0x3f000000 | (ai & 0x007fffff);
35 *ep = t ? 0 : e;
36 return t ? x : as_float(i);
37}
38#define __CLC_FREXP_VEC(width) \
39_CLC_OVERLOAD _CLC_DEF float##width frexp(float##width x, private int##width *ep) { \
40 int##width i = as_int##width(x); \
41 int##width ai = i & 0x7fffffff; \
42 int##width d = ai > 0 & ai < 0x00800000; \
43 /* scale subnormal by 2^26 without multiplying */ \
44 float##width s = as_float##width(ai | 0x0d800000) - 0x1.0p-100F; \
45 ai = bitselect(ai, as_int##width(s), d); \
46 int##width e = (ai >> 23) - 126 - bitselect((int##width)0, (int##width)26, d); \
47 int##width t = ai == (int##width)0 | e == (int##width)129; \
48 i = (i & (int##width)0x80000000) | (int##width)0x3f000000 | (ai & 0x007fffff); \
49 *ep = bitselect(e, (int##width)0, t); \
50 return bitselect(as_float##width(i), x, as_float##width(t)); \
51}
52__CLC_FREXP_VEC(2)
53__CLC_FREXP_VEC(3)
54__CLC_FREXP_VEC(4)
55__CLC_FREXP_VEC(8)
56__CLC_FREXP_VEC(16)
57#undef __CLC_FREXP_VEC
58#endif
59#endif
60
61#if __CLC_FPSIZE == 64
62#ifdef __CLC_SCALAR
63_CLC_OVERLOAD _CLC_DEF __CLC_GENTYPE frexp(__CLC_GENTYPE x, private __CLC_INTN *ep) {
64 long i = as_long(x);
65 long ai = i & 0x7fffffffffffffffL;
66 int d = ai > 0 & ai < 0x0010000000000000L;
67 // scale subnormal by 2^54 without multiplying
68 double s = as_double(ai | 0x0370000000000000L) - 0x1.0p-968;
69 ai = d ? as_long(s) : ai;
70 int e = (int)(ai >> 52) - 1022 - (d ? 54 : 0);
71 int t = ai == 0 | e == 1025;
72 i = (i & 0x8000000000000000L) | 0x3fe0000000000000L | (ai & 0x000fffffffffffffL);
73 *ep = t ? 0 : e;
74 return t ? x : as_double(i);
75}
76#define __CLC_FREXP_VEC(width) \
77_CLC_OVERLOAD _CLC_DEF double##width frexp(double##width x, private int##width *ep) { \
78 long##width i = as_long##width(x); \
79 long##width ai = i & 0x7fffffffffffffffL; \
80 long##width d = ai > 0 & ai < 0x0010000000000000L; \
81 /* scale subnormal by 2^54 without multiplying */ \
82 double##width s = as_double##width(ai | 0x0370000000000000L) - 0x1.0p-968; \
83 ai = bitselect(ai, as_long##width(s), d); \
84 int##width e = convert_int##width(ai >> 52) - 1022 - bitselect((int##width)0, (int##width)54, convert_int##width(d)); \
85 int##width t = convert_int##width(ai == (long##width)0) | (e == (int##width)129); \
86 i = (i & (long##width)0x8000000000000000L) | (long##width)0x3fe0000000000000L | (ai & 0x000fffffffffffffL); \
87 *ep = bitselect(e, (int##width)0, t); \
88 return bitselect(as_double##width(i), x, as_double##width(convert_long##width(t))); \
89}
90__CLC_FREXP_VEC(2)
91__CLC_FREXP_VEC(3)
92__CLC_FREXP_VEC(4)
93__CLC_FREXP_VEC(8)
94__CLC_FREXP_VEC(16)
95#undef __CLC_FREXP_VEC
96#endif
97#endif
98
99#define __CLC_FREXP_DEF(addrspace) \
100 _CLC_OVERLOAD _CLC_DEF __CLC_GENTYPE frexp(__CLC_GENTYPE x, addrspace __CLC_INTN *iptr) { \
101 __CLC_INTN private_iptr; \
102 __CLC_GENTYPE ret = frexp(x, &private_iptr); \
103 *iptr = private_iptr; \
104 return ret; \
105}
106
107__CLC_FREXP_DEF(local);
108__CLC_FREXP_DEF(global);
109
110#undef __CLC_FREXP_DEF