All of lore.kernel.org
 help / color / mirror / Atom feed
From: <ahmeddan@amazon.com>
To: <kvm@vger.kernel.org>, <pbonzini@redhat.com>,
	<nikos.nikoleris@arm.com>, <drjones@redhat.com>,
	<graf@amazon.com>
Cc: Daniele Ahmed <ahmeddan@amazon.com>
Subject: [kvm-unit-tests PATCH v2 1/3] lib/string: Add stroull and strtoll
Date: Mon, 27 Sep 2021 15:30:26 +0000	[thread overview]
Message-ID: <20210927153028.27680-1-ahmeddan@amazon.com> (raw)
In-Reply-To: <08d356da-17ce-d380-1fc9-18ba7ec67020@amazon.com>

From: Daniele Ahmed <ahmeddan@amazon.com>

Add the two functions strtoull and strtoll.
This is in preparation for an update
in x86/msr.c to write 64b values
that are given as inputs as strings by
a user.

Signed-off-by: Daniele Ahmed <ahmeddan@amazon.com>
---
 lib/stdlib.h |  2 ++
 lib/string.c | 74 ++++++++++++++++++++++++++++++++++++++++++++++++++++
 2 files changed, 76 insertions(+)

diff --git a/lib/stdlib.h b/lib/stdlib.h
index 33c00e8..48e10f0 100644
--- a/lib/stdlib.h
+++ b/lib/stdlib.h
@@ -9,5 +9,7 @@
 
 long int strtol(const char *nptr, char **endptr, int base);
 unsigned long int strtoul(const char *nptr, char **endptr, int base);
+long long int strtoll(const char *nptr, char **endptr, int base);
+unsigned long long strtoull(const char *nptr, char **endptr, int base);
 
 #endif /* _STDLIB_H_ */
diff --git a/lib/string.c b/lib/string.c
index ffc7c7e..dacd927 100644
--- a/lib/string.c
+++ b/lib/string.c
@@ -242,6 +242,80 @@ unsigned long int strtoul(const char *nptr, char **endptr, int base)
     return __strtol(nptr, endptr, base, false);
 }
 
+static unsigned long long __strtoll(const char *nptr, char **endptr,
+                              int base, bool is_signed) {
+    unsigned long long acc = 0;
+    const char *s = nptr;
+    int neg, c;
+
+    assert(base == 0 || (base >= 2 && base <= 36));
+
+    while (isspace(*s))
+        s++;
+
+    if (*s == '-') {
+        neg = 1;
+        s++;
+    } else {
+        neg = 0;
+        if (*s == '+')
+            s++;
+    }
+
+    if (base == 0 || base == 16) {
+        if (*s == '0') {
+            s++;
+            if (*s == 'x' || *s == 'X') {
+                 s++;
+                 base = 16;
+            } else if (base == 0)
+                 base = 8;
+        } else if (base == 0)
+            base = 10;
+    }
+
+    while (*s) {
+        if (*s >= '0' && *s < '0' + base && *s <= '9')
+            c = *s - '0';
+        else if (*s >= 'a' && *s < 'a' + base - 10)
+            c = *s - 'a' + 10;
+        else if (*s >= 'A' && *s < 'A' + base - 10)
+            c = *s - 'A' + 10;
+        else
+            break;
+
+        if (is_signed) {
+            long long sacc = (long long)acc;
+            assert(!check_mul_overflow(sacc, base));
+            assert(!check_add_overflow(sacc * base, c));
+        } else {
+            assert(!check_mul_overflow(acc, base));
+            assert(!check_add_overflow(acc * base, c));
+        }
+
+        acc = acc * base + c;
+        s++;
+    }
+
+    if (neg)
+        acc = -acc;
+
+    if (endptr)
+        *endptr = (char *)s;
+
+    return acc;
+}
+
+long long int strtoll(const char *nptr, char **endptr, int base)
+{
+    return __strtoll(nptr, endptr, base, true);
+}
+
+unsigned long long int strtoull(const char *nptr, char **endptr, int base)
+{
+    return __strtoll(nptr, endptr, base, false);
+}
+
 long atol(const char *ptr)
 {
     return strtol(ptr, NULL, 10);
-- 
2.32.0




Amazon Development Center Germany GmbH
Krausenstr. 38
10117 Berlin
Geschaeftsfuehrung: Christian Schlaeger, Jonathan Weiss
Eingetragen am Amtsgericht Charlottenburg unter HRB 149173 B
Sitz: Berlin
Ust-ID: DE 289 237 879




  parent reply	other threads:[~2021-09-27 15:30 UTC|newest]

Thread overview: 11+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2021-08-10 14:31 [kvm-unit-tests PATCH] x86/msr.c generalize to any input msr yqwfh
2021-09-23  9:11 ` Alexander Graf
2021-09-23  9:32   ` Paolo Bonzini
2021-09-27 15:30   ` ahmeddan [this message]
2021-09-27 15:30     ` [kvm-unit-tests PATCH v2 2/3] [kvm-unit-tests PATCH] x86/msr.c refactor out generic test logic ahmeddan
2021-09-27 15:38       ` Alexander Graf
2021-09-27 15:30     ` [kvm-unit-tests PATCH v2 3/3] x86/msr.c generalize to any input msr ahmeddan
2021-09-28 15:36       ` Paolo Bonzini
2021-10-01 14:14         ` Ahmed, Daniele
2021-10-15 15:57       ` Paolo Bonzini
2021-10-13 16:44     ` [kvm-unit-tests PATCH v2 1/3] lib/string: Add stroull and strtoll Andrew Jones

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20210927153028.27680-1-ahmeddan@amazon.com \
    --to=ahmeddan@amazon.com \
    --cc=drjones@redhat.com \
    --cc=graf@amazon.com \
    --cc=kvm@vger.kernel.org \
    --cc=nikos.nikoleris@arm.com \
    --cc=pbonzini@redhat.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.