]> www.infradead.org Git - users/sagi/nvme-cli.git/commitdiff
util/types: Add 128 bit conversion helpers
authorSteven Seungcheol Lee <sc108.lee@samsung.com>
Wed, 7 Sep 2022 09:10:01 +0000 (11:10 +0200)
committerDaniel Wagner <dwagner@suse.de>
Wed, 7 Sep 2022 09:27:26 +0000 (11:27 +0200)
Signed-off-by: Steven Seungcheol Lee <sc108.lee@samsung.com>
[dwagner: refactoring]
Signed-off-by: Daniel Wagner <dwagner@suse.de>
util/types.c
util/types.h

index 1f1c344f6a0c3c6dae46113b6b5a64d2bc857c42..e52c35a8b049c612b998755ece0d5499011389a3 100644 (file)
@@ -5,6 +5,18 @@
 
 #include "types.h"
 
+__uint128_t le128_to_cpu(__u8 *data)
+{
+       int i;
+       __uint128_t result = 0;
+
+       for (i = 0; i < 16; i++) {
+               result *= 256;
+               result += data[15 - i];
+       }
+       return result;
+}
+
 long double int128_to_double(__u8 *data)
 {
        int i;
@@ -29,6 +41,25 @@ uint64_t int48_to_long(__u8 *data)
        return result;
 }
 
+char str_uint128[40];
+char *uint128_t_to_string(__uint128_t val)
+{
+       char str_rev[40]; /* __uint128_t  maximum string length is 39 */
+       int i, j;
+
+       for (i = 0; val > 0; i++) {
+               str_rev[i] = (val % 10) + 48;
+               val /= 10;
+       }
+
+       for (j = 0; i >= 0;) {
+               str_uint128[j++] = str_rev[--i];
+       }
+       str_uint128[j] = '\0';
+
+       return str_uint128;
+}
+
 const char *util_uuid_to_string(uuid_t uuid)
 {
        /* large enough to hold uuid str (37) + null-termination byte */
index 8b63452d30ba547ab0be565e7259a09accf6d843..be98059fdd7c2907c638422e5aba491a011ce5a0 100644 (file)
@@ -15,9 +15,11 @@ static inline long kelvin_to_celsius(long t)
        return t + ABSOLUTE_ZERO_CELSIUS;
 }
 
+__uint128_t le128_to_cpu(__u8 *data);
 long double int128_to_double(__u8 *data);
 uint64_t int48_to_long(__u8 *data);
 
+char *uint128_t_to_string(__uint128_t val);
 const char *util_uuid_to_string(uuid_t uuid);
 const char *util_fw_to_string(char *c);