2 * Copyright (c) 2014 Advanced Micro Devices, Inc.
3 * Copyright (c) 2016 Aaron Watry
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:
12 * The above copyright notice and this permission notice shall be included in
13 * all copies or substantial portions of the Software.
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
24 #include <clc/clcmacro.h>
26 #define __CLC_AS_GENTYPE __CLC_XCONCAT(as_, __CLC_GENTYPE)
27 #define __CLC_AS_INTN __CLC_XCONCAT(as_, __CLC_INTN)
29 #if __CLC_FPSIZE == 32
30 _CLC_OVERLOAD _CLC_DEF __CLC_GENTYPE frexp(__CLC_GENTYPE x, __CLC_ADDRESS_SPACE __CLC_INTN *ep) {
31 __CLC_INTN i = __CLC_AS_INTN(x);
32 __CLC_INTN ai = i & 0x7fffffff;
33 __CLC_INTN d = ai > 0 & ai < 0x00800000;
34 /* scale subnormal by 2^26 without multiplying */
35 __CLC_GENTYPE s = __CLC_AS_GENTYPE(ai | 0x0d800000) - 0x1.0p-100f;
36 ai = select(ai, __CLC_AS_INTN(s), d);
37 __CLC_INTN e = (ai >> 23) - 126 - select((__CLC_INTN)0, (__CLC_INTN)26, d);
38 __CLC_INTN t = ai == (__CLC_INTN)0 | e == (__CLC_INTN)129;
39 i = (i & (__CLC_INTN)0x80000000) | (__CLC_INTN)0x3f000000 | (ai & 0x007fffff);
40 *ep = select(e, (__CLC_INTN)0, t);
41 return select(__CLC_AS_GENTYPE(i), x, t);
45 #if __CLC_FPSIZE == 16
47 _CLC_OVERLOAD _CLC_DEF __CLC_GENTYPE frexp(__CLC_GENTYPE x,
48 __CLC_ADDRESS_SPACE __CLC_INTN *ep) {
49 return (__CLC_GENTYPE)frexp((float)x, ep);
51 _CLC_V_V_VP_VECTORIZE(_CLC_OVERLOAD _CLC_DEF, __CLC_GENTYPE, frexp,
52 __CLC_GENTYPE, __CLC_ADDRESS_SPACE, __CLC_INTN);
56 #if __CLC_FPSIZE == 64
58 #define __CLC_AS_LONGN as_long
59 #define __CLC_LONGN long
60 #define __CLC_CONVERT_INTN convert_int
62 #define __CLC_AS_LONGN __CLC_XCONCAT(as_long, __CLC_VECSIZE)
63 #define __CLC_LONGN __CLC_XCONCAT(long, __CLC_VECSIZE)
64 #define __CLC_CONVERT_INTN __CLC_XCONCAT(convert_int, __CLC_VECSIZE)
67 _CLC_OVERLOAD _CLC_DEF __CLC_GENTYPE frexp(__CLC_GENTYPE x, __CLC_ADDRESS_SPACE __CLC_INTN *ep) {
68 __CLC_LONGN i = __CLC_AS_LONGN(x);
69 __CLC_LONGN ai = i & 0x7fffffffffffffffL;
70 __CLC_LONGN d = ai > 0 & ai < 0x0010000000000000L;
71 // scale subnormal by 2^54 without multiplying
72 __CLC_GENTYPE s = __CLC_AS_GENTYPE(ai | 0x0370000000000000L) - 0x1.0p-968;
73 ai = select(ai, __CLC_AS_LONGN(s), d);
74 __CLC_LONGN e = (ai >> 52) - (__CLC_LONGN)1022 - select((__CLC_LONGN)0, (__CLC_LONGN)54, d);
75 __CLC_LONGN t = ai == 0 | e == 1025;
76 i = (i & (__CLC_LONGN)0x8000000000000000L) | (__CLC_LONGN)0x3fe0000000000000L | (ai & (__CLC_LONGN)0x000fffffffffffffL);
77 *ep = __CLC_CONVERT_INTN(select(e, 0L, t));
78 return select(__CLC_AS_GENTYPE(i), x, t);
83 #undef __CLC_CONVERT_INTN
86 #undef __CLC_AS_GENTYPE