From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from mailman by lists.gnu.org with archive (Exim 4.43) id 1Lt81u-0002xn-Jb for mharc-grub-devel@gnu.org; Sun, 12 Apr 2009 18:19:58 -0400 Received: from mailman by lists.gnu.org with tmda-scanned (Exim 4.43) id 1Lt81s-0002xi-S9 for grub-devel@gnu.org; Sun, 12 Apr 2009 18:19:56 -0400 Received: from exim by lists.gnu.org with spam-scanned (Exim 4.43) id 1Lt81n-0002tM-Rl for grub-devel@gnu.org; Sun, 12 Apr 2009 18:19:56 -0400 Received: from [199.232.76.173] (port=41044 helo=monty-python.gnu.org) by lists.gnu.org with esmtp (Exim 4.43) id 1Lt81n-0002tE-PD for grub-devel@gnu.org; Sun, 12 Apr 2009 18:19:51 -0400 Received: from mail-fx0-f166.google.com ([209.85.220.166]:55940) by monty-python.gnu.org with esmtp (Exim 4.60) (envelope-from ) id 1Lt81n-0007ue-7A for grub-devel@gnu.org; Sun, 12 Apr 2009 18:19:51 -0400 Received: by fxm10 with SMTP id 10so1997517fxm.42 for ; Sun, 12 Apr 2009 15:19:49 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=gamma; h=domainkey-signature:received:received:message-id:date:from :user-agent:mime-version:to:subject:references:in-reply-to :content-type; bh=TpE0DTz8lWbzPCYKlSwVlNHwAmIKuj0AyLl+lubyMws=; b=jY5NrqQlQRBtWFn6e4YYAkEP1q5xpQO7Gix/L/atWESiqCucjfukLYhfW4xhqxTXr5 aZUuQQc5I3JIqASlURmf7CMB+aP5t7p7J7Q6gsCeym+H4TsNXm0N2P/ckWqJCG1L3EHO 1JkmwqGT+RptlaopWcPd+FFGAZTHovoTHMbUI= DomainKey-Signature: a=rsa-sha1; c=nofws; d=gmail.com; s=gamma; h=message-id:date:from:user-agent:mime-version:to:subject:references :in-reply-to:content-type; b=IUUjPOoEkEDRqrB+pjzQMm2cZfI/tigm379SHV0PasyyMRu4BG7xWaZ2UfMiQWcPHU iL+f8ek9iGl/I1lZxSRUYAnDj+lCg51suSnOoF6tAOXp5A797NPri/P6eSOWm+9tKYwq qOGaRSLvo5adzCXs64VUUzDtB4GsRzwrvRrpU= Received: by 10.86.26.11 with SMTP id 11mr4282624fgz.33.1239574789108; Sun, 12 Apr 2009 15:19:49 -0700 (PDT) Received: from ?192.168.1.25? (116-145.62-81.cust.bluewin.ch [81.62.145.116]) by mx.google.com with ESMTPS id 12sm6203194fgg.12.2009.04.12.15.19.48 (version=SSLv3 cipher=RC4-MD5); Sun, 12 Apr 2009 15:19:48 -0700 (PDT) Message-ID: <49E26909.8080701@gmail.com> Date: Mon, 13 Apr 2009 00:19:53 +0200 From: phcoder User-Agent: Thunderbird 2.0.0.21 (X11/20090318) MIME-Version: 1.0 To: The development of GRUB 2 References: <200904111919.49761.okuji@enbug.org> <20090411.044848.183598331.davem@davemloft.net> <49E0EB22.4070308@gmail.com> <20090412.010719.229891360.davem@davemloft.net> <49E1E2BC.3020405@gmail.com> <1239572015.14481.32.camel@ct> <1239573107.14481.41.camel@ct> In-Reply-To: <1239573107.14481.41.camel@ct> Content-Type: multipart/mixed; boundary="------------030904010105060405010507" X-detected-operating-system: by monty-python.gnu.org: GNU/Linux 2.6 (newer, 2) Subject: Re: [PATCH]: grub: Fix handling of long printf arguments on 64-bit. X-BeenThere: grub-devel@gnu.org X-Mailman-Version: 2.1.5 Precedence: list Reply-To: The development of GRUB 2 List-Id: The development of GRUB 2 List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Sun, 12 Apr 2009 22:19:57 -0000 This is a multi-part message in MIME format. --------------030904010105060405010507 Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 7bit I already understood what you meant in first mail. Sorry for not paying attention to this detail. Here is my proposition. IT decreases the size from 31224 to 31068 bytes. I tested it with following input grub_printf ("Hello World: %d %ld %lld %x %lx %llx %u %lu %llu\n", 0xffffffff, 0xffffffff, 0xffffffffffffffffLL, 0xffffffff, 0xffffffff, 0xffffffffffffffffLL, 0xffffffff, 0xffffffff, 0xffffffffffffffffLL); grub_printf ("Hello World: %d %ld %lld %x %lx %llx %u %lu %llu\n", 0x0fffffff, 0x0fffffff, 0x0fffffffffffffffLL, 0x0fffffff, 0x0fffffff, 0x0fffffffffffffffLL, 0x0fffffff, 0x0fffffff, 0x0fffffffffffffffLL); Output was: Hello World: -1 -1 -1 ffffffff ffffffff ffffffffffffffff 4294967295 4294967295 18446744073709551615 Hello World: 268435455 268435455 1152921504606846975 fffffff fffffff fffffffffffffff 268435455 268435455 1152921504606846975 Pavel Roskin wrote: > On Sun, 2009-04-12 at 17:33 -0400, Pavel Roskin wrote: > >> If the type is going to be converted, we need to treat signed and >> unsigned values separately. > > This is compile tested only, to show what I mean. The size of > kernel.img is reduced from 31220 to 31124. The code looks long, but I'm > sure an optimizing compiler can simplify it well. Variable names are > changed for readability. > > Index: kern/misc.c > =================================================================== > --- kern/misc.c (revision 2087) > +++ kern/misc.c (working copy) > @@ -565,56 +565,6 @@ > } > } > > -static char * > -grub_itoa (char *str, int c, unsigned n) > -{ > - unsigned base = (c == 'x') ? 16 : 10; > - char *p; > - > - if ((int) n < 0 && c == 'd') > - { > - n = (unsigned) (-((int) n)); > - *str++ = '-'; > - } > - > - p = str; > - do > - { > - unsigned d = n % base; > - *p++ = (d > 9) ? d + 'a' - 10 : d + '0'; > - } > - while (n /= base); > - *p = 0; > - > - grub_reverse (str); > - return p; > -} > - > -static char * > -grub_ltoa (char *str, int c, unsigned long n) > -{ > - unsigned long base = (c == 'x') ? 16 : 10; > - char *p; > - > - if ((long) n < 0 && c == 'd') > - { > - n = (unsigned long) (-((long) n)); > - *str++ = '-'; > - } > - > - p = str; > - do > - { > - unsigned long d = n % base; > - *p++ = (d > 9) ? d + 'a' - 10 : d + '0'; > - } > - while (n /= base); > - *p = 0; > - > - grub_reverse (str); > - return p; > -} > - > /* Divide N by D, return the quotient, and store the remainder in *R. */ > grub_uint64_t > grub_divmod64 (grub_uint64_t n, grub_uint32_t d, grub_uint32_t *r) > @@ -807,23 +757,36 @@ > /* fall through */ > case 'x': > case 'u': > + if (longlongfmt) > + { > + unsigned long long ull = va_arg (args, unsigned long long); > + grub_lltoa (tmp, c, ull); > + } > + else if (longfmt) > + { > + unsigned long ul = va_arg (args, unsigned long); > + grub_lltoa (tmp, c, ul); > + } > + else > + { > + unsigned int ui = va_arg (args, unsigned int); > + grub_lltoa (tmp, c, ui); > + } > case 'd': > if (longlongfmt) > { > - long long ll; > - > - ll = va_arg (args, long long); > - grub_lltoa (tmp, c, ll); > + long long sll = va_arg (args, long long); > + grub_lltoa (tmp, c, sll); > } > else if (longfmt) > { > - long l = va_arg (args, long); > - grub_ltoa (tmp, c, l); > + long sl = va_arg (args, long); > + grub_lltoa (tmp, c, sl); > } > else > { > - n = va_arg (args, int); > - grub_itoa (tmp, c, n); > + int si = va_arg (args, int); > + grub_lltoa (tmp, c, si); > } > if (! rightfill && grub_strlen (tmp) < format1) > write_fill (zerofill, format1 - grub_strlen (tmp)); > > -- Regards Vladimir 'phcoder' Serbinenko --------------030904010105060405010507 Content-Type: text/x-diff; name="lltoa.diff" Content-Transfer-Encoding: 7bit Content-Disposition: inline; filename="lltoa.diff" diff --git a/ChangeLog b/ChangeLog index 237c6f4..d701519 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,11 @@ +2009-04-13 Vladimir Serbinenko + + use grub_lltoa instead of grub_itoa and grub_ltoa for all purposes + + * kern/misc.c (grub_itoa): Removed function + (grub_ltoa): likewise + (grub_vsprintf): use grub_lltoa + 2009-04-12 David S. Miller * kern/misc.c (grub_ltoa): Fix cast when handling negative diff --git a/kern/misc.c b/kern/misc.c index 5a12535..4c71ca8 100644 --- a/kern/misc.c +++ b/kern/misc.c @@ -565,56 +565,6 @@ grub_reverse (char *str) } } -static char * -grub_itoa (char *str, int c, unsigned n) -{ - unsigned base = (c == 'x') ? 16 : 10; - char *p; - - if ((int) n < 0 && c == 'd') - { - n = (unsigned) (-((int) n)); - *str++ = '-'; - } - - p = str; - do - { - unsigned d = n % base; - *p++ = (d > 9) ? d + 'a' - 10 : d + '0'; - } - while (n /= base); - *p = 0; - - grub_reverse (str); - return p; -} - -static char * -grub_ltoa (char *str, int c, unsigned long n) -{ - unsigned long base = (c == 'x') ? 16 : 10; - char *p; - - if ((long) n < 0 && c == 'd') - { - n = (unsigned long) (-((long) n)); - *str++ = '-'; - } - - p = str; - do - { - unsigned long d = n % base; - *p++ = (d > 9) ? d + 'a' - 10 : d + '0'; - } - while (n /= base); - *p = 0; - - grub_reverse (str); - return p; -} - /* Divide N by D, return the quotient, and store the remainder in *R. */ grub_uint64_t grub_divmod64 (grub_uint64_t n, grub_uint32_t d, grub_uint32_t *r) @@ -746,6 +696,7 @@ grub_vsprintf (char *str, const char *fmt, va_list args) int n; int longfmt = 0; int longlongfmt = 0; + int unsig = 0; if (*fmt && *fmt =='-') { @@ -804,9 +755,11 @@ grub_vsprintf (char *str, const char *fmt, va_list args) write_str ("0x"); c = 'x'; longlongfmt |= (sizeof (void *) == sizeof (long long)); - /* fall through */ + /* Fall through. */ case 'x': case 'u': + unsig = 1; + /* Fall through. */ case 'd': if (longlongfmt) { @@ -815,15 +768,25 @@ grub_vsprintf (char *str, const char *fmt, va_list args) ll = va_arg (args, long long); grub_lltoa (tmp, c, ll); } + else if (longfmt && unsig) + { + unsigned long l = va_arg (args, unsigned long); + grub_lltoa (tmp, c, l); + } else if (longfmt) { long l = va_arg (args, long); - grub_ltoa (tmp, c, l); + grub_lltoa (tmp, c, l); + } + else if (unsig) + { + unsigned u = va_arg (args, unsigned); + grub_lltoa (tmp, c, u); } else { n = va_arg (args, int); - grub_itoa (tmp, c, n); + grub_lltoa (tmp, c, n); } if (! rightfill && grub_strlen (tmp) < format1) write_fill (zerofill, format1 - grub_strlen (tmp)); --------------030904010105060405010507--