public inbox for gcc@gcc.gnu.org
 help / color / mirror / Atom feed
* RE: `quad' printf format specifier ?
@ 1999-02-08  9:25 Guillermo A. Loyola
       [not found] ` < 8C36CEF2AF34D211922D00A0C9D60A54020D0F@epimail.epigram.com >
  1999-02-28 22:53 ` Guillermo A. Loyola
  0 siblings, 2 replies; 30+ messages in thread
From: Guillermo A. Loyola @ 1999-02-08  9:25 UTC (permalink / raw)
  To: egcs

> Come to think of that, does it imply that we can do 'short 
> short' for an eight-bit byte? =)

From the list of gcc ports that never saw the light of day,
Microunity had one that defined:

		long long : 128 bits
		     long : 64 bits
			int : 64 bits
		    short : 32 bits
	    short short : 16 bits
		     char : 8 bits

Gmo.

^ permalink raw reply	[flat|nested] 30+ messages in thread
* Re: `quad' printf format specifier ?
@ 1999-02-10 11:33 Kaveh R. Ghazi
  1999-02-28 22:53 ` Kaveh R. Ghazi
  0 siblings, 1 reply; 30+ messages in thread
From: Kaveh R. Ghazi @ 1999-02-10 11:33 UTC (permalink / raw)
  To: Marc.Espie; +Cc: egcs, millert

 > From: Marc Espie <Marc.Espie@liafa.jussieu.fr>
 > 
 > > 	Perhaps the right fix would be for the format check for %qd to
 > > stop examining whether the underlying type is long long, but instead
 > > it should check whether the type of the argument is `quad_t' at some
 > > level of abstraction.  I don't know if this info has been lost at the
 > > point of the test though.
 > 
 > I'm afraid it has been... IF you know how to patch gcc/egcs to make it
 > work at that level, this is VERY fine. Otherwise, we would want egcs
 > to stop assuming that quad_t == long long, since this is plain wrong.
 > 
 > If you prefer: we slightly prefer "%qd" to behave correctly in the
 > presence of (quad_t) than yield possible bogus warnings... the best
 > way would probably be to keep the type-info and ensure that quad_t has
 > been used (though there is a good chance that some code will roll its
 > own), but baring that, equating "%qd" with long long is more annoying
 > than anything.
 > -- 
 > 	Marc Espie		


	I don't think we have to worry about people who roll their own
quad_t.  Remember we're talking about format specifier checks for printf
from libc.  Thus the quad_t in user code must be the same one used when
the printf function from libc was built.  I.e. both must be whatever the
OS's notion of quad_t is.  If the user rolls their own, all bets are off. 

	As for the actual solution, the info is available.  If you look
at the end of function check_format_info() in c-common.c, all the tree
information is there for you.  All you have to do is check if the name
of the type is "quad_t" and make an exception for that case when the
format specifier is 'q'.  There is a problem when using layers of
typedefs.  E.g. 

 > typedef long quad_t; /* From OS header */
 > 
 > typedef quad_t q2; /* In user code */
 > printf("%qd", (q2) 0);

	In this case, when you muck through the tree structure, the
"type" appears to be "q2".  So I think what you need to do is chain
through the typedef declarations somehow (ending when you reach a
builtin type) to see if any of them are "quad_t" and if you find quad_t,
then make the exception.  I'm not an expert on trees so you'll have to
figure out how to do that yourself. 

		--Kaveh
--
Kaveh R. Ghazi			Engagement Manager / Project Services
ghazi@caip.rutgers.edu		Qwest Internet Solutions

^ permalink raw reply	[flat|nested] 30+ messages in thread
* Re: `quad' printf format specifier ?
@ 1999-02-10  8:02 Kaveh R. Ghazi
  1999-02-28 22:53 ` Kaveh R. Ghazi
  0 siblings, 1 reply; 30+ messages in thread
From: Kaveh R. Ghazi @ 1999-02-10  8:02 UTC (permalink / raw)
  To: Marc.Espie, zack; +Cc: egcs

 > From: Marc Espie <Marc.Espie@liafa.jussieu.fr>
 > 
 > On Sat, Feb 06, 1999 at 02:04:00PM -0500, Zack Weinberg wrote:
 > > It makes sense now.  I can make a case for fixing gcc or for changing
 > > your definition of quad_t:
 > 
 > > - %qd is defined to print a 64 bit quantity, so if long is 64 bits,
 > > then we shouldn't warn when %qd is used to print something with base
 > > type long.
 > Yes, this should make sense.


	No, this does not ensure your printf code is portable.



 > > - but long isn't 64 bits everywhere, so portable code must use int64_t
 > > and/or long long to get 64 bits reliably; therefore %qd and %lld
 > > should warn when given something with base type long.
 > Current definition of quad_t is encapsulated in machine/cdefs.h, or
 > something like this, so this is not a portability issue.
 > `quad_t' will be 64 bits, but the underlying `true' type may change...
 > what's interesting for us is to get something sensible according to
 > whether or not the current base type has the same size as the underlying
 > type or not.   I believe such portability warnings belong in the 
 > -ansi -pedantic class.


	You should never mix format specifiers (which pair with builtin
types, quad_t being an exception) and int64_t because this is completely
non-portable.  -ansi and -pedantic are for standards compliance
warnings.  Since %qd is a BSDism, I don't think quad_t formats relates
to it. 



 > If there is no actual standard to enforce, I fail to see how OpenBSD
 > could get it `wrong', and I'm mainly trying to keep current practice
 > from breaking without disturbing correct code. 
 > 	Marc Espie		


	The root of the problem is that gcc assumes quad_t == long long
in its -Wformat check.  You can see this in c-common.c:check_format_info().

The -Wformat checks don't care (and shouldn't care) if the format
specifier and the type used happen to be the same _size_.  That test
would only be valid on the CPU in question.  A true portability test
checks the argument's _type_ and that's what's causing the problem
because quad_t is not a builtin type.  However we should not break this
correct feature of the -Wformat checks even for quad_t. 

	Since quad_t is not a builtin type and I've not read in this
thread that an official standard governs its _typedef_, I don't think
gcc can make any assumption about it.  I.e.  OpenBSD is not wrong to use
plain `long' if that type is in fact 64 bits on the arch in question. 

	Perhaps the right fix would be for the format check for %qd to
stop examining whether the underlying type is long long, but instead
it should check whether the type of the argument is `quad_t' at some
level of abstraction.  I don't know if this info has been lost at the
point of the test though.

		--Kaveh
--
Kaveh R. Ghazi			Engagement Manager / Project Services
ghazi@caip.rutgers.edu		Qwest Internet Solutions

^ permalink raw reply	[flat|nested] 30+ messages in thread
* Re: `quad' printf format specifier ?
@ 1999-02-09  7:13 John Breen
       [not found] ` < 19990209150626.21275.qmail@hotmail.com >
  0 siblings, 1 reply; 30+ messages in thread
From: John Breen @ 1999-02-09  7:13 UTC (permalink / raw)
  To: egcs

From: Jeffrey A Law <law@hurl.cygnus.com>
>  In message 
< Pine.LNX.4.05.9902081606350.164-100000@lo-pc3035a.hitc.com >you wr
>ite:
>  > [...] then 'long', 'int', 'short' and 'char' can also be
>  > 64 bits in certain circumstances? 
>I believe they can.  I'm not aware of any such target, but I do not
>think it is disallowed by the standard.

TI's C compiler for the C40 declares long, int, short, and char to all 
be 32 bits (all memory accesses on the C40 are 32 bits).  I haven't 
checked the c4x port in egcs.
--
John A. Breen
jab3@hotmail.com


______________________________________________________
Get Your Private, Free Email at http://www.hotmail.com

^ permalink raw reply	[flat|nested] 30+ messages in thread
* Re: `quad' printf format specifier ?
@ 1999-02-08 15:20 Ross Smith
  1999-02-28 22:53 ` Ross Smith
  0 siblings, 1 reply; 30+ messages in thread
From: Ross Smith @ 1999-02-08 15:20 UTC (permalink / raw)
  To: egcs

From: Alex Buell <alex.buell@tahallah.demon.co.uk>
>On Mon, 8 Feb 1999, Jeffrey A Law wrote:
>
>> No, you can't assume that.  You can assume:
>> 
>> sizeof (long long) >=  sizeof (long) >= sizeof (int) >= sizeof (short) >= sizeof (char)
>
>Assuming that >= means greater *or* equal to, are you saying that given
>'long long' is 64 bits then 'long', 'int', 'short' and 'char' can also be
>64 bits in certain circumstances? 
>
>If that's correct, the expression 'Oh bugger!' is something that comes to
>mind. What's the best way to ensure that you get datatypes of a given
>bit-length in C/C++?

It depends on exactly what you mean by "a given bit-length".

When C was originally specified, the intention was that "int" would
always be the host machine's natural word size. That way, you could
use "int" in your code and be reasonably sure of getting maximally
efficient arithmetic. If you needed space efficiency and didn't need
large values, you used "short"; if you needed to be sure that large
values (over 16 bits) were valid, you used "long".

At that time, most hardware (at least, most hardware that ran C
compilers) was 16-bit or 32-bit. Now, with 64-bit CPUs becoming more
common, a lot of people porting compilers to those machines have
chosen to use 16-bit short, 32-bit int, and 64-bit long. I suppose
that seems natural if you're not familiar with the reasoning behind
the standard requirements, but it breaks the "int is best" assumption.
It's no longer possible to write code that works efficiently on both
32-bit and 64-bit platforms, without using your own typedef for "best
integer type".

(Java actually *requires* the s16/i32/l64 scheme, because its design
philosophy is that absolutely predictable behaviour is more important
than efficiency.)

The proposed C9X standard includes a whole bunch of integer typedefs,
which fall into three categories:

  The fastest integer with at least N bits (time optimisation)
  The smallest integer with at least N bits (space optimisation)
  Integers with exactly N bits (fatal error if no such type exists)

It also adds the "long long" type, which is apparently for those who
want a 64-bit type on 32-bit hardware. (Given the typedefs, which
provide the same facility more consistently, this seems silly.)

If you want something with more reliable behaviour than short, int,
and long, you can use the C9X typedefs if you have them, or you can
easily synthesise the same sort of thing in C++. You can see my
implementation at
http://animal.ihug.co.nz/c++/integer_bits.hpp

>OK, this is getting off-topic for the egcs mailing list, time to take this
>to comp.lang.c or .c++.

Probably. (But use the respective .moderated groups, the old
unmoderated ones have long since collapsed into unreadable cesspools.)

--
Ross Smith ................................... mailto:ross.s@ihug.co.nz
.............. The Internet Group, Auckland, New Zealand ..............
                               * * * * *
    To err is human. To really screw up requires the root password.


^ permalink raw reply	[flat|nested] 30+ messages in thread
[parent not found: <19990206133120.26761@liafa1.liafa.jussieu.fr>]

end of thread, other threads:[~1999-02-28 22:53 UTC | newest]

Thread overview: 30+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
1999-02-08  9:25 `quad' printf format specifier ? Guillermo A. Loyola
     [not found] ` < 8C36CEF2AF34D211922D00A0C9D60A54020D0F@epimail.epigram.com >
1999-02-08 11:48   ` Alex Buell
     [not found]     ` < Pine.LNX.4.05.9902081436140.307-100000@lo-pc3035a.hitc.com >
1999-02-08 12:24       ` Jeffrey A Law
     [not found]         ` < 1997.918505244@hurl.cygnus.com >
1999-02-08 13:12           ` Alex Buell
     [not found]             ` < Pine.LNX.4.05.9902081606350.164-100000@lo-pc3035a.hitc.com >
1999-02-08 13:57               ` Joe Buck
1999-02-28 22:53                 ` Joe Buck
1999-02-08 14:00               ` Jeffrey A Law
1999-02-28 22:53                 ` Jeffrey A Law
1999-02-28 22:53             ` Alex Buell
1999-02-28 22:53         ` Jeffrey A Law
1999-02-28 22:53     ` Alex Buell
1999-02-28 22:53 ` Guillermo A. Loyola
  -- strict thread matches above, loose matches on Subject: below --
1999-02-10 11:33 Kaveh R. Ghazi
1999-02-28 22:53 ` Kaveh R. Ghazi
1999-02-10  8:02 Kaveh R. Ghazi
1999-02-28 22:53 ` Kaveh R. Ghazi
1999-02-09  7:13 John Breen
     [not found] ` < 19990209150626.21275.qmail@hotmail.com >
1999-02-09  8:59   ` Alan Lehotsky
1999-02-08 15:20 Ross Smith
1999-02-28 22:53 ` Ross Smith
     [not found] <19990206133120.26761@liafa1.liafa.jussieu.fr>
1999-02-06 11:04 ` Zack Weinberg
     [not found]   ` < 199902061904.OAA02631@blastula.phys.columbia.edu >
1999-02-06 12:27     ` Alex Buell
1999-02-28 22:53       ` Alex Buell
1999-02-06 13:23     ` Marc Espie
1999-02-08  1:44       ` Andreas Schwab
1999-02-28 22:53         ` Andreas Schwab
     [not found]       ` < 19990206222305.19585@liafa1.liafa.jussieu.fr >
1999-02-08 20:51         ` Zack Weinberg
1999-02-28 22:53           ` Zack Weinberg
1999-02-28 22:53       ` Marc Espie
1999-02-28 22:53   ` Zack Weinberg

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).