1 /* bitstring.c -- Builtins for HSAIL bitstring instructions.
3 Copyright (C) 2015-2017 Free Software Foundation, Inc.
4 Contributed by Pekka Jaaskelainen <pekka.jaaskelainen@parmance.com>
5 for General Processor Tech.
7 Permission is hereby granted, free of charge, to any person obtaining a
8 copy of this software and associated documentation files
9 (the "Software"), to deal in the Software without restriction, including
10 without limitation the rights to use, copy, modify, merge, publish,
11 distribute, sublicense, and/or sell copies of the Software, and to
12 permit persons to whom the Software is furnished to do so, subject to
13 the following conditions:
15 The above copyright notice and this permission notice shall be included
16 in all copies or substantial portions of the Software.
18 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
19 OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
20 MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
21 IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
22 DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
23 OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
24 USE OR OTHER DEALINGS IN THE SOFTWARE.
30 #define BITEXTRACT(DEST_TYPE, SRC0, SRC1, SRC2) \
31 uint32_t offset = SRC1 & (sizeof (DEST_TYPE) * 8 - 1); \
32 uint32_t width = SRC2 & (sizeof (DEST_TYPE) * 8 - 1); \
36 return (SRC0 << (sizeof (DEST_TYPE) * 8 - width - offset)) \
37 >> (sizeof (DEST_TYPE) * 8 - width)
40 __hsail_bitextract_u32 (uint32_t src0
, uint32_t src1
, uint32_t src2
)
42 BITEXTRACT (uint32_t, src0
, src1
, src2
);
46 __hsail_bitextract_s32 (int32_t src0
, uint32_t src1
, uint32_t src2
)
48 BITEXTRACT (int32_t, src0
, src1
, src2
);
52 __hsail_bitextract_u64 (uint64_t src0
, uint32_t src1
, uint32_t src2
)
54 BITEXTRACT (uint64_t, src0
, src1
, src2
);
58 __hsail_bitextract_s64 (int64_t src0
, uint32_t src1
, uint32_t src2
)
60 BITEXTRACT (int64_t, src0
, src1
, src2
);
63 #define BITINSERT(DEST_TYPE, SRC0, SRC1, SRC2, SRC3) \
64 uint32_t offset = SRC2 & (sizeof (DEST_TYPE) * 8 - 1); \
65 uint32_t width = SRC3 & (sizeof (DEST_TYPE) * 8 - 1); \
66 DEST_TYPE mask = ((DEST_TYPE) 1 << width) - 1; \
67 return (SRC0 & ~(mask << offset)) | ((SRC1 & mask) << offset)
70 __hsail_bitinsert_u32 (uint32_t src0
, uint32_t src1
, uint32_t src2
,
73 BITINSERT (uint32_t, src0
, src1
, src2
, src3
);
77 __hsail_bitinsert_u64 (uint64_t src0
, uint64_t src1
, uint32_t src2
,
80 BITINSERT (uint64_t, src0
, src1
, src2
, src3
);
83 #define BITMASK(DEST_TYPE, SRC0, SRC1) \
84 uint32_t offset = SRC0 & (sizeof (DEST_TYPE) * 8 - 1); \
85 uint32_t width = SRC1 & (sizeof (DEST_TYPE) * 8 - 1); \
86 DEST_TYPE mask = ((DEST_TYPE) 1 << width) - 1; \
90 __hsail_bitmask_u32 (uint32_t src0
, uint32_t src1
)
92 BITMASK (uint32_t, src0
, src1
);
96 __hsail_bitmask_u64 (uint32_t src0
, uint32_t src1
)
98 BITMASK (uint64_t, src0
, src1
);
101 /* The dummy, but readable version from
102 http://graphics.stanford.edu/~seander/bithacks.html#BitReverseObvious
103 This (also) often maps to a single instruction in DSPs. */
105 #define BITREV(DEST_TYPE, SRC) \
108 int s = sizeof (SRC) * CHAR_BIT - 1; \
110 for (v >>= 1; v; v >>= 1) \
119 __hsail_bitrev_u32 (uint32_t src0
)
121 BITREV (uint32_t, src0
);
125 __hsail_bitrev_u64 (uint64_t src0
)
127 BITREV (uint64_t, src0
);
131 __hsail_bitselect_u32 (uint32_t src0
, uint32_t src1
, uint32_t src2
)
133 return (src1
& src0
) | (src2
& ~src0
);
137 __hsail_bitselect_u64 (uint64_t src0
, uint64_t src1
, uint64_t src2
)
139 return (src1
& src0
) | (src2
& ~src0
);
142 /* Due to the defined behavior with 0, we cannot use the gcc builtin
143 __builtin_clz* () directly. __builtin_ffs () has defined behavior, but
144 returns 0 while HSAIL requires to return -1. */
147 __hsail_firstbit_u32 (uint32_t src0
)
151 return __builtin_clz (src0
);
155 __hsail_firstbit_s32 (int32_t src0
)
157 uint32_t converted
= src0
>= 0 ? src0
: ~src0
;
158 return __hsail_firstbit_u32 (converted
);
162 __hsail_firstbit_u64 (uint64_t src0
)
166 return __builtin_clzl (src0
);
170 __hsail_firstbit_s64 (int64_t src0
)
172 uint64_t converted
= src0
>= 0 ? src0
: ~src0
;
173 return __hsail_firstbit_u64 (converted
);
177 __hsail_lastbit_u32 (uint32_t src0
)
181 return __builtin_ctz (src0
);
185 __hsail_lastbit_u64 (uint64_t src0
)
189 return __builtin_ctzl (src0
);