From 225f1408e03f2e66198f3da20c42a746e2729ab0 Mon Sep 17 00:00:00 2001 From: Mattias Andrée Date: Thu, 7 Dec 2023 20:35:16 +0100 Subject: Fourth commit MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Mattias Andrée --- libsyscalls_get_integer_alignment.c | 58 +++++++++++++++++++++++++++++++++++++ 1 file changed, 58 insertions(+) create mode 100644 libsyscalls_get_integer_alignment.c (limited to 'libsyscalls_get_integer_alignment.c') diff --git a/libsyscalls_get_integer_alignment.c b/libsyscalls_get_integer_alignment.c new file mode 100644 index 0000000..7802a06 --- /dev/null +++ b/libsyscalls_get_integer_alignment.c @@ -0,0 +1,58 @@ +/* See LICENSE file for copyright and license details. */ +#include "common.h" + + +#include "generated/integers.c" + +enum libsyscalls_error +libsyscalls_get_integer_alignment(enum libsyscalls_os os, enum libsyscalls_arch arch, unsigned width_in_bits, unsigned *alignment_out) +{ + unsigned maxalign; + + if (!width_in_bits) + return LIBSYSCALLS_E_INVAL; + + switch ((int)arch) { + case LIBSYSCALLS_ARCH_M68K: /* https://m680x0.github.io/doc/abi.html#scalar-types + * https://lists.nongnu.org/archive/html/qemu-devel/2015-02/msg04797.html */ + maxalign = 16; + break; + + case LIBSYSCALLS_ARCH_I386: + maxalign = 32; + break; + + case LIBSYSCALLS_ARCH_AMD64: + case LIBSYSCALLS_ARCH_AMD64_X32: + case LIBSYSCALLS_ARCH_ALPHA: /* https://static.lwn.net/images/pdf/LDD3/ch11.pdf */ + case LIBSYSCALLS_ARCH_IA64: /* https://static.lwn.net/images/pdf/LDD3/ch11.pdf */ + case LIBSYSCALLS_ARCH_MIPS_O32: /* https://static.lwn.net/images/pdf/LDD3/ch11.pdf */ + case LIBSYSCALLS_ARCH_PARISC_32: /* https://www.ece.lsu.edu/ee4720/doc/pa1.1.pdf (page 26) */ + case LIBSYSCALLS_ARCH_PARISC_64: + case LIBSYSCALLS_ARCH_POWERPC_32: /* https://static.lwn.net/images/pdf/LDD3/ch11.pdf */ + case LIBSYSCALLS_ARCH_SPARC_32: /* https://www.gaisler.com/doc/sparcv8.pdf (page 46) */ + case LIBSYSCALLS_ARCH_SPARC_64: /* https://static.lwn.net/images/pdf/LDD3/ch11.pdf */ + case LIBSYSCALLS_ARCH_XTENSA: /* https://loboris.eu/ESP32/Xtensa_lx%20Overview%20handbook.pdf (page 97) */ + maxalign = 64; + break; + + default: + return LIBSYSCALLS_E_ARCHNOSUP; + } + + if (width_in_bits & (width_in_bits - 1) || width_in_bits & 7 || width_in_bits > 64) + return LIBSYSCALLS_E_NOSUCHTYPE; + *alignment_out = width_in_bits < maxalign ? width_in_bits : maxalign; + +#define CASE(UPPERCASE, LOWERCASE)\ + case LIBSYSCALLS_OS_##UPPERCASE:\ + return get_##LOWERCASE##_integer_alignment(arch, width_in_bits, alignment_out) + + switch ((int)os) { + LIST_OSES(CASE, ;); + default: + return LIBSYSCALLS_E_OSNOSUP; + } + +#undef CASE +} -- cgit v1.2.3-70-g09d2