diff options
author | Mattias Andrée <maandree@kth.se> | 2023-12-16 12:40:10 +0100 |
---|---|---|
committer | Mattias Andrée <maandree@kth.se> | 2023-12-16 12:40:10 +0100 |
commit | 683f205402a99cfc8cea46c83ce9b46a42616d42 (patch) | |
tree | f6ee1619454a6ec8b9e31770bbbc2abf36aae2fd /libsyscalls_make_signed_integer.c | |
parent | Improve portability (diff) | |
download | libsyscalls-683f205402a99cfc8cea46c83ce9b46a42616d42.tar.gz libsyscalls-683f205402a99cfc8cea46c83ce9b46a42616d42.tar.bz2 libsyscalls-683f205402a99cfc8cea46c83ce9b46a42616d42.tar.xz |
All kinds of stuff
Signed-off-by: Mattias Andrée <maandree@kth.se>
Diffstat (limited to 'libsyscalls_make_signed_integer.c')
-rw-r--r-- | libsyscalls_make_signed_integer.c | 56 |
1 files changed, 56 insertions, 0 deletions
diff --git a/libsyscalls_make_signed_integer.c b/libsyscalls_make_signed_integer.c new file mode 100644 index 0000000..b4142dc --- /dev/null +++ b/libsyscalls_make_signed_integer.c @@ -0,0 +1,56 @@ +/* See LICENSE file for copyright and license details. */ +#include "common.h" + + +enum libsyscalls_error +libsyscalls_make_signed_integer(unsigned long long int value_in, int negative, + enum libsyscalls_datatype_sign_representation representation, + size_t bits, unsigned long long int *value_out) +{ + unsigned long long int value = value_in, mask, highbit; + + if (!bits || bits > sizeof(value) / sizeof(char) * CHAR_BIT) + return LIBSYSCALLS_E_INVAL; + + switch (representation) { + case LIBSYSCALLS_SIGN_UNDETERMINED: + break; + + case LIBSYSCALLS_SIGN_TWOS_COMPLEMENT: + if (negative && value) { + highbit = 1ULL << (bits - 1U); + mask = highbit - 1ULL; + value = ~(value - 1) & mask; + value |= highbit; + } + break; + + case LIBSYSCALLS_SIGN_ONES_COMPLEMENT: + if (negative) { + highbit = 1ULL << (bits - 1U); + mask = highbit - 1ULL; + value = ~value & mask; + value |= highbit; + } + break; + + case LIBSYSCALLS_SIGN_SIGN_MAGNITUDE: + if (negative) + value ^= 1ULL << (bits - 1); + break; + + case LIBSYSCALLS_SIGN_EXCESS_HALF: + if (!negative) + value ^= 1ULL << (bits - 1); + else + value = (1ULL << (bits - 1)) - value; + break; + + default: + return LIBSYSCALLS_E_INVAL; + } + + if (value_out) + *value_out = value; + return LIBSYSCALLS_E_OK; +} |