From: Greg A. Woods (no email)
Date: Tue Sep 02 2003 - 04:52:05 EDT
[ On Monday, September 1, 2003 at 20:41:36 (-0400), Wietse Venema wrote: ]
> Subject: Re: max file size (again)...
>
> It would be nice to hear how this works on 64-bit systems. I can
> try this on SPARC/Solaris myself.
$ uname -srm
NetBSD 1.6.1_STABLE alpha
$ gcc --version
2.95.3
$ gcc -v
Using builtin specs.
gcc version 2.95.3 20010315 (release) (NetBSD nb3)
$ ./tmaxint
127
32767
2147483647
9223372036854775807
9223372036854775807
Here are some more related tests I had handy in my play directory:
#include <sys/types.h>
#include <stdio.h>
main(argc, argv)
int argc;
char *argv[];
{
int x;
printf("(short) -1 = %hd, %hu\n", (short) -1, (short) -1);
printf("(long) -1 = %ld, %lu\n", (long) -1, (unsigned long) -1);
printf("(quad) -1 = %qd, %qu\n", (quad_t) -1, (u_quad_t) -1);
printf("(short) -2 = %hd, %hu\n", (short) -2, (short) -2);
printf("(long) -2 = %ld, %lu\n", (long) -2, (unsigned long) -2);
printf("(quad) -2 = %qd, %qu\n", (quad_t) -2, (u_quad_t) -2);
exit(0);
}
(short) -1 = -1, 65535
(long) -1 = -1, 18446744073709551615
(quad) -1 = -1, 18446744073709551615
(short) -2 = -2, 65534
(long) -2 = -2, 18446744073709551614
(quad) -2 = -2, 18446744073709551614
/*
* cheap tricks to try to to count the number of bits in various C types.
* (without using CHAR_BIT :-)
*
* These seem to work as long as you know how wide the largest integer
* type supported by your compiler is and you can define the needed
* constants as that width....
*/
#include <stdio.h>
#include <limits.h>
#if 0 /* !defined(HAVE_LONG_LONG) */
# define _FOFO_MASK 0x0f0f0f0fL /* alternating nibbles */
# define _SEVEN_MASK 0x77777777UL /* alternating nibbles with high bit off */
# define _THREE_MASK 0x33333333UL /* alternating pairs of bits */
# define _ONE_MASK 0x11111111UL /* nibbles with low bit only */
#else
# define _FOFO_MASK 0x0f0f0f0f0f0f0f0fLL
# define _SEVEN_MASK 0x7777777777777777ULL
# define _THREE_MASK 0x3333333333333333ULL
# define _ONE_MASK 0x1111111111111111ULL
#endif
#define _BX(type, x) ((x) - (((x) >> 1) & (unsigned type) _SEVEN_MASK) \
- (((x) >> 2) & (unsigned type) _THREE_MASK) \
- (((x) >> 3) & (unsigned type) _ONE_MASK))
#define _CHAR_BC(x) (((_BX(char, x) + (_BX(char, x) >> 4)) & (char) _FOFO_MASK) % 255)
#define _SHORT_BC(x) (((_BX(short, x) + (_BX(short, x) >> 4)) & (short) _FOFO_MASK) % 255)
#define _INT_BC(x) (((_BX(int, x) + (_BX(int, x) >> 4)) & (int) _FOFO_MASK) % 255)
#define _LONG_BC(x) (((_BX(long, x) + (_BX(long, x) >> 4)) & (long) _FOFO_MASK) % 255)
#if 1 /* defined(HAVE_LONG_LONG */
# define _LONG_LONG_BC(x) (((_BX(long long, x) + (_BX(long long, x) >> 4)) & (long long) _FOFO_MASK) % 255)
#endif
#define CHAR_BITCOUNT _CHAR_BC((char) -1)
#define SHORT_BITCOUNT _SHORT_BC((short) -1)
#define INT_BITCOUNT _INT_BC((int) -1)
#define LONG_BITCOUNT _LONG_BC((long) -1)
#if 1 /* defined(HAVE_LONG_LONG */
# define LONG_LONG_BITCOUNT _LONG_LONG_BC((long long) -1)
#endif
int
main(argc, argv)
int argc;
char *argv[];
{
printf("char bits = %d, really %d.\n", (int) CHAR_BITCOUNT, (int) CHAR_BIT);
printf("short bits = %d, really %d.\n", (int) SHORT_BITCOUNT, (int) sizeof(short) * CHAR_BIT);
printf("int bits = %d, really %d.\n", (int) INT_BITCOUNT, (int) sizeof(int) * CHAR_BIT);
printf("long bits = %d, really %d.\n", (int) LONG_BITCOUNT, (int) sizeof(long) * CHAR_BIT);
#if 1 /* defined(HAVE_LONG_LONG) */
printf("long long bits = %d, really %d.\n", (int) LONG_LONG_BITCOUNT, (int) sizeof(long long) * CHAR_BIT);
#endif
printf("uchar max: %u.\n", (unsigned int) ((unsigned char) ~0ULL));
printf("ushort max: %u.\n", (unsigned int) ((unsigned short) ~0ULL));
printf("uint max: %llu.\n", (unsigned long long) ((unsigned int) ~0ULL));
printf("ulong max: %llu.\n", (unsigned long long) ((unsigned long) ~0ULL));
#if 1 /* defined(HAVE_LONG_LONG) */
printf("ulong long max: %llu.\n", (unsigned long long) ((unsigned long long) ~0ULL));
#endif
exit(0);
/* NOTREACHED */
}
and the output of the above, again on NetBSD/Alpha:
char bits = 8, really 8.
short bits = 16, really 16.
int bits = 32, really 32.
long bits = 64, really 64.
long long bits = 64, really 64.
uchar max: 255.
ushort max: 65535.
uint max: 4294967295.
ulong max: 18446744073709551615.
ulong long max: 18446744073709551615.
and on NetBSD/i386 (gcc 2.95.3):
char bits = 8, really 8.
short bits = 16, really 16.
int bits = 32, really 32.
long bits = 32, really 32.
long long bits = 64, really 64.
uchar max: 255.
ushort max: 65535.
uint max: 4294967295.
ulong max: 4294967295.
ulong long max: 18446744073709551615.
and on NetBSD/sparc (gcc 2.95.3):
char bits = 8, really 8.
short bits = 16, really 16.
int bits = 32, really 32.
long bits = 32, really 32.
long long bits = 64, really 64.
uchar max: 255.
ushort max: 65535.
uint max: 4294967295.
ulong max: 4294967295.
ulong long max: 18446744073709551615.
-- Greg A. Woods +1 416 218-0098 VE3TCP RoboHack <> Planix, Inc. <> Secrets of the Weird <>
|
|
|