summaryrefslogtreecommitdiffstats
path: root/libsyscalls_parse_signed_integer.c
diff options
context:
space:
mode:
authorMattias Andrée <maandree@kth.se>2023-12-16 12:40:10 +0100
committerMattias Andrée <maandree@kth.se>2023-12-16 12:40:10 +0100
commit683f205402a99cfc8cea46c83ce9b46a42616d42 (patch)
treef6ee1619454a6ec8b9e31770bbbc2abf36aae2fd /libsyscalls_parse_signed_integer.c
parentImprove portability (diff)
downloadlibsyscalls-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_parse_signed_integer.c')
-rw-r--r--libsyscalls_parse_signed_integer.c61
1 files changed, 61 insertions, 0 deletions
diff --git a/libsyscalls_parse_signed_integer.c b/libsyscalls_parse_signed_integer.c
new file mode 100644
index 0000000..da6f078
--- /dev/null
+++ b/libsyscalls_parse_signed_integer.c
@@ -0,0 +1,61 @@
+/* See LICENSE file for copyright and license details. */
+#include "common.h"
+
+
+enum libsyscalls_error
+libsyscalls_parse_signed_integer(unsigned long long int value_in, enum libsyscalls_datatype_sign_representation representation,
+ size_t bits, unsigned long long int *value_out, int *negative_out)
+{
+ unsigned long long int value = value_in, mask;
+ int negative = 0;
+
+ 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 (value >> (bits - 1U)) {
+ mask = (1ULL << (bits - 1U)) - 1ULL;
+ value = ~value & mask;
+ value += 1ULL;
+ negative = 1;
+ }
+ break;
+
+ case LIBSYSCALLS_SIGN_ONES_COMPLEMENT:
+ if (value >> (bits - 1U)) {
+ mask = (1ULL << (bits - 1U)) - 1ULL;
+ value = ~value & mask;
+ negative = 1;
+ }
+ break;
+
+ case LIBSYSCALLS_SIGN_SIGN_MAGNITUDE:
+ if (value >> (bits - 1U)) {
+ value ^= 1ULL << (bits - 1U);
+ negative = 1;
+ }
+ break;
+
+ case LIBSYSCALLS_SIGN_EXCESS_HALF:
+ if (value >> (bits - 1U)) {
+ value ^= 1ULL << (bits - 1U);
+ } else {
+ value = (1ULL << (bits - 1U)) - value;
+ negative = 1;
+ }
+ break;
+
+ default:
+ return LIBSYSCALLS_E_INVAL;
+ }
+
+ if (value_out)
+ *value_out = value;
+ if (negative_out)
+ *negative_out = negative;
+ return LIBSYSCALLS_E_OK;
+}