From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id A393FC433F5 for ; Thu, 14 Oct 2021 18:51:32 +0000 (UTC) Received: from phobos.denx.de (phobos.denx.de [85.214.62.61]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by mail.kernel.org (Postfix) with ESMTPS id 2BC216108E for ; Thu, 14 Oct 2021 18:51:32 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.1 mail.kernel.org 2BC216108E Authentication-Results: mail.kernel.org; dmarc=fail (p=none dis=none) header.from=chromium.org Authentication-Results: mail.kernel.org; spf=pass smtp.mailfrom=lists.denx.de Received: from h2850616.stratoserver.net (localhost [IPv6:::1]) by phobos.denx.de (Postfix) with ESMTP id 7DAE9837D4; Thu, 14 Oct 2021 20:50:41 +0200 (CEST) Authentication-Results: phobos.denx.de; dmarc=pass (p=none dis=none) header.from=chromium.org Authentication-Results: phobos.denx.de; spf=pass smtp.mailfrom=u-boot-bounces@lists.denx.de Authentication-Results: phobos.denx.de; dkim=pass (1024-bit key; unprotected) header.d=chromium.org header.i=@chromium.org header.b="ne/08r3x"; dkim-atps=neutral Received: by phobos.denx.de (Postfix, from userid 109) id ABBF383649; Thu, 14 Oct 2021 20:49:17 +0200 (CEST) Received: from mail-io1-xd35.google.com (mail-io1-xd35.google.com [IPv6:2607:f8b0:4864:20::d35]) (using TLSv1.3 with cipher TLS_AES_128_GCM_SHA256 (128/128 bits)) (No client certificate requested) by phobos.denx.de (Postfix) with ESMTPS id D528C836E0 for ; Thu, 14 Oct 2021 20:48:37 +0200 (CEST) Authentication-Results: phobos.denx.de; dmarc=pass (p=none dis=none) header.from=chromium.org Authentication-Results: phobos.denx.de; spf=pass smtp.mailfrom=sjg@chromium.org Received: by mail-io1-xd35.google.com with SMTP id d125so4962040iof.5 for ; Thu, 14 Oct 2021 11:48:37 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=chromium.org; s=google; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=msOqsxFEzdmbn0jdAEMOhT3moOBeYdM4nNwvhrubYFU=; b=ne/08r3xN+kNM3EBvHV8rd7twZW0e6lq9i4TVfvmmku8RhIl+PaT6jsWFL5E0GEFmc QN6Ww88V9/LvBUZXTGTP54Dh2Al7nQ6AHO2GHdzqqKuA42bIKCOdeXGINsMSYFYStsLR gh3rPWMl9EW/NwN07R1KXa1wK8qbhzWBMcsBc= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=msOqsxFEzdmbn0jdAEMOhT3moOBeYdM4nNwvhrubYFU=; b=LaULzJV4631Yu4omU9413/O4BnYkUSY7RVrDqrV3I4bwxJWu4UjqdTzZGymW6aV9D8 gPdf2gJ9d8LIUtmXN0r12Ld34UrdakPv0BBf3UM4nK6PTKNAhAY4KvwTs3hCz0R3eJlE T7cGnQ8cU8tnS5S5zcZasJrBsNc2sbE+0A3r2hIseTZ4okbw131CTdiuRP26iaJDACsG w+9ULXqRg2q7P6aCea8x89+LVKZhTH2aC4LxAzjnVfK4p4VMWbEbW8emGIrgwT/A0GKf Krqh0XZIeXVG3nX1wegczB3SnHJ0g54Kzq/IA3fdgUMshkjuSbI6bODS/6gmyTefQz2H pPtw== X-Gm-Message-State: AOAM530/1DOf3pzknJSK86RpFEsL4daS3Vo1io0OQ/XgIBKD8P7gxUve uFshSAwtfIoAP9nv3958dXkuK1QlglM= X-Google-Smtp-Source: ABdhPJyp1Vx3cuTlBJot1o2NED4lqA6sLWDH/kt0cz+nyINM7hNv0CjJ32FJPHWNIiIOKU0+YH+r5g== X-Received: by 2002:a05:6602:158a:: with SMTP id e10mr488768iow.157.1634237316235; Thu, 14 Oct 2021 11:48:36 -0700 (PDT) Received: from kiwi.bld.corp.google.com (c-67-190-101-114.hsd1.co.comcast.net. [67.190.101.114]) by smtp.gmail.com with ESMTPSA id w15sm1697625ill.23.2021.10.14.11.48.35 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 14 Oct 2021 11:48:35 -0700 (PDT) From: Simon Glass To: U-Boot Mailing List Cc: Patrice Chotard , Artem Lapkin , Tom Rini , Joe Hershberger , Heinrich Schuchardt , Peter Hoyes , Simon Glass Subject: [PATCH v3 14/18] lib: Add a function to convert a string to a hex value Date: Thu, 14 Oct 2021 12:48:07 -0600 Message-Id: <20211014124803.v3.14.Ifcd170c380694dd653290f0eb044b279c3593df4@changeid> X-Mailer: git-send-email 2.33.0.1079.g6e70778dc9-goog In-Reply-To: <20211014184811.482560-1-sjg@chromium.org> References: <20211014184811.482560-1-sjg@chromium.org> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-BeenThere: u-boot@lists.denx.de X-Mailman-Version: 2.1.34 Precedence: list List-Id: U-Boot discussion List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: u-boot-bounces@lists.denx.de Sender: "U-Boot" X-Virus-Scanned: clamav-milter 0.103.2 at phobos.denx.de X-Virus-Status: Clean Add an xtoa() function, similar to itoa() but for hex instead. Signed-off-by: Simon Glass --- (no changes since v1) include/vsprintf.h | 16 ++++++++++++++-- lib/vsprintf.c | 20 ++++++++++++++++++-- test/print_ut.c | 24 ++++++++++++++++++++++++ 3 files changed, 56 insertions(+), 4 deletions(-) diff --git a/include/vsprintf.h b/include/vsprintf.h index 4479df0af3f..b4746301462 100644 --- a/include/vsprintf.h +++ b/include/vsprintf.h @@ -177,14 +177,26 @@ int vsprintf(char *buf, const char *fmt, va_list args); * simple_itoa() - convert an unsigned integer to a string * * This returns a static string containing the decimal representation of the - * given value. The returned value may be overwritten by other calls to the - * same function, so should be used immediately + * given value. The returned value may be overwritten by other calls to other + * simple_... functions, so should be used immediately * * @val: Value to convert * @return string containing the decimal representation of @val */ char *simple_itoa(ulong val); +/** + * simple_xtoa() - convert an unsigned integer to a hex string + * + * This returns a static string containing the hexadecimal representation of the + * given value. The returned value may be overwritten by other calls to other + * simple_... functions, so should be used immediately + * + * @val: Value to convert + * @return string containing the hexecimal representation of @val + */ +char *simple_xtoa(ulong num); + /** * Format a string and place it in a buffer * diff --git a/lib/vsprintf.c b/lib/vsprintf.c index d7ee35b4773..e634bd70b66 100644 --- a/lib/vsprintf.c +++ b/lib/vsprintf.c @@ -816,11 +816,12 @@ int vprintf(const char *fmt, va_list args) } #endif +static char local_toa[22]; + char *simple_itoa(ulong i) { /* 21 digits plus null terminator, good for 64-bit or smaller ints */ - static char local[22]; - char *p = &local[21]; + char *p = &local_toa[21]; *p-- = '\0'; do { @@ -830,6 +831,21 @@ char *simple_itoa(ulong i) return p + 1; } +char *simple_xtoa(ulong num) +{ + /* 16 digits plus nul terminator, good for 64-bit or smaller ints */ + char *p = &local_toa[17]; + + *--p = '\0'; + do { + p -= 2; + hex_byte_pack(p, num & 0xff); + num >>= 8; + } while (num > 0); + + return p; +} + /* We don't seem to have %'d in U-Boot */ void print_grouped_ull(unsigned long long int_val, int digits) { diff --git a/test/print_ut.c b/test/print_ut.c index 4fbb15b6d3c..152a8c3334f 100644 --- a/test/print_ut.c +++ b/test/print_ut.c @@ -335,16 +335,40 @@ static int print_itoa(struct unit_test_state *uts) ut_asserteq_str("0", simple_itoa(0)); ut_asserteq_str("2147483647", simple_itoa(0x7fffffff)); ut_asserteq_str("4294967295", simple_itoa(0xffffffff)); + + /* Use #ifdef here to avoid a compiler warning on 32-bit machines */ +#ifdef CONFIG_PHYS_64BIT if (sizeof(ulong) == 8) { ut_asserteq_str("9223372036854775807", simple_itoa((1UL << 63) - 1)); ut_asserteq_str("18446744073709551615", simple_itoa(-1)); } +#endif /* CONFIG_PHYS_64BIT */ return 0; } PRINT_TEST(print_itoa, 0); +static int print_xtoa(struct unit_test_state *uts) +{ + ut_asserteq_str("7f", simple_xtoa(127)); + ut_asserteq_str("00", simple_xtoa(0)); + ut_asserteq_str("7fffffff", simple_xtoa(0x7fffffff)); + ut_asserteq_str("ffffffff", simple_xtoa(0xffffffff)); + + /* Use #ifdef here to avoid a compiler warning on 32-bit machines */ +#ifdef CONFIG_PHYS_64BIT + if (sizeof(ulong) == 8) { + ut_asserteq_str("7fffffffffffffff", + simple_xtoa((1UL << 63) - 1)); + ut_asserteq_str("ffffffffffffffff", simple_xtoa(-1)); + } +#endif /* CONFIG_PHYS_64BIT */ + + return 0; +} +PRINT_TEST(print_xtoa, 0); + int do_ut_print(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[]) { struct unit_test *tests = UNIT_TEST_SUITE_START(print_test); -- 2.33.0.1079.g6e70778dc9-goog