From patchwork Mon Jun 20 00:42:10 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Kent Overstreet X-Patchwork-Id: 12886902 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 kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by smtp.lore.kernel.org (Postfix) with ESMTP id 011C6C433EF for ; Mon, 20 Jun 2022 00:42:58 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 8E2A96B0082; Sun, 19 Jun 2022 20:42:58 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 8944C6B0083; Sun, 19 Jun 2022 20:42:58 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 734426B0085; Sun, 19 Jun 2022 20:42:58 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0015.hostedemail.com [216.40.44.15]) by kanga.kvack.org (Postfix) with ESMTP id 676606B0082 for ; Sun, 19 Jun 2022 20:42:58 -0400 (EDT) Received: from smtpin29.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay10.hostedemail.com (Postfix) with ESMTP id 39FC95E3 for ; Mon, 20 Jun 2022 00:42:58 +0000 (UTC) X-FDA: 79596764436.29.50E882D Received: from mail-qv1-f50.google.com (mail-qv1-f50.google.com [209.85.219.50]) by imf12.hostedemail.com (Postfix) with ESMTP id E13B240096 for ; Mon, 20 Jun 2022 00:42:57 +0000 (UTC) Received: by mail-qv1-f50.google.com with SMTP id g18so6506690qvn.2 for ; Sun, 19 Jun 2022 17:42:57 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=4+doRSndIqb/FisTLPVe5Bh/ofBw5aQUkb0tO+s0zbQ=; b=WRsAgh5sLiSXnUWsFWUdKfv3snQ371BnO978e4fY372IVVti8PHQr4mdmPcHCoZIfl cWczSiCcC50anmfaWm5q+8+IS6sHDTgs3xufm0GoK1c6bn3oMXlKLNMszpjs0bDm7H9b VJm2O7Ylezs9ZcWjgYT74IdKN+FJ+ItstRUT58jSjskNJXfCspOSSunzLuskncp+UTom ETzU9mOWaaMZ0IN7+jLeqWPNOuFYTit5m4dagIJPviypEkq0hnRkrDJ4tK9AgOtHougZ o2L9q/1OmXm/Y7ZyrFN6uDiZMnY883rXN8+npJm2rHrMai7cWfip3CRSNksw6aBnugVH J6wg== 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=4+doRSndIqb/FisTLPVe5Bh/ofBw5aQUkb0tO+s0zbQ=; b=zrfk1nQWYZHncoo/MANn+TBUhlpeYwuuDB3JmlSOwvY+e9Gh2i4u+rq5mley/ddB+6 Kc6XhYKRQ1aTe6b0QYv2untmdyyIbzEnmNBOOoge0KkpNbXS0weiEP/YFEMnYAuRNI7Y LsP1sj7/aVgWjNsKSuaxarzmXpSiLhGLacT0m1tDxORazamaNdkBxTYdLlLYz5cIXU55 XfaWu/0reRklR12r7tcar0joJsIq0f4L/ynLLqi2QFBuySAgCgALz358LdAWIdapEz0y 8V7173vaNsQk9KH8CZFHbqvY7pROhZ04D+HaTaImhVA32kdz/ILX1BC6JCE1di+peoMb OE2w== X-Gm-Message-State: AJIora8YnyvZWWvderx8URiNCjidStVjXynuiFxH/akDPMuCREhVTFDP U23cV8dmiyGMm8E5jHU1hA== X-Google-Smtp-Source: AGRyM1vA0V9RjbIKZsYA7zZoD0L520hpVBmC/QV5Y4Fl5nCasrnrNz3TXtyGGqCBHjwmSFxKxKwLGQ== X-Received: by 2002:a05:6214:c82:b0:46a:b677:e284 with SMTP id r2-20020a0562140c8200b0046ab677e284mr16988331qvr.28.1655685777230; Sun, 19 Jun 2022 17:42:57 -0700 (PDT) Received: from localhost (c-73-219-103-14.hsd1.vt.comcast.net. [73.219.103.14]) by smtp.gmail.com with ESMTPSA id l16-20020a05620a28d000b006a6cadd89efsm11521487qkp.82.2022.06.19.17.42.55 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 19 Jun 2022 17:42:55 -0700 (PDT) From: Kent Overstreet To: linux-kernel@vger.kernel.org, linux-mm@kvack.org, pmladek@suse.com Cc: Kent Overstreet , rostedt@goodmis.org, enozhatsky@chromium.org, linux@rasmusvillemoes.dk, willy@infradead.org Subject: [PATCH v4 11/34] vsprintf: Improve number() Date: Sun, 19 Jun 2022 20:42:10 -0400 Message-Id: <20220620004233.3805-12-kent.overstreet@gmail.com> X-Mailer: git-send-email 2.36.1 In-Reply-To: <20220620004233.3805-1-kent.overstreet@gmail.com> References: <20220620004233.3805-1-kent.overstreet@gmail.com> MIME-Version: 1.0 ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1655685777; a=rsa-sha256; cv=none; b=Bde+wjziU+xHen1SOaYa/kQ9Yzexn+BVusSCH7qPu2DLxEUF0BGJ3JMFkV+wY6sgC3Drq3 o8zyi+1LiYJZo1YC+UGGg9DGEBaETUj2BkvGtAxXOCgjFa/6poZFV8DiX4DULWIJfXLrGB B4hMxbo50OpGxIOrc4Qdntd0Mqpo1i8= ARC-Authentication-Results: i=1; imf12.hostedemail.com; dkim=pass header.d=gmail.com header.s=20210112 header.b=WRsAgh5s; spf=pass (imf12.hostedemail.com: domain of kent.overstreet@gmail.com designates 209.85.219.50 as permitted sender) smtp.mailfrom=kent.overstreet@gmail.com; dmarc=pass (policy=none) header.from=gmail.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1655685777; h=from:from:sender:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:mime-version: content-type:content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references:dkim-signature; bh=4+doRSndIqb/FisTLPVe5Bh/ofBw5aQUkb0tO+s0zbQ=; b=nH+rxKZzYPKgGu7/MG2+LlAt1qis1uwscqzhxotZQqiXutj6uIMsZwTVrG0rxa6Us32/RE 1cBYut2jTHuolWBjNtBMcm6C/R9xsDgK6C8xEMgAn6o9gjutX7aoOEjXw3E/GNKqXQd5p2 jroaD7yo7XpUwWi0On6RY+ZRoqtRaCk= X-Stat-Signature: ijqghqxqd5qs1zqy5np5ydafiju1hxhy X-Rspamd-Queue-Id: E13B240096 Authentication-Results: imf12.hostedemail.com; dkim=pass header.d=gmail.com header.s=20210112 header.b=WRsAgh5s; spf=pass (imf12.hostedemail.com: domain of kent.overstreet@gmail.com designates 209.85.219.50 as permitted sender) smtp.mailfrom=kent.overstreet@gmail.com; dmarc=pass (policy=none) header.from=gmail.com X-Rspamd-Server: rspam07 X-Rspam-User: X-HE-Tag: 1655685777-215874 X-Bogosity: Ham, tests=bogofilter, spamicity=0.000000, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: This patch refactors number() to make it a bit clearer, and it also changes it to call printbuf_make_room() only once at the start, instead of in the printbuf output helpers. Signed-off-by: Kent Overstreet --- lib/vsprintf.c | 83 +++++++++++++++++++++++++------------------------- 1 file changed, 41 insertions(+), 42 deletions(-) diff --git a/lib/vsprintf.c b/lib/vsprintf.c index 5afa74dda5..7d20406deb 100644 --- a/lib/vsprintf.c +++ b/lib/vsprintf.c @@ -458,93 +458,92 @@ void number(struct printbuf *out, unsigned long long num, { /* put_dec requires 2-byte alignment of the buffer. */ char tmp[3 * sizeof(num)] __aligned(2); - char sign; - char locase; + char sign = 0; + /* locase = 0 or 0x20. ORing digits or letters with 'locase' + * produces same digits or (maybe lowercased) letters */ + char locase = (spec.flags & SMALL); int need_pfx = ((spec.flags & SPECIAL) && spec.base != 10); - int i; bool is_zero = num == 0LL; int field_width = spec.field_width; int precision = spec.precision; + int nr_digits = 0; + int output_bytes = 0; - /* locase = 0 or 0x20. ORing digits or letters with 'locase' - * produces same digits or (maybe lowercased) letters */ - locase = (spec.flags & SMALL); if (spec.flags & LEFT) spec.flags &= ~ZEROPAD; - sign = 0; if (spec.flags & SIGN) { if ((signed long long)num < 0) { sign = '-'; num = -(signed long long)num; - field_width--; + output_bytes++; } else if (spec.flags & PLUS) { sign = '+'; - field_width--; + output_bytes++; } else if (spec.flags & SPACE) { sign = ' '; - field_width--; + output_bytes++; } } if (need_pfx) { if (spec.base == 16) - field_width -= 2; + output_bytes += 2; else if (!is_zero) - field_width--; + output_bytes++; } /* generate full string in tmp[], in reverse order */ - i = 0; - if (num < spec.base) - tmp[i++] = hex_asc_upper[num] | locase; - else if (spec.base != 10) { /* 8 or 16 */ + if (spec.base == 10) { + nr_digits = put_dec(tmp, num) - tmp; + } else { /* 8 or 16 */ int mask = spec.base - 1; - int shift = 3; + int shift = ilog2((unsigned) spec.base); - if (spec.base == 16) - shift = 4; do { - tmp[i++] = (hex_asc_upper[((unsigned char)num) & mask] | locase); + tmp[nr_digits++] = (hex_asc_upper[((unsigned char)num) & mask] | locase); num >>= shift; } while (num); - } else { /* base 10 */ - i = put_dec(tmp, num) - tmp; } /* printing 100 using %2d gives "100", not "00" */ - if (i > precision) - precision = i; + precision = max(nr_digits, precision); + output_bytes += precision; + field_width = max(0, field_width - output_bytes); + + printbuf_make_room(out, field_width + output_bytes); + /* leading space padding */ - field_width = max(0, field_width - precision); if (!(spec.flags & (ZEROPAD | LEFT)) && field_width) { - __prt_chars(out, ' ', field_width); + __prt_chars_reserved(out, ' ', field_width); field_width = 0; } + /* sign */ if (sign) - __prt_char(out, sign); + __prt_char_reserved(out, sign); + /* "0x" / "0" prefix */ if (need_pfx) { if (spec.base == 16 || !is_zero) - __prt_char(out, '0'); + __prt_char_reserved(out, '0'); if (spec.base == 16) - __prt_char(out, 'X' | locase); + __prt_char_reserved(out, 'X' | locase); } - /* zero or space padding */ - if (!(spec.flags & LEFT) && field_width) { - char c = ' ' + (spec.flags & ZEROPAD); - __prt_chars(out, c, field_width); - field_width = 0; - } - /* hmm even more zero padding? */ - if (precision > i) - __prt_chars(out, '0', precision - i); + /* zero padding */ + if (!(spec.flags & LEFT) && field_width) + __prt_chars_reserved(out, '0', field_width); + + /* zero padding from precision */ + if (precision > nr_digits) + __prt_chars_reserved(out, '0', precision - nr_digits); + /* actual digits of result */ - while (--i >= 0) - __prt_char(out, tmp[i]); + while (--nr_digits >= 0) + __prt_char_reserved(out, tmp[nr_digits]); + /* trailing space padding */ - if (field_width) - __prt_chars(out, ' ', field_width); + if ((spec.flags & LEFT) && field_width) + __prt_chars_reserved(out, ' ', field_width); printbuf_nul_terminate(out); }