Skip to contentSkip to conversation

fix print_size printing fractional gigabyte numbers on 32-bit platforms

PATCH New Timur Tabi 2 participants 6 messages Apr 13, 2010 – May 5, 2010 Library code

v2 of 3 New Latest is v3 →

lore
Share & tools
lore

Timur Tabi

author
#0 lore raw

PATCH v2 1/2 fix print_size printing fractional gigabyte numbers on 32-bit platforms

In print_size(), the math that calculates the fractional remainder of a number used the same integer size as a physical address. However, the "10 *" factor of the algorithm means that a large number (e.g. 1.5GB) can overflow the integer if we're running on a 32-bit system. Therefore, we need to disassociate this function from the size of a physical address.

---
 lib/display_options.c |    4 ++--
 1 files changed, 2 insertions(+), 2 deletions(-)
lib/display_options.c +2 -2
index 2dc2567..08a7914 100644
@@ -45,8 +45,8 @@ int display_options (void)
  */
 void print_size (phys_size_t size, const char *s)
 {
-	ulong m = 0, n;
-	phys_size_t d = 1 << 30;		/* 1 GB */
+	unsigned long m = 0, n;
+	unsigned long long d = 1 << 30; 	/* 1 GB */
 	char  c = 'G';
 
 	if (size < d) {			/* try MB */
Signature · 1 line
1.6.5
author
#1 lore raw

PATCH v6 2/2 allow print_size to print large numbers on 32-bit systems

Modify print_size() so that it can accept numbers larger than 4GB on 32-bit systems.

Add support for display terabyte, petabyte, and exabyte sizes. Change the output to use International Electrotechnical Commission binary prefix standard.

---
 include/common.h      |    2 +-
 lib/display_options.c |   31 ++++++++++++++++++-------------
 2 files changed, 19 insertions(+), 14 deletions(-)
include/common.h +1 -1
index df956bb..5591e1d 100644
@@ -218,7 +218,7 @@ void	hang		(void) __attribute__ ((noreturn));
 /* */
 phys_size_t initdram (int);
 int	display_options (void);
-void	print_size (phys_size_t, const char *);
+void	print_size(unsigned long long, const char *);
 int	print_buffer (ulong addr, void* data, uint width, uint count, uint linelen);
 
 /* common/main.c */
lib/display_options.c +19 -14
index 08a7914..86df05d 100644
@@ -39,25 +39,30 @@ int display_options (void)
 }
 
 /*
- * print sizes as "xxx kB", "xxx.y kB", "xxx MB", "xxx.y MB",
- * xxx GB, or xxx.y GB as needed; allow for optional trailing string
+ * print sizes as "xxx KiB", "xxx.y KiB", "xxx MiB", "xxx.y MiB",
+ * xxx GiB, xxx.y GiB, etc as needed; allow for optional trailing string
  * (like "\n")
  */
-void print_size (phys_size_t size, const char *s)
+void print_size(unsigned long long size, const char *s)
 {
 	unsigned long m = 0, n;
-	unsigned long long d = 1 << 30; 	/* 1 GB */
-	char  c = 'G';
-
-	if (size < d) {			/* try MB */
-		c = 'M';
-		d = 1 << 20;
-		if (size < d) {		/* print in kB */
-			c = 'k';
-			d = 1 << 10;
+	static const char names[] = {'E', 'P', 'T', 'G', 'M', 'K'};
+	unsigned long long d = 1ULL << (10 * ARRAY_SIZE(names));
+	char c = 0;
+	unsigned int i;
+
+	for (i = 0; i < ARRAY_SIZE(names); i++, d >>= 10) {
+		if (size >= d) {
+			c = names[i];
+			break;
 		}
 	}
 
+	if (!c) {
+		printf("%llu Bytes%s", size, s);
+		return;
+	}
+
 	n = size / d;
 
 	/* If there's a remainder, deal with it */
@@ -70,11 +75,11 @@ void print_size (phys_size_t size, const char *s)
 		}
 	}
 
-	printf ("%2ld", n);
+	printf ("%lu", n);
 	if (m) {
 		printf (".%ld", m);
 	}
-	printf (" %cB%s", c, s);
+	printf (" %ciB%s", c, s);
 }
 
 /*
Signature · 1 line
1.6.5
author
#2 lore raw

PATCH v6 2/2 allow print_size to print large numbers on 32-bit systems

Wolfgang, if you have no objections, please apply this patch.

Quoted 8 lines · Timur Tabi

Modify print_size() so that it can accept numbers larger than 4GB on 32-bit systems.

Add support for display terabyte, petabyte, and exabyte sizes. ?Change the output to use International Electrotechnical Commission binary prefix standard.

---

Signature · 2 lines
Timur Tabi
Linux kernel developer at Freescale
#3 lore raw

PATCH v6 2/2 allow print_size to print large numbers on 32-bit systems

In reply to Timur Tabi

Dear Timur Tabi,

Quoted 11 lines · In message <[email protected]> you wrote:

Modify print_size() so that it can accept numbers larger than 4GB on 32-bit systems.

Add support for display terabyte, petabyte, and exabyte sizes. Change the output to use International Electrotechnical Commission binary prefix standard.

---
 include/common.h      |    2 +-
 lib/display_options.c |   31 ++++++++++++++++++-------------
 2 files changed, 19 insertions(+), 14 deletions(-)

Applied, thanks.

Best regards,

Wolfgang Denk

Signature · 6 lines
DENX Software Engineering GmbH,     MD: Wolfgang Denk & Detlev Zundel
HRB 165235 Munich, Office: Kirchenstr.5, D-82194 Groebenzell, Germany
Phone: (+49)-8142-66989-10 Fax: (+49)-8142-66989-80 Email: wd at denx.de
The years of peak mental activity are undoubtedly between the ages of
four and eighteen. At four we know all the questions, at eighteen all
the answers.
author
#4 lore raw

PATCH v2 1/2 fix print_size printing fractional gigabyte numbers on 32-bit platforms

In reply to Timur Tabi

Wolfgang, if you have no objections, please apply this patch.

Quoted 8 lines · Timur Tabi

In print_size(), the math that calculates the fractional remainder of a number used the same integer size as a physical address. ?However, the "10 *" factor of the algorithm means that a large number (e.g. 1.5GB) can overflow the integer if we're running on a 32-bit system. ?Therefore, we need to disassociate this function from the size of a physical address.

---

Signature · 2 lines
Timur Tabi
Linux kernel developer at Freescale
#5 lore raw

PATCH v2 1/2 fix print_size printing fractional gigabyte numbers on 32-bit platforms

In reply to Timur Tabi

Dear Timur Tabi,

Quoted 10 lines · In message <[email protected]> you wrote:

In print_size(), the math that calculates the fractional remainder of a number used the same integer size as a physical address. However, the "10 *" factor of the algorithm means that a large number (e.g. 1.5GB) can overflow the integer if we're running on a 32-bit system. Therefore, we need to disassociate this function from the size of a physical address.

---
 lib/display_options.c |    4 ++--
 1 files changed, 2 insertions(+), 2 deletions(-)

Applied, thanks.

Best regards,

Wolfgang Denk

Signature · 5 lines
DENX Software Engineering GmbH,     MD: Wolfgang Denk & Detlev Zundel
HRB 165235 Munich, Office: Kirchenstr.5, D-82194 Groebenzell, Germany
Phone: (+49)-8142-66989-10 Fax: (+49)-8142-66989-80 Email: wd at denx.de
I am not now, nor have I ever been, a member of the demigodic party.
                                                   -- Dennis Ritchie