Message ID | 20231010074952.79165-2-philmd@linaro.org |
---|---|
State | New |
Headers | show |
Series | tests/qtest: Introduce qtest_get_base_arch() and qtest_get_arch_bits() | expand |
On 10/10/2023 09.49, Philippe Mathieu-Daudé wrote: > Add a method to return the architecture bits (currently 8/32/64). > > Signed-off-by: Philippe Mathieu-Daudé <philmd@linaro.org> > --- > tests/qtest/libqtest.h | 8 ++++++++ > tests/qtest/libqtest.c | 21 +++++++++++++++++++++ > 2 files changed, 29 insertions(+) > > diff --git a/tests/qtest/libqtest.h b/tests/qtest/libqtest.h > index e53e350e3a..1e1b42241d 100644 > --- a/tests/qtest/libqtest.h > +++ b/tests/qtest/libqtest.h > @@ -654,6 +654,14 @@ bool qtest_big_endian(QTestState *s); > */ > const char *qtest_get_arch(void); > > +/** > + * qtest_get_arch_bits: > + * > + * Returns: The architecture bits (a.k.a. word size) for the QEMU executable > + * under test. > + */ > +unsigned qtest_get_arch_bits(void); > + > /** > * qtest_has_accel: > * @accel_name: Accelerator name to check for. > diff --git a/tests/qtest/libqtest.c b/tests/qtest/libqtest.c > index b1eba71ffe..a643a6309c 100644 > --- a/tests/qtest/libqtest.c > +++ b/tests/qtest/libqtest.c > @@ -925,6 +925,27 @@ const char *qtest_get_arch(void) > return end + 1; > } > > +unsigned qtest_get_arch_bits(void) > +{ > + static const char *const arch64[] = { > + "aarch64", "hppa", "x86_64", "loongarch64", "mips64", > + "mips64el", "ppc64", "riscv64", "s390x", "sparc64", > + }; > + const char *arch = qtest_get_arch(); > + > + if (!strcmp(arch, "avr")) { Just a matter of taste, but I prefer g_str_equal(), that's easier to read. > + return 8; > + } > + > + for (unsigned i = 0; i < ARRAY_SIZE(arch64); i++) { > + if (!strcmp(arch, arch64[i])) { > + return 64; > + } > + } > + > + return 32; > +} Since this function might get called multiple times, would it make sense to cache the value? I.e.: static const unsigned bits; if (!bits) { ... do all the magic to find out the right bits ... } return bits; ? Thomas
On 10/10/23 11:46, Thomas Huth wrote: > On 10/10/2023 09.49, Philippe Mathieu-Daudé wrote: >> Add a method to return the architecture bits (currently 8/32/64). >> >> Signed-off-by: Philippe Mathieu-Daudé <philmd@linaro.org> >> --- >> tests/qtest/libqtest.h | 8 ++++++++ >> tests/qtest/libqtest.c | 21 +++++++++++++++++++++ >> 2 files changed, 29 insertions(+) >> +unsigned qtest_get_arch_bits(void) >> +{ >> + static const char *const arch64[] = { >> + "aarch64", "hppa", "x86_64", "loongarch64", "mips64", >> + "mips64el", "ppc64", "riscv64", "s390x", "sparc64", >> + }; >> + const char *arch = qtest_get_arch(); >> + >> + if (!strcmp(arch, "avr")) { > > Just a matter of taste, but I prefer g_str_equal(), that's easier to read. > >> + return 8; >> + } >> + >> + for (unsigned i = 0; i < ARRAY_SIZE(arch64); i++) { >> + if (!strcmp(arch, arch64[i])) { >> + return 64; >> + } >> + } >> + >> + return 32; >> +} > > Since this function might get called multiple times, would it make sense > to cache the value? I.e.: > > static const unsigned bits; > > if (!bits) { > ... do all the magic to find out the right bits ... > } > > return bits; > > ? Fine by me, I'll do as suggested in v2.
diff --git a/tests/qtest/libqtest.h b/tests/qtest/libqtest.h index e53e350e3a..1e1b42241d 100644 --- a/tests/qtest/libqtest.h +++ b/tests/qtest/libqtest.h @@ -654,6 +654,14 @@ bool qtest_big_endian(QTestState *s); */ const char *qtest_get_arch(void); +/** + * qtest_get_arch_bits: + * + * Returns: The architecture bits (a.k.a. word size) for the QEMU executable + * under test. + */ +unsigned qtest_get_arch_bits(void); + /** * qtest_has_accel: * @accel_name: Accelerator name to check for. diff --git a/tests/qtest/libqtest.c b/tests/qtest/libqtest.c index b1eba71ffe..a643a6309c 100644 --- a/tests/qtest/libqtest.c +++ b/tests/qtest/libqtest.c @@ -925,6 +925,27 @@ const char *qtest_get_arch(void) return end + 1; } +unsigned qtest_get_arch_bits(void) +{ + static const char *const arch64[] = { + "aarch64", "hppa", "x86_64", "loongarch64", "mips64", + "mips64el", "ppc64", "riscv64", "s390x", "sparc64", + }; + const char *arch = qtest_get_arch(); + + if (!strcmp(arch, "avr")) { + return 8; + } + + for (unsigned i = 0; i < ARRAY_SIZE(arch64); i++) { + if (!strcmp(arch, arch64[i])) { + return 64; + } + } + + return 32; +} + bool qtest_has_accel(const char *accel_name) { if (g_str_equal(accel_name, "tcg")) {
Add a method to return the architecture bits (currently 8/32/64). Signed-off-by: Philippe Mathieu-Daudé <philmd@linaro.org> --- tests/qtest/libqtest.h | 8 ++++++++ tests/qtest/libqtest.c | 21 +++++++++++++++++++++ 2 files changed, 29 insertions(+)