summaryrefslogtreecommitdiffstats
path: root/libsyscalls_unsection_value.c
blob: fb0f1fe26dd9bd989103a8a05d2e20a8ca7c4359 (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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
/* See LICENSE file for copyright and license details. */
#include "common.h"


enum libsyscalls_error
libsyscalls_unsection_value(unsigned long long int value_in, size_t bits,
                            enum libsyscalls_datatype_section section,
                            unsigned long long int *value_out)
{
	unsigned long long int value = value_in, mask, shift;

	if (bits > sizeof(value) / sizeof(char) * CHAR_BIT ||
	    bits & (size_t)(LIBSYSCALLS_GET_SECTION_FRACTION(section) - 1U))
		return LIBSYSCALLS_E_INVAL;

	switch (section) {
	case LIBSYSCALLS_SECTION_UNDETERMINED:
	case LIBSYSCALLS_SECTION_WHOLE: /* 0xFFFFFFFFFFFFFFFF */
	case LIBSYSCALLS_SECTION_LOWER_HALF: /* 0x00000000FFFFFFFF */
	case LIBSYSCALLS_SECTION_LOWER_QUARTER: /* 0x000000000000FFFF */
		break;

	case LIBSYSCALLS_SECTION_UPPER_HALF: /* 0xFFFFFFFF00000000 */
	case LIBSYSCALLS_SECTION_LOWER_MID_QUARTER: /* 0x00000000FFFF0000 */
		value <<= bits;
		break;

	case LIBSYSCALLS_SECTION_INNER_HALF: /* 0x0000FFFFFFFF0000 */
		value <<= bits / 2;
		break;

	case LIBSYSCALLS_SECTION_UPPER_MID_QUARTER: /* 0x0000FFFF00000000 */
		value <<= bits * 2;
		break;

	case LIBSYSCALLS_SECTION_UPPER_QUARTER: /* 0xFFFF000000000000 */
		value <<= bits * 3;
		break;

	case LIBSYSCALLS_SECTION_OUTER_HALF: /* 0xFFFF00000000FFFF */
		mask = (1ULL << (bits / 2)) - 1ULL;
		value = ((value & ~mask) << bits)
		      | (value & mask);
		break;

	case LIBSYSCALLS_SECTION_EVEN_QUARTERS_AS_HALF: /* 0x0000FFFF0000FFFF */
		mask = (1ULL << (bits / 2)) - 1ULL;
		value = ((value & ~mask) << (bits / 2))
		      | (value & mask);
		break;

	case LIBSYSCALLS_SECTION_ODD_QUARTERS_AS_HALF: /* 0xFFFF0000FFFF0000 */
		mask = (1ULL << (bits / 2)) - 1ULL;
		value = ((value & ~mask) << (bits / 2))
		      | (value & mask);
		value <<= bits / 2;
		break;

	case LIBSYSCALLS_SECTION_EVEN_BYTES_AS_HALF: /* 0x00FF00FF00FF00FF */
		value = 0;
		for (shift = 0; value_in; value_in >>= 8, shift += 16)
			value |= (value_in & 0xFFU) << shift;
		break;

	case LIBSYSCALLS_SECTION_ODD_BYTES_AS_HALF: /* 0xFF00FF00FF00FF00 */
		value = 0;
		for (shift = 8; value_in; value_in >>= 8, shift += 16)
			value |= (value_in & 0xFFU) << shift;
		break;

	default:
		return LIBSYSCALLS_E_INVAL;
	}

	if (value_out)
		*value_out = value;
	return LIBSYSCALLS_E_OK;
}