Skip site navigation (1)Skip section navigation (2)
Date:      Tue, 11 Feb 2003 00:17:44 +1100 (EST)
From:      Bruce Evans <bde@zeta.org.au>
To:        David Schultz <dschultz@uclink.Berkeley.EDU>
Cc:        Dag-Erling Smorgrav <des@ofug.org>, Lukas Ertl <l.ertl@univie.ac.at>, <standards@FreeBSD.ORG>
Subject:   Re: C99 floating point macros
Message-ID:  <20030210234147.B890-100000@gamplex.bde.org>
In-Reply-To: <20030210020259.GA4103@HAL9000.homeunix.com>

next in thread | previous in thread | raw e-mail | index | archive | help
On Sun, 9 Feb 2003, David Schultz wrote:

> Thus spake Dag-Erling Smorgrav <des@ofug.org>:
> > David Schultz <dschultz@uclink.Berkeley.EDU> writes:
> > > Hmm...why not just use some macros, like this?
> >
> > Macros behave strangely when invoked with arguments which have side
> > effects.

This would be a programmer error, since these interfaces are specified
to be macros.

> Doh!  I should have seen that.  I'll wrap them in a do...while(0)
> with a local variable.  Thanks for the catch!

I thought that you saw it.  Implementing these interfaces using local
a local variable would be very messy and/or unportable.  To begin with,
do...while(0) doesn't work because it cannot return a value.  A not
very messy but very unportable version could use gcc statement-expressions
and __typeof.  Portable versions seem to require combinatorial explosion
(3 * 3 cases):

#define isgreater(x, y) (						\
	sizeof(x) == 4 && sizeof(y) == 4 ? isgreaterff((x), (y)) :	\
	sizeof(x) == 4 && sizeof(y) == 8 ? isgreaterfd((x), (y)) :	\
	sizeof(x) == 4 && sizeof(y)  > 8 ? isgreaterfld((x), (y)) :	\
	sizeof(x) == 8 && sizeof(y) == 4 ? isgreaterdf((x), (y)) :	\
	sizeof(x) == 8 && sizeof(y) == 8 ? isgreaterdd((x), (y)) :	\
	sizeof(x) == 8 && sizeof(y)  > 8 ? isgreaterdld((x), (y)) :	\
	sizeof(x)  > 8 && sizeof(y) == 4 ? isgreaterldf((x), (y)) :	\
	sizeof(x)  > 8 && sizeof(y) == 8 ? isgreaterldd((x), (y)) :	\
	sizeof(x)  > 8 && sizeof(y)  > 8 ? isgreaterldld((x), (y)) :	\
	(abort(), 0))
/*
 * The final step seems to require functions to avoid side effects
 * (unless we use expression-statements).  The above just avoids using
 * __typeof (using a not completely portable sizeof() hack).
 */
int isgreaterff(float x, float y);
...

I think your simpler macros are better.  Why protect the programmer more
than for getc_unlocked (was: getc())?

Bruce


To Unsubscribe: send mail to majordomo@FreeBSD.org
with "unsubscribe freebsd-standards" in the body of the message




Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?20030210234147.B890-100000>