[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Bit operations in PL47 localtim.c



Jerry's time patch replaces the PL46 localtim.c with V7.53 of
NetBSD localtime.c.  As far as I could determine,  there were no
MiNT-specific modifications made (Jerry?) 

The following code, in function detzcode(), was causing a
problem at compile time.  It is related to bit-shifting
operations on characters (First question: why would anyone want
to do bit-shifting on chars?)

The problem is that with -mshort,  the compiler complains about
the 24 places left shift in the excerpt below:
(codep is a char array)

Quote:

         /* The first character must be sign extended on systems
         ** with >32bit longs. <...> 
         */
#ifdef __STDC__
#define SIGN_EXTEND_CHAR(x)	((signed char) x)
#else
#define SIGN_EXTEND_CHAR(x)    ((x & 0x80) ? ((~0 << 8) | x) : x)
#endif

	result = (SIGN_EXTEND_CHAR(codep[0]) << 24) \
	       | (codep[1] & 0xff) << 16 \
	       | (codep[2] & 0xff) << 8
	       | (codep[3] & 0xff);

End quote.

This appears unacceptable to me, as there is no telling what
this function will return for programs compiled with the short
integer model.

While researching this problem on the net, I came across V7.61
of the same localtime.c.  It has much nicer-looking code to do
the same thing:

	result = (codep[0] & 0x80) ? ~0L : 0L;
	for (i = 0; i < 4; ++i)
		result = (result << 8) | (codep[i] & 0xff);


This gets rid of the compiler warning, but since I don't
understand the function, I can't be sure that it is safe to use
with -mshort.  Opinions?

Yves