blob: 6cd797e9ba0fa6a43b04a65726cc7481b6517d00 (
plain) (
tree)
|
|
/* 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 */
break;
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;
}
|