Skip to contentSkip to conversation

tools/envcrc.c: use the target sizeof rather than build

PATCH New Mike Frysinger 4 participants 14 messages Mar 30, 2008 – Apr 24, 2008 Kbuild

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 tools/envcrc.c: use the target sizeof rather than build

The envcrc.c does sizeof(unsigned long) when calculating the crc, but this is done with the build toolchain instead of the target toolchain, so if the build is a 64bit system but the target is 32bits, the size will obviously be wrong. This introduces a sizeof.sh script to calculate the required sizeof() value of any type with any compiler.

---
 tools/Makefile  |    2 +-
 tools/envcrc.c  |    4 ++--
 tools/sizeof.sh |   35 +++++++++++++++++++++++++++++++++++
 3 files changed, 38 insertions(+), 3 deletions(-)
 create mode 100755 tools/sizeof.sh
tools/Makefile +1 -1
index 8784a6d..7351f28 100644
@@ -168,7 +168,7 @@ $(obj)mpc86x_clk$(SFX):	$(obj)mpc86x_clk.o
 		$(STRIP) $@
 
 $(obj)envcrc.o:	$(src)envcrc.c
-		$(CC) -g $(CFLAGS) -c -o $@ $<
+		$(CC) -g $(CFLAGS) -c -o $@ $< -DTARGET_SIZEOF_ULONG=$(shell $(CONFIG_SHELL) $(src)sizeof.sh unsigned long)
 
 $(obj)ubsha1.o:	$(src)ubsha1.c
 		$(CC) -g $(CFLAGS) -c -o $@ $<
tools/envcrc.c +2 -2
index 7b77183..afde912 100644
@@ -58,9 +58,9 @@
 #endif	/* CFG_ENV_IS_IN_FLASH */
 
 #ifdef CFG_REDUNDAND_ENVIRONMENT
-# define ENV_HEADER_SIZE	(sizeof(unsigned long) + 1)
+# define ENV_HEADER_SIZE	(TARGET_SIZEOF_ULONG + 1)
 #else
-# define ENV_HEADER_SIZE	(sizeof(unsigned long))
+# define ENV_HEADER_SIZE	(TARGET_SIZEOF_ULONG)
 #endif
 
 #define ENV_SIZE (CFG_ENV_SIZE - ENV_HEADER_SIZE)
tools/sizeof.sh new file +35 -0
new file mode 100755
index 0000000..e17596c
@@ -0,0 +1,35 @@
+#!/bin/sh
+# Get the sizeof(type) for a compiler
+
+type="$*"
+CC=${CC:-gcc}
+
+if [ -z "$type" ] ; then
+	echo "Usage: sizeof.sh <type>" 1>&2
+	exit 1
+fi
+
+test_size() {
+	cat <<-EOF > sizeof.c
+	typedef $type ac__type_sizeof_;
+	int main() {
+		static int test_array [1 - 2 * !(((long int) (sizeof (ac__type_sizeof_))) <= $size)];
+		test_array [0] = 0;
+		return 0;
+	}
+	EOF
+	${CC} -c sizeof.c >/dev/null 2>&1
+	ret=$?
+	rm -f sizeof.[co]
+	return $ret
+}
+
+size=0
+while [ ${size} -lt 64 ] ; do
+	if test_size ${size} ; then
+		echo ${size}
+		exit 0
+	fi
+	((size+=1))
+done
+exit 1
Signature · 1 line
1.5.4.4
#1 lore raw

PATCH 1/1 tools/envcrc.c: use the target sizeof rather than build

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

The envcrc.c does sizeof(unsigned long) when calculating the crc, but this is done with the build toolchain instead of the target toolchain, so if the build is a 64bit system but the target is 32bits, the size will obviously be wrong. This introduces a sizeof.sh script to calculate the required sizeof() value of any type with any compiler.

It seems this script requires up to 64 runs of the C compiler. I consider this pretty expensive. Isn't there a cheaper way to do that?

Consider the simple logic used to decide between 32 versus 64 bit pointer types - shouldn't this be sufficient here, too?

Quoted 18 lines
tools/sizeof.sh new file +15 -0
@@ -0,0 +1,35 @@
+#!/bin/sh
+# Get the sizeof(type) for a compiler
+
+type="$*"
+CC=${CC:-gcc}
+
+if [ -z "$type" ] ; then
+	echo "Usage: sizeof.sh <type>" 1>&2
+	exit 1
+fi
+
+test_size() {
+	cat <<-EOF > sizeof.c
+	typedef $type ac__type_sizeof_;
+	int main() {

tools/sizeof.sh line 25

Quoted 10 lines
tools/sizeof.sh new file +10 -0
+		static int test_array [1 - 2 * !(((long int) (sizeof (ac__type_sizeof_))) <= $size)];
+		test_array [0] = 0;
+		return 0;
+	}
+	EOF
+	${CC} -c sizeof.c >/dev/null 2>&1
+	ret=$?
+	rm -f sizeof.[co]
+	return $ret
+}

Are there no Copyrights / License conditions attached to this code? Where exactly is it coming from?

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
...when fits of creativity run strong, more than  one  programmer  or
writer  has  been  known to abandon the desktop for the more spacious
floor.                                             - Fred Brooks, Jr.
#2 lore raw

PATCH 1/1 tools/envcrc.c: use the target sizeof rather than build

Quoted 9 lines · Wolfgang Denk
Quoted 5 lines · In message <[email protected]> you wrote:

The envcrc.c does sizeof(unsigned long) when calculating the crc, but this is done with the build toolchain instead of the target toolchain, so if the build is a 64bit system but the target is 32bits, the size will obviously be wrong. This introduces a sizeof.sh script to calculate the required sizeof() value of any type with any compiler.

It seems this script requires up to 64 runs of the C compiler. I consider this pretty expensive. Isn't there a cheaper way to do that?

if the type you're testing for is 64bytes, then yes it'll run that many times, as it should. i guess it should be even larger though considering there are types out there that are 128bytes or bigger.

but 64 is the upper limit before the script gives up. it starts at 0 and counts up. so on a normal run, it'll execute either 4 or 8 times before it gets success.

Quoted 2 lines

Consider the simple logic used to decide between 32 versus 64 bit pointer types - shouldn't this be sufficient here, too?

the script can be used to calculate any type. i imagine i could make it increment by 4 and then count down to find the limit, but i dont really think the savings is worth while.

Quoted 23 lines
Quoted 23 lines
tools/sizeof.sh new file +16 -0
@@ -0,0 +1,35 @@
+#!/bin/sh
+# Get the sizeof(type) for a compiler
+
+type="$*"
+CC=${CC:-gcc}
+
+if [ -z "$type" ] ; then
+	echo "Usage: sizeof.sh <type>" 1>&2
+	exit 1
+fi
+
+test_size() {
+	cat <<-EOF > sizeof.c
+	typedef $type ac__type_sizeof_;
+	int main() {
+		static int test_array [1 - 2 * !(((long int) (sizeof

(ac__type_sizeof_))) <= $size)]; + test_array [0] = 0;

tools/sizeof.sh new file +3 -0
+		return 0;
+	}
+	EOF
Quoted 8 lines

tools/sizeof.sh line 25

Quoted 5 lines
tools/sizeof.sh new file +5 -0
+	${CC} -c sizeof.c >/dev/null 2>&1
+	ret=$?
+	rm -f sizeof.[co]
+	return $ret
+}

Are there no Copyrights / License conditions attached to this code? Where exactly is it coming from?

i wrote it. the one magic line (the test_array) is based on code from autoconf, so i guess it'll be GPL-2.
-mike
-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: application/pgp-signature
Size: 827 bytes
Desc: This is a digitally signed message part.
Url : http://lists.denx.de/pipermail/u-boot/attachments/20080330/b11bd4d5/attachment.pgp

#3 lore raw

PATCH v2 1/1 tools/envcrc.c: use the target sizeof rather than build

In reply to Mike Frysinger

The envcrc.c does sizeof(unsigned long) when calculating the crc, but this is done with the build toolchain instead of the target toolchain, so if the build is a 64bit system but the target is 32bits, the size will obviously be wrong. This introduces a sizeof.sh script to calculate the required sizeof() value of any type with any compiler.

---
Changes:
- adds copyright/license
- increase limit to 512
- increment by 4 bytes to find "32bit naturals" faster:
(old/new # = number of times gcc is called)
type | sizeof | old | new
char | 1 | 1 | 5
short | 2 | 2 | 4
int | 4 | 4 | 2
long | 8 | 8 | 3
double | 8 | 8 | 3

---
 tools/Makefile  |    2 +-
 tools/envcrc.c  |    4 ++--
 tools/sizeof.sh |   45 +++++++++++++++++++++++++++++++++++++++++++++
 3 files changed, 48 insertions(+), 3 deletions(-)
 create mode 100755 tools/sizeof.sh
tools/Makefile +1 -1
index 8784a6d..7351f28 100644
@@ -168,7 +168,7 @@ $(obj)mpc86x_clk$(SFX):	$(obj)mpc86x_clk.o
 		$(STRIP) $@
 
 $(obj)envcrc.o:	$(src)envcrc.c
-		$(CC) -g $(CFLAGS) -c -o $@ $<
+		$(CC) -g $(CFLAGS) -c -o $@ $< -DTARGET_SIZEOF_ULONG=$(shell $(CONFIG_SHELL) $(src)sizeof.sh unsigned long)
 
 $(obj)ubsha1.o:	$(src)ubsha1.c
 		$(CC) -g $(CFLAGS) -c -o $@ $<
tools/envcrc.c +2 -2
index 7b77183..afde912 100644
@@ -58,9 +58,9 @@
 #endif	/* CFG_ENV_IS_IN_FLASH */
 
 #ifdef CFG_REDUNDAND_ENVIRONMENT
-# define ENV_HEADER_SIZE	(sizeof(unsigned long) + 1)
+# define ENV_HEADER_SIZE	(TARGET_SIZEOF_ULONG + 1)
 #else
-# define ENV_HEADER_SIZE	(sizeof(unsigned long))
+# define ENV_HEADER_SIZE	(TARGET_SIZEOF_ULONG)
 #endif
 
 #define ENV_SIZE (CFG_ENV_SIZE - ENV_HEADER_SIZE)
tools/sizeof.sh new file +45 -0

45 lines not on this page · raw message

Signature · 1 line
1.5.4.4
#4 lore raw

PATCH v2 1/1 tools/envcrc.c: use the target sizeof rather than build

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

The envcrc.c does sizeof(unsigned long) when calculating the crc, but this is done with the build toolchain instead of the target toolchain, so if the build is a 64bit system but the target is 32bits, the size will obviously be wrong. This introduces a sizeof.sh script to calculate the required sizeof() value of any type with any compiler.

Quoted 12 lines

---
Changes:
- adds copyright/license
- increase limit to 512
- increment by 4 bytes to find "32bit naturals" faster:
(old/new # = number of times gcc is called)
type | sizeof | old | new
char | 1 | 1 | 5
short | 2 | 2 | 4
int | 4 | 4 | 2
long | 8 | 8 | 3
double | 8 | 8 | 3

Sorry, but I still think this is serious overkill which just slows down the build process.

We need exactly one single case of this, i. e. sizeof(long). And instead of tricking around all this here, I think we should rather use somthing like "u32" in the code.

Best regards,

Wolfgang Denk

Signature · 4 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
Save yourself!  Reboot in 5 seconds!
#5 lore raw

PATCH v2 1/1 tools/envcrc.c: use the target sizeof rather than build

Quoted 21 lines · Wolfgang Denk
Quoted 7 lines · In message <[email protected]> you wrote:

The envcrc.c does sizeof(unsigned long) when calculating the crc, but this is done with the build toolchain instead of the target toolchain, so if the build is a 64bit system but the target is 32bits, the size will obviously be wrong. This introduces a sizeof.sh script to calculate the required sizeof() value of any type with any compiler.

Quoted 12 lines

---
Changes:
- adds copyright/license
- increase limit to 512
- increment by 4 bytes to find "32bit naturals" faster:
(old/new # = number of times gcc is called)
type | sizeof | old | new
char | 1 | 1 | 5
short | 2 | 2 | 4
int | 4 | 4 | 2
long | 8 | 8 | 3
double | 8 | 8 | 3

Quoted 2 lines

Sorry, but I still think this is serious overkill which just slows down the build process.

two or three executions of gcc will not affect the overall build speed in any way (which is what i proposed).

Quoted 1 line

We need exactly one single case of this, i. e. sizeof(long).

which is what you get. i posted the numbers only as reference, not because gcc actually gets executed that many times.

Quoted 3 lines

And
instead of tricking around all this here, I think we should rather use somthing like "u32" in the code.

if the value in question represents a CRC32, then that's what should have been used in the first place ...
-mike
-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: application/pgp-signature
Size: 827 bytes
Desc: This is a digitally signed message part.
Url : http://lists.denx.de/pipermail/u-boot/attachments/20080331/d371697a/attachment.pgp

#6 lore raw

PATCH v2 1/1 tools/envcrc.c: use the target sizeof rather than build

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

And
instead of tricking around all this here, I think we should rather use somthing like "u32" in the code.

if the value in question represents a CRC32, then that's what should have been used in the first place ...

Indeed. So please let's focus on fixing the cause of the problem instead of adding scripts to work around it, slowly.

Best regards,

Wolfgang Denk

Signature · 4 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
Never put off until tomorrow what you can put off indefinitely.
#7 lore raw

PATCH v2 1/1 tools/envcrc.c: use the target sizeof rather than build

Quoted 10 lines · Wolfgang Denk
Quoted 6 lines · In message <[email protected]> you wrote:
Quoted 3 lines

And
instead of tricking around all this here, I think we should rather use somthing like "u32" in the code.

if the value in question represents a CRC32, then that's what should have been used in the first place ...

Indeed. So please let's focus on fixing the cause of the problem instead of adding scripts to work around it, slowly.

check your inbox ;)
-mike
-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: application/pgp-signature
Size: 827 bytes
Desc: This is a digitally signed message part.
Url : http://lists.denx.de/pipermail/u-boot/attachments/20080331/51a6be23/attachment.pgp

#8 lore raw

PATCH v3 1/1 crc32: use uint32_t rather than unsigned long

In reply to Mike Frysinger

The envcrc.c does sizeof(unsigned long) when calculating the crc, but this is done with the build toolchain instead of the target toolchain, so if the build is a 64bit system but the target is 32bits, the size will obviously be wrong. This converts all unsigned long stuff related to crc32 to uint32_t types. Compile tested only: output of ./tools/envcrc when run on a 32bit build system matches that of a 64bit build system.

---
 Makefile              |    2 +-
 common/image.c        |    2 +-
 include/common.h      |    4 ++--
 include/environment.h |   11 ++++++++---
 lib_generic/crc32.c   |   19 ++++++++++---------
 tools/Makefile        |    6 +++++-
 tools/envcrc.c        |    9 +++++----
 7 files changed, 32 insertions(+), 21 deletions(-)
Makefile +1 -1
index 4255cf5..59d09bd 100644
@@ -2891,7 +2891,7 @@ clobber:	clean
 		$(obj)cscope.* $(obj)*.*~
 	@rm -f $(obj)u-boot $(obj)u-boot.map $(obj)u-boot.hex $(ALL)
 	@rm -f $(obj)tools/{crc32.c,environment.c,env/crc32.c,md5.c,sha1.c,inca-swap-bytes}
-	@rm -f $(obj)tools/{image.c,fdt.c,fdt_ro.c,fdt_rw.c,fdt_strerror.c}
+	@rm -f $(obj)tools/{image.c,fdt.c,fdt_ro.c,fdt_rw.c,fdt_strerror.c,zlib.h}
 	@rm -f $(obj)tools/{fdt_wip.c,libfdt_internal.h}
 	@rm -f $(obj)cpu/mpc824x/bedbug_603e.c
 	@rm -f $(obj)include/asm/proc $(obj)include/asm/arch $(obj)include/asm
common/image.c +1 -1
index f04826a..6ade7b0 100644
@@ -155,7 +155,7 @@ static table_entry_t uimage_comp[] = {
 	{	-1,		"",		"",			},
 };
 
-unsigned long crc32 (unsigned long, const unsigned char *, unsigned int);
+uint32_t crc32 (uint32_t, const unsigned char *, uint);
 static void genimg_print_size (uint32_t size);
 #if defined(CONFIG_TIMESTAMP) || defined(CONFIG_CMD_DATE) || defined(USE_HOSTCC)
 static void genimg_print_time (time_t timestamp);
include/common.h +2 -2
index 39bcd30..0ea60ba 100644
@@ -603,8 +603,8 @@ int	sprintf(char * buf, const char *fmt, ...);
 int	vsprintf(char *buf, const char *fmt, va_list args);
 
 /* lib_generic/crc32.c */
-ulong crc32 (ulong, const unsigned char *, uint);
-ulong crc32_no_comp (ulong, const unsigned char *, uint);
+uint32_t crc32 (uint32_t, const unsigned char *, uint);
+uint32_t crc32_no_comp (uint32_t, const unsigned char *, uint);
 
 /* common/console.c */
 int	console_init_f(void);	/* Before relocation; uses the serial  stuff	*/
include/environment.h +8 -3
index af605ab..c4f7c33 100644
@@ -84,18 +84,23 @@
 # endif
 #endif /* CFG_ENV_IS_IN_NAND */
 
+#ifdef USE_HOSTCC
+# include <stdint.h>
+#else
+# include <linux/types.h>
+#endif
 
 #ifdef CFG_REDUNDAND_ENVIRONMENT
-# define ENV_HEADER_SIZE	(sizeof(unsigned long) + 1)
+# define ENV_HEADER_SIZE	(sizeof(uint32_t) + 1)
 #else
-# define ENV_HEADER_SIZE	(sizeof(unsigned long))
+# define ENV_HEADER_SIZE	(sizeof(uint32_t))
 #endif
 
 
 #define ENV_SIZE (CFG_ENV_SIZE - ENV_HEADER_SIZE)
 
 typedef	struct environment_s {
-	unsigned long	crc;		/* CRC32 over data bytes	*/
+	uint32_t	crc;		/* CRC32 over data bytes	*/
 #ifdef CFG_REDUNDAND_ENVIRONMENT
 	unsigned char	flags;		/* active/obsolete flags	*/
 #endif
lib_generic/crc32.c +10 -9
index df0dbca..b882daa 100644
@@ -10,18 +10,19 @@
 
 #ifndef USE_HOSTCC	/* Shut down "ANSI does not permit..." warnings */
 #include <common.h>
+#else
+#include <stdint.h>
 #endif
 
 #include "zlib.h"
 
 #define local static
 #define ZEXPORT	/* empty */
-unsigned long crc32 (unsigned long, const unsigned char *, unsigned int);
 
 #ifdef DYNAMIC_CRC_TABLE
 
 local int crc_table_empty = 1;
-local uLongf crc_table[256];
+local uint32_t crc_table[256];
 local void make_crc_table OF((void));
 
 /*
@@ -50,7 +51,7 @@ local void make_crc_table OF((void));
 */
 local void make_crc_table()
 {
-  uLong c;
+  uint32_t c;
   int n, k;
   uLong poly;            /* polynomial exclusive-or pattern */
   /* terms of polynomial defining this crc (except x^32): */
@@ -74,7 +75,7 @@ local void make_crc_table()
 /* ========================================================================
  * Table of CRC-32's of all single-byte values (made by make_crc_table)
  */
-local const uLongf crc_table[256] = {
+local const uint32_t crc_table[256] = {
   0x00000000L, 0x77073096L, 0xee0e612cL, 0x990951baL, 0x076dc419L,
   0x706af48fL, 0xe963a535L, 0x9e6495a3L, 0x0edb8832L, 0x79dcb8a4L,
   0xe0d5e91eL, 0x97d2d988L, 0x09b64c2bL, 0x7eb17cbdL, 0xe7b82d07L,
@@ -134,12 +135,12 @@ local const uLongf crc_table[256] = {
 /* =========================================================================
  * This function can be used by asm versions of crc32()
  */
-const uLongf * ZEXPORT get_crc_table()
+const uint32_t * ZEXPORT get_crc_table()
 {
 #ifdef DYNAMIC_CRC_TABLE
   if (crc_table_empty) make_crc_table();
 #endif
-  return (const uLongf *)crc_table;
+  return (const uint32_t *)crc_table;
 }
 #endif
 
@@ -150,8 +151,8 @@ const uLongf * ZEXPORT get_crc_table()
 #define DO8(buf)  DO4(buf); DO4(buf);
 
 /* ========================================================================= */
-uLong ZEXPORT crc32(crc, buf, len)
-    uLong crc;
+uint32_t ZEXPORT crc32(crc, buf, len)
+    uint32_t crc;
     const Bytef *buf;
     uInt len;
 {
@@ -178,7 +179,7 @@ uLong ZEXPORT crc32(crc, buf, len)
 /* No ones complement version. JFFS2 (and other things ?)
  * don't use ones compliment in their CRC calculations.
  */
-uLong ZEXPORT crc32_no_comp(uLong crc, const Bytef *buf, uInt len)
+uint32_t ZEXPORT crc32_no_comp(uint32_t crc, const Bytef *buf, uInt len)
 {
 #ifdef DYNAMIC_CRC_TABLE
     if (crc_table_empty)
tools/Makefile +5 -1
index 8784a6d..b897923 100644
@@ -236,7 +236,11 @@ $(obj)environment.c:
 $(obj)environment.o:	$(obj)environment.c
 		$(CC) -g $(HOST_ENVIRO_CFLAGS) $(CPPFLAGS) -c -o $@ $<
 
-$(obj)crc32.c:
+$(obj)zlib.h:
+		@rm -f $@
+		ln -s $(src)../include/zlib.h $@
+
+$(obj)crc32.c: $(obj)zlib.h
 		@rm -f $(obj)crc32.c
 		ln -s $(src)../lib_generic/crc32.c $(obj)crc32.c
 
tools/envcrc.c +5 -4
index 7b77183..550cf82 100644
@@ -22,6 +22,7 @@
  */
 
 #include <stdio.h>
+#include <stdint.h>
 #include <stdlib.h>
 #include <unistd.h>
 
@@ -58,15 +59,15 @@
 #endif	/* CFG_ENV_IS_IN_FLASH */
 
 #ifdef CFG_REDUNDAND_ENVIRONMENT
-# define ENV_HEADER_SIZE	(sizeof(unsigned long) + 1)
+# define ENV_HEADER_SIZE	(sizeof(uint32_t) + 1)
 #else
-# define ENV_HEADER_SIZE	(sizeof(unsigned long))
+# define ENV_HEADER_SIZE	(sizeof(uint32_t))
 #endif
 
 #define ENV_SIZE (CFG_ENV_SIZE - ENV_HEADER_SIZE)
 
 
-extern unsigned long crc32 (unsigned long, const unsigned char *, unsigned int);
+extern uint32_t crc32 (uint32_t, const unsigned char *, unsigned int);
 
 #ifdef	ENV_IS_EMBEDDED
 extern unsigned int env_size;
@@ -76,7 +77,7 @@ extern unsigned char environment;
 int main (int argc, char **argv)
 {
 #ifdef	ENV_IS_EMBEDDED
-	int crc;
+	uint32_t crc;
 	unsigned char *envptr = &environment,
 		*dataptr = envptr + ENV_HEADER_SIZE;
 	unsigned int datasize = ENV_SIZE;
#9 lore raw

PATCH v3 1/1 crc32: use uint32_t rather than unsigned long

Quoted 6 lines · Mike Frysinger

The envcrc.c does sizeof(unsigned long) when calculating the crc, but this is done with the build toolchain instead of the target toolchain, so if the build is a 64bit system but the target is 32bits, the size will obviously be wrong. This converts all unsigned long stuff related to crc32 to uint32_t types. Compile tested only: output of ./tools/envcrc when run on a 32bit build system matches that of a 64bit build system.

ping
-mike
-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: application/pgp-signature
Size: 827 bytes
Desc: This is a digitally signed message part.
Url : http://lists.denx.de/pipermail/u-boot/attachments/20080423/ec2c46bc/attachment.pgp

#10 lore raw

PATCH v3 1/1 crc32: use uint32_t rather than unsigned long

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

The envcrc.c does sizeof(unsigned long) when calculating the crc, but this is done with the build toolchain instead of the target toolchain, so if the build is a 64bit system but the target is 32bits, the size will obviously be wrong. This converts all unsigned long stuff related to crc32 to uint32_t types. Compile tested only: output of ./tools/envcrc when run on a 32bit build system matches that of a 64bit build system.

ping
-mike

Applied. Thanks for the patch, and thanks for the reminder.

Best regards,

Wolfgang Denk

Signature · 7 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
If a packet hits a pocket on a socket on a port,
And the bus is interrupted as a very last resort,
And the address of the memory makes your floppy disk abort,
Then the socket packet pocket has an error to report! - Ken Burchill?
#11 lore raw

PATCH v3 1/1 crc32: use uint32_t rather than unsigned long

In reply to Mike Frysinger
Quoted 9 lines · Mike Frysinger

The envcrc.c does sizeof(unsigned long) when calculating the crc, but this is done with the build toolchain instead of the target toolchain, so if the build is a 64bit system but the target is 32bits, the size will obviously be wrong. This converts all unsigned long stuff related to crc32 to uint32_t types. Compile tested only: output of ./tools/envcrc when run on a 32bit build system matches that of a 64bit build system.

---

#12 lore raw

PATCH 1/1 tools/envcrc.c: use the target sizeof rather than build

In reply to Mike Frysinger
Quoted 7 lines · Mike Frysinger

The envcrc.c does sizeof(unsigned long) when calculating the crc, but this is done with the build toolchain instead of the target toolchain, so if the build is a 64bit system but the target is 32bits, the size will obviously be wrong. This introduces a sizeof.sh script to calculate the required sizeof() value of any type with any compiler.

[SNIP]

Quoted 31 lines
tools/sizeof.sh new file +25 -0
new file mode 100755
index 0000000..e17596c
@@ -0,0 +1,35 @@
+#!/bin/sh
+# Get the sizeof(type) for a compiler
+
+type="$*"
+CC=${CC:-gcc}
+
+if [ -z "$type" ] ; then
+	echo "Usage: sizeof.sh <type>" 1>&2
+	exit 1
+fi
+
+test_size() {
+	cat <<-EOF > sizeof.c
+	typedef $type ac__type_sizeof_;
+	int main() {
+		static int test_array [1 - 2 * !(((long int) (sizeof (ac__type_sizeof_))) <= $size)];
+		test_array [0] = 0;
+		return 0;
+	}
+	EOF
+	${CC} -c sizeof.c >/dev/null 2>&1
+	ret=$?
+	rm -f sizeof.[co]
+	return $ret
+}

tools/sizeof.sh line 35

Quoted 10 lines
tools/sizeof.sh new file +10 -0
+
+size=0
+while [ ${size} -lt 64 ] ; do
+	if test_size ${size} ; then
+		echo ${size}
+		exit 0
+	fi
+	((size+=1))
+done
+exit 1

Cool script, which reminds me about something: Is it possible to define a script that can output if the cpu is Big Endian or Little Endian?

If one ever wanted to port the optimised crc32 from linux to u-boot one needs to know the endian of the CPU.

Jocke

#13 lore raw

PATCH 1/1 tools/envcrc.c: use the target sizeof rather than build

Quoted 3 lines · Joakim Tjernlund

Cool script, which reminds me about something: Is it possible to define a script that can output if the cpu is Big Endian or Little Endian?

if you can rely on grep being sane, yes. it's pretty easy.

$ cat test.c
short ascii_mm[] = { 0x4249, 0x4765, 0x6E44, 0x6961, 0x6E53, 0x7953, 0 };
short ascii_ii[] = { 0x694C, 0x5454, 0x656C, 0x6E45, 0x6944, 0x6E61, 0 };
void _ascii() { char* s = (char*) ascii_mm; s = (char*) ascii_ii; }
short ebcdic_ii[] = { 0x89D3, 0xE3E3, 0x8593, 0x95C5, 0x89C4, 0x9581, 0 };
short ebcdic_mm[] = { 0xC2C9, 0xC785, 0x95C4, 0x8981, 0x95E2, 0xA8E2, 0 };
void _ebcdic() { char* s = (char*) ebcdic_mm; s = (char*) ebcdic_ii; }
int main() { _ascii (); _ebcdic (); return 0; }
$ gcc -c test.c
$ grep BIGenDianSyS test.o && echo BIG
$ grep LiTTleEnDian test.o && echo LITTLE

this shows you how to do it, so you can prob create your own version as needed -mike
-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: application/pgp-signature
Size: 827 bytes
Desc: This is a digitally signed message part.
Url : http://lists.denx.de/pipermail/u-boot/attachments/20080331/99d23ccf/attachment.pgp