Skip to content

Commit b959822

Browse files
committed
include/qemu/int128: Use Int128 structure for TCI
We are about to allow passing Int128 to/from tcg helper functions, but libffi doesn't support __int128_t, so use the structure. In order for atomic128.h to continue working, we must provide a mechanism to frob between real __int128_t and the structure. Provide a new union, Int128Alias, for this. We cannot modify Int128 itself, as any changed alignment would also break libffi. Reviewed-by: Alex Bennée <[email protected]> Reviewed-by: Philippe Mathieu-Daudé <[email protected]> Signed-off-by: Richard Henderson <[email protected]>
1 parent c6556aa commit b959822

File tree

3 files changed

+87
-9
lines changed

3 files changed

+87
-9
lines changed

include/qemu/atomic128.h

Lines changed: 23 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -44,13 +44,23 @@
4444
#if defined(CONFIG_ATOMIC128)
4545
static inline Int128 atomic16_cmpxchg(Int128 *ptr, Int128 cmp, Int128 new)
4646
{
47-
return qatomic_cmpxchg__nocheck(ptr, cmp, new);
47+
Int128Alias r, c, n;
48+
49+
c.s = cmp;
50+
n.s = new;
51+
r.i = qatomic_cmpxchg__nocheck((__int128_t *)ptr, c.i, n.i);
52+
return r.s;
4853
}
4954
# define HAVE_CMPXCHG128 1
5055
#elif defined(CONFIG_CMPXCHG128)
5156
static inline Int128 atomic16_cmpxchg(Int128 *ptr, Int128 cmp, Int128 new)
5257
{
53-
return __sync_val_compare_and_swap_16(ptr, cmp, new);
58+
Int128Alias r, c, n;
59+
60+
c.s = cmp;
61+
n.s = new;
62+
r.i = __sync_val_compare_and_swap_16((__int128_t *)ptr, c.i, n.i);
63+
return r.s;
5464
}
5565
# define HAVE_CMPXCHG128 1
5666
#elif defined(__aarch64__)
@@ -89,12 +99,18 @@ Int128 QEMU_ERROR("unsupported atomic")
8999
#if defined(CONFIG_ATOMIC128)
90100
static inline Int128 atomic16_read(Int128 *ptr)
91101
{
92-
return qatomic_read__nocheck(ptr);
102+
Int128Alias r;
103+
104+
r.i = qatomic_read__nocheck((__int128_t *)ptr);
105+
return r.s;
93106
}
94107

95108
static inline void atomic16_set(Int128 *ptr, Int128 val)
96109
{
97-
qatomic_set__nocheck(ptr, val);
110+
Int128Alias v;
111+
112+
v.s = val;
113+
qatomic_set__nocheck((__int128_t *)ptr, v.i);
98114
}
99115

100116
# define HAVE_ATOMIC128 1
@@ -132,7 +148,8 @@ static inline void atomic16_set(Int128 *ptr, Int128 val)
132148
static inline Int128 atomic16_read(Int128 *ptr)
133149
{
134150
/* Maybe replace 0 with 0, returning the old value. */
135-
return atomic16_cmpxchg(ptr, 0, 0);
151+
Int128 z = int128_make64(0);
152+
return atomic16_cmpxchg(ptr, z, z);
136153
}
137154

138155
static inline void atomic16_set(Int128 *ptr, Int128 val)
@@ -141,7 +158,7 @@ static inline void atomic16_set(Int128 *ptr, Int128 val)
141158
do {
142159
cmp = old;
143160
old = atomic16_cmpxchg(ptr, cmp, val);
144-
} while (old != cmp);
161+
} while (int128_ne(old, cmp));
145162
}
146163

147164
# define HAVE_ATOMIC128 1

include/qemu/int128.h

Lines changed: 22 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,12 @@
33

44
#include "qemu/bswap.h"
55

6-
#ifdef CONFIG_INT128
6+
/*
7+
* With TCI, we need to use libffi for interfacing with TCG helpers.
8+
* But libffi does not support __int128_t, and therefore cannot pass
9+
* or return values of this type, force use of the Int128 struct.
10+
*/
11+
#if defined(CONFIG_INT128) && !defined(CONFIG_TCG_INTERPRETER)
712
typedef __int128_t Int128;
813

914
static inline Int128 int128_make64(uint64_t a)
@@ -460,8 +465,7 @@ Int128 int128_divu(Int128, Int128);
460465
Int128 int128_remu(Int128, Int128);
461466
Int128 int128_divs(Int128, Int128);
462467
Int128 int128_rems(Int128, Int128);
463-
464-
#endif /* CONFIG_INT128 */
468+
#endif /* CONFIG_INT128 && !CONFIG_TCG_INTERPRETER */
465469

466470
static inline void bswap128s(Int128 *s)
467471
{
@@ -472,4 +476,19 @@ static inline void bswap128s(Int128 *s)
472476
#define INT128_MAX int128_make128(UINT64_MAX, INT64_MAX)
473477
#define INT128_MIN int128_make128(0, INT64_MIN)
474478

479+
/*
480+
* When compiler supports a 128-bit type, define a combination of
481+
* a possible structure and the native types. Ease parameter passing
482+
* via use of the transparent union extension.
483+
*/
484+
#ifdef CONFIG_INT128
485+
typedef union {
486+
Int128 s;
487+
__int128_t i;
488+
__uint128_t u;
489+
} Int128Alias __attribute__((transparent_union));
490+
#else
491+
typedef Int128 Int128Alias;
492+
#endif /* CONFIG_INT128 */
493+
475494
#endif /* INT128_H */

util/int128.c

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -144,4 +144,46 @@ Int128 int128_rems(Int128 a, Int128 b)
144144
return r;
145145
}
146146

147+
#elif defined(CONFIG_TCG_INTERPRETER)
148+
149+
Int128 int128_divu(Int128 a_s, Int128 b_s)
150+
{
151+
Int128Alias r, a, b;
152+
153+
a.s = a_s;
154+
b.s = b_s;
155+
r.u = a.u / b.u;
156+
return r.s;
157+
}
158+
159+
Int128 int128_remu(Int128 a_s, Int128 b_s)
160+
{
161+
Int128Alias r, a, b;
162+
163+
a.s = a_s;
164+
b.s = b_s;
165+
r.u = a.u % b.u;
166+
return r.s;
167+
}
168+
169+
Int128 int128_divs(Int128 a_s, Int128 b_s)
170+
{
171+
Int128Alias r, a, b;
172+
173+
a.s = a_s;
174+
b.s = b_s;
175+
r.i = a.i / b.i;
176+
return r.s;
177+
}
178+
179+
Int128 int128_rems(Int128 a_s, Int128 b_s)
180+
{
181+
Int128Alias r, a, b;
182+
183+
a.s = a_s;
184+
b.s = b_s;
185+
r.i = a.i % b.i;
186+
return r.s;
187+
}
188+
147189
#endif

0 commit comments

Comments
 (0)