summaryrefslogtreecommitdiffstats
path: root/libsyscalls_get_integer_alignment.c
blob: f13f40f733ea988d64d17e7927c4b5f5e1072c17 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
/* See LICENSE file for copyright and license details. */
#include "common.h"


#include "generated/integers.c"

#define X(ARCH, CHARBITS, INTPTR_BITS, SIZE_BITS, INTMAX_ALIGN, ENDIAN, SIGN) [ARCH] = INTMAX_ALIGN
static const unsigned char aligntable[] = {LIST_ARCH_SPECS(X, COMMA)};
#undef X

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;

	if ((size_t)arch >= ELEMSOF(aligntable))
		return LIBSYSCALLS_E_ARCHNOSUP;
	maxalign = aligntable[(size_t)arch];
	if (!maxalign)
		return LIBSYSCALLS_E_ARCHNOSUP;

	if (IS_NOT_2POW(width_in_bits) || width_in_bits % 8 || 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
}