From 16cddfb8329a25190525858a0784321698cb848b Mon Sep 17 00:00:00 2001 From: Mattias Andrée Date: Wed, 16 Dec 2015 19:45:48 +0100 Subject: we need signed integer representation conversion MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Mattias Andrée --- include/arpa/inet.h | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+) (limited to 'include') diff --git a/include/arpa/inet.h b/include/arpa/inet.h index 34ff52c..a7bd2c1 100644 --- a/include/arpa/inet.h +++ b/include/arpa/inet.h @@ -157,6 +157,32 @@ uint64_t _htonll(uint64_t) #endif +/* TODO We "need" conversion to and from two's complement. + * It is important to remember than intN_t cannot be + * used for this functions because they require two's + * complement. + * + * unsigned to_twos_complement(signed v) { + * unsigned r; + * if (v >= -0) + * return (unsigned)v; + * r = (unsigned)-v; + * r = ~(r - 1); + * return r; + * } + * + * signed from_twos_complement(unsigned v) { + * signed r; + * if ((v >> (X_BIT - 1)) == 0) + * return (signed)v; + * r = ~r; + * if (r == X_MAX) + * return errno = ERANGE, 0; + * return errno = 0, r + 1; + * } + */ + + #endif -- cgit v1.2.3-70-g09d2