/* See LICENSE file for copyright and license details. */ #include "common.h" enum libsyscalls_error libsyscalls_section_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; bits /= (size_t)LIBSYSCALLS_GET_SECTION_FRACTION(section); if (bits == sizeof(mask) / sizeof(char) * CHAR_BIT) mask = ~0ULL; else mask = (1ULL << bits) - 1ULL; 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 */ value = ((value & (mask << (bits + bits / 2))) >> bits) | (value & (mask >> (bits / 2))); break; case LIBSYSCALLS_SECTION_ODD_QUARTERS_AS_HALF: /* 0xFFFF0000FFFF0000 */ value >>= bits / 2; /* fall through */ case LIBSYSCALLS_SECTION_EVEN_QUARTERS_AS_HALF: /* 0x0000FFFF0000FFFF */ value = ((value & (mask << (bits / 2)) & ~mask) >> (bits / 2)) | (value & (mask >> (bits / 2))); break; case LIBSYSCALLS_SECTION_ODD_BYTES_AS_HALF: /* 0xFF00FF00FF00FF00 */ value_in >>= 8; /* fall through */ case LIBSYSCALLS_SECTION_EVEN_BYTES_AS_HALF: /* 0x00FF00FF00FF00FF */ value = 0; for (shift = 0; value_in; value_in >>= 16, shift += 8) value |= (value_in & 0x00FFU) << shift; break; default: return LIBSYSCALLS_E_INVAL; } value &= mask; masked: if (value_out) *value_out = value; return LIBSYSCALLS_E_OK; }