Skip to content

Commit d7d84d3

Browse files
authored
Merge pull request #20 from antoyo/fix-bswap128
Fix bswap for 128-bit integers
2 parents e10dd0a + 07d916d commit d7d84d3

File tree

3 files changed

+61
-32
lines changed

3 files changed

+61
-32
lines changed

gcc-patches/0004-Add-support-for-sized-integer-types-including-128-bi.patch

Lines changed: 36 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
From 05d931244a575f3d03d8c7ff409d539e0cc6d154 Mon Sep 17 00:00:00 2001
1+
From f41a659255180e29f57f48d6fe38a552f4dda0a2 Mon Sep 17 00:00:00 2001
22
From: Antoni Boucher <[email protected]>
33
Date: Mon, 10 May 2021 19:43:02 -0400
44
Subject: [PATCH] Add support for sized integer types, including 128-bit
@@ -13,6 +13,7 @@ Subject: [PATCH] Add support for sized integer types, including 128-bit
1313
GCC_JIT_TYPE_UINT32_T, GCC_JIT_TYPE_UINT64_T,
1414
GCC_JIT_TYPE_UINT128_T, GCC_JIT_TYPE_INT8_T, GCC_JIT_TYPE_INT16_T,
1515
GCC_JIT_TYPE_INT32_T, GCC_JIT_TYPE_INT64_T, GCC_JIT_TYPE_INT128_T.
16+
* jit-builtins.c: Add support for BT_UINT128.
1617
* jit-playback.c: Add support for the sized integer types.
1718
* jit-recording.c: Add support for the sized integer types.
1819
* libgccjit.h (GCC_JIT_TYPE_UINT8_T, GCC_JIT_TYPE_UINT16_T,
@@ -25,11 +26,12 @@ Subject: [PATCH] Add support for sized integer types, including 128-bit
2526
* jit.dg/test-types.c: Add tests for sized integer types.
2627
---
2728
gcc/jit/docs/topics/types.rst | 10 +++
29+
gcc/jit/jit-builtins.c | 1 +
2830
gcc/jit/jit-playback.c | 21 ++++++
29-
gcc/jit/jit-recording.c | 73 ++++++++++++++++++++
31+
gcc/jit/jit-recording.c | 77 +++++++++++++++++++++
3032
gcc/jit/libgccjit.h | 11 +++
3133
gcc/testsuite/jit.dg/test-types.c | 111 ++++++++++++++++++++++++++++++
32-
5 files changed, 226 insertions(+)
34+
6 files changed, 231 insertions(+)
3335

3436
diff --git a/gcc/jit/docs/topics/types.rst b/gcc/jit/docs/topics/types.rst
3537
index 831f11b679a..68accacca45 100644
@@ -52,6 +54,18 @@ index 831f11b679a..68accacca45 100644
5254
:c:data:`GCC_JIT_TYPE_FLOAT`
5355
:c:data:`GCC_JIT_TYPE_DOUBLE`
5456
:c:data:`GCC_JIT_TYPE_LONG_DOUBLE`
57+
diff --git a/gcc/jit/jit-builtins.c b/gcc/jit/jit-builtins.c
58+
index 1ea96f4e025..27f3eec3e54 100644
59+
--- a/gcc/jit/jit-builtins.c
60+
+++ b/gcc/jit/jit-builtins.c
61+
@@ -483,6 +483,7 @@ builtins_manager::make_primitive_type (enum jit_builtin_type type_id)
62+
case BT_UINT16: return m_ctxt->get_int_type (2, false);
63+
case BT_UINT32: return m_ctxt->get_int_type (4, false);
64+
case BT_UINT64: return m_ctxt->get_int_type (8, false);
65+
+ case BT_UINT128: return m_ctxt->get_int_type (16, false);
66+
// case BT_WORD:
67+
// case BT_UNWINDWORD:
68+
case BT_FLOAT: return m_ctxt->get_type (GCC_JIT_TYPE_FLOAT);
5569
diff --git a/gcc/jit/jit-playback.c b/gcc/jit/jit-playback.c
5670
index c6136301243..40630aa1ab8 100644
5771
--- a/gcc/jit/jit-playback.c
@@ -85,10 +99,21 @@ index c6136301243..40630aa1ab8 100644
8599
return long_integer_type_node;
86100
case GCC_JIT_TYPE_UNSIGNED_LONG:
87101
diff --git a/gcc/jit/jit-recording.c b/gcc/jit/jit-recording.c
88-
index 117ff70114c..b67ae8bfb78 100644
102+
index 117ff70114c..3848e5da138 100644
89103
--- a/gcc/jit/jit-recording.c
90104
+++ b/gcc/jit/jit-recording.c
91-
@@ -2247,6 +2247,18 @@ recording::memento_of_get_type::get_size ()
105+
@@ -822,6 +822,10 @@ recording::context::get_int_type (int num_bytes, int is_signed)
106+
return get_type (is_signed
107+
? GCC_JIT_TYPE_LONG_LONG
108+
: GCC_JIT_TYPE_UNSIGNED_LONG_LONG);
109+
+ if (num_bits == 128)
110+
+ return get_type (is_signed
111+
+ ? GCC_JIT_TYPE_INT128_T
112+
+ : GCC_JIT_TYPE_UINT128_T);
113+
114+
/* Some other size, not corresponding to the C int types. */
115+
/* To be written: support arbitrary other sizes, sharing by
116+
@@ -2247,6 +2251,18 @@ recording::memento_of_get_type::get_size ()
92117
case GCC_JIT_TYPE_UNSIGNED_LONG_LONG:
93118
size = LONG_LONG_TYPE_SIZE;
94119
break;
@@ -107,7 +132,7 @@ index 117ff70114c..b67ae8bfb78 100644
107132
case GCC_JIT_TYPE_FLOAT:
108133
size = FLOAT_TYPE_SIZE;
109134
break;
110-
@@ -2295,6 +2307,16 @@ recording::memento_of_get_type::dereference ()
135+
@@ -2295,6 +2311,16 @@ recording::memento_of_get_type::dereference ()
111136
case GCC_JIT_TYPE_UNSIGNED_LONG:
112137
case GCC_JIT_TYPE_LONG_LONG:
113138
case GCC_JIT_TYPE_UNSIGNED_LONG_LONG:
@@ -124,7 +149,7 @@ index 117ff70114c..b67ae8bfb78 100644
124149
case GCC_JIT_TYPE_FLOAT:
125150
case GCC_JIT_TYPE_DOUBLE:
126151
case GCC_JIT_TYPE_LONG_DOUBLE:
127-
@@ -2347,6 +2369,16 @@ recording::memento_of_get_type::is_int () const
152+
@@ -2347,6 +2373,16 @@ recording::memento_of_get_type::is_int () const
128153
case GCC_JIT_TYPE_UNSIGNED_LONG:
129154
case GCC_JIT_TYPE_LONG_LONG:
130155
case GCC_JIT_TYPE_UNSIGNED_LONG_LONG:
@@ -141,7 +166,7 @@ index 117ff70114c..b67ae8bfb78 100644
141166
return true;
142167

143168
case GCC_JIT_TYPE_FLOAT:
144-
@@ -2400,6 +2432,16 @@ recording::memento_of_get_type::is_float () const
169+
@@ -2400,6 +2436,16 @@ recording::memento_of_get_type::is_float () const
145170
case GCC_JIT_TYPE_UNSIGNED_LONG:
146171
case GCC_JIT_TYPE_LONG_LONG:
147172
case GCC_JIT_TYPE_UNSIGNED_LONG_LONG:
@@ -158,7 +183,7 @@ index 117ff70114c..b67ae8bfb78 100644
158183
return false;
159184

160185
case GCC_JIT_TYPE_FLOAT:
161-
@@ -2453,6 +2495,16 @@ recording::memento_of_get_type::is_bool () const
186+
@@ -2453,6 +2499,16 @@ recording::memento_of_get_type::is_bool () const
162187
case GCC_JIT_TYPE_UNSIGNED_LONG:
163188
case GCC_JIT_TYPE_LONG_LONG:
164189
case GCC_JIT_TYPE_UNSIGNED_LONG_LONG:
@@ -175,7 +200,7 @@ index 117ff70114c..b67ae8bfb78 100644
175200
return false;
176201

177202
case GCC_JIT_TYPE_FLOAT:
178-
@@ -2511,6 +2563,17 @@ static const char * const get_type_strings[] = {
203+
@@ -2511,6 +2567,17 @@ static const char * const get_type_strings[] = {
179204
"long long", /* GCC_JIT_TYPE_LONG_LONG */
180205
"unsigned long long", /* GCC_JIT_TYPE_UNSIGNED_LONG_LONG */
181206

@@ -193,7 +218,7 @@ index 117ff70114c..b67ae8bfb78 100644
193218
"float", /* GCC_JIT_TYPE_FLOAT */
194219
"double", /* GCC_JIT_TYPE_DOUBLE */
195220
"long double", /* GCC_JIT_TYPE_LONG_DOUBLE */
196-
@@ -2551,6 +2614,16 @@ static const char * const get_type_enum_strings[] = {
221+
@@ -2551,6 +2618,16 @@ static const char * const get_type_enum_strings[] = {
197222
"GCC_JIT_TYPE_UNSIGNED_LONG",
198223
"GCC_JIT_TYPE_LONG_LONG",
199224
"GCC_JIT_TYPE_UNSIGNED_LONG_LONG",

gcc-test-backend/src/main.rs

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -204,4 +204,19 @@ fn main() {
204204

205205
const TRAILING_ZEROS: u32 = NonZeroU16::new(1 << 2).unwrap().trailing_zeros();
206206
assert_eq!(TRAILING_ZEROS, 2);
207+
208+
const A: u128 = 0b0101100;
209+
const B: u128 = 0b0100001;
210+
const C: u128 = 0b1111001;
211+
212+
const _0: u128 = 0;
213+
const _1: u128 = !0;
214+
215+
assert_eq!(u128::from_be(A.to_be()), A);
216+
assert_eq!(u128::from_be(B.to_be()), B);
217+
assert_eq!(u128::from_be(C.to_be()), C);
218+
assert_eq!(u128::from_be(_0), _0);
219+
assert_eq!(u128::from_be(_1), _1);
220+
assert_eq!(_0.to_be(), _0);
221+
assert_eq!(_1.to_be(), _1);
207222
}

src/intrinsic.rs

Lines changed: 10 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -271,28 +271,17 @@ impl<'a, 'gcc, 'tcx> IntrinsicCallMethods<'tcx> for Builder<'a, 'gcc, 'tcx> {
271271
args[0].immediate() // byte swap a u8/i8 is just a no-op
272272
}
273273
else {
274-
match width {
275-
128 => {
276-
// TODO: check if it's faster to use string literals and a
277-
// match instead of format!.
278-
// FIXME: support 128-bit ints.
279-
let bswap = self.cx.context.get_builtin_function("__builtin_bswap64");
280-
self.cx.context.new_call(None, bswap, &[self.intcast(args[0].immediate(), self.ulong_type, false)])
281-
},
282-
_ => {
283-
// TODO: check if it's faster to use string literals and a
284-
// match instead of format!.
285-
let bswap = self.cx.context.get_builtin_function(&format!("__builtin_bswap{}", width));
286-
let mut arg = args[0].immediate();
287-
// FIXME: this cast should not be necessary. Remove
288-
// when having proper sized integer types.
289-
let param_type = bswap.get_param(0).to_rvalue().get_type();
290-
if param_type != arg.get_type() {
291-
arg = self.bitcast(arg, param_type);
292-
}
293-
self.cx.context.new_call(None, bswap, &[arg])
294-
},
274+
// TODO: check if it's faster to use string literals and a
275+
// match instead of format!.
276+
let bswap = self.cx.context.get_builtin_function(&format!("__builtin_bswap{}", width));
277+
let mut arg = args[0].immediate();
278+
// FIXME: this cast should not be necessary. Remove
279+
// when having proper sized integer types.
280+
let param_type = bswap.get_param(0).to_rvalue().get_type();
281+
if param_type != arg.get_type() {
282+
arg = self.bitcast(arg, param_type);
295283
}
284+
self.cx.context.new_call(None, bswap, &[arg])
296285
}
297286
},
298287
sym::bitreverse => self.bit_reverse(width, args[0].immediate()),

0 commit comments

Comments
 (0)