By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
449,215 Members | 1,930 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 449,215 IT Pros & Developers. It's quick & easy.

Register Variables

P: n/a
some say floats can't be stored in register.
size of int and float are same ( on some machine am i Wrong?)
so if int can be delcared as register than why not float(where in and
float are of same size) ?

tx
Ajay
Nov 13 '05 #1
Share this Question
Share on Google+
10 Replies


P: n/a
dis
"ajay" <aj*************@yahoo.com> wrote in message
news:2a**************************@posting.google.c om...
some say floats can't be stored in register.
You have been misinformed. Some implementations allow for a float object to
be stored in a register.
size of int and float are same ( on some machine am i Wrong?)
sizeof(int) == sizeof(float) can yield either a value of 0 or a value of 1
for a given implementation.
so if int can be delcared as register than why not float(where in and
float are of same size) ?


You have been misinformed. The following is a compliant program:

int main(void)
{
register float pi = 3.0;
return 0;
}

Nov 13 '05 #2

P: n/a
ajay writes:
some say floats can't be stored in register.
size of int and float are same ( on some machine am i Wrong?)
so if int can be delcared as register than why not float(where in and
float are of same size) ?


Since the register keyword is only advice, and not a mandate, to the
compiler there is no need for a register to actually be able to contain the
relevant datum. If a float won't fit, the compiler can simply ignore the
suggestion.
Nov 13 '05 #3

P: n/a
aj*************@yahoo.com (ajay) wrote:
some say floats can't be stored in register.
This is immaterial to C. They certainly can be declared using the
register keyword, but whether that actually results in them being stored
in a register is something the Standard cannot possibly require.
size of int and float are same ( on some machine am i Wrong?)
On some machine, sure. On others they're not.
so if int can be delcared as register than why not float(where in and
float are of same size) ?


_If_ it were true that there is a machine on which int and float are the
same size, and _if_ it were true that on that machine ints could be
stored in registers, and floats couldn't, _then_ a reason could be that
this machine has specialised integer and floating point registers, and
the FP unit only works on doubles.
On the other hand, _if_ there were a machine on which int and float are
widely different sizes, and _if_ on that machine both floats and ints
could be stored in registers, _then_ the reason could be that this
machine, too, has specialised integer and floating point registers, but
its FPU can handle both floats and doubles.

The important thing, though, is that the C language requires neither of
these machines to exist. All it requires is that all objects can be
declared with the register storage class, and (quote from n869.txt):

# [#4] A declaration of an identifier for an object with
# storage-class specifier register suggests that access to the
# object be as fast as possible. The extent to which such
# suggestions are effective is implementation-defined.

IOW, the register keyword is _supposed_ to make the object faster, and
this is _usually_ achieved by putting it in a hardware register, but the
Standard can demand nothing in this respect. Hey, some machines might
not even _have_ registers!

Richard
Nov 13 '05 #4

P: n/a
On Fri, 26 Sep 2003, ajay wrote:
some say floats can't be stored in register.
size of int and float are same ( on some machine am i Wrong?)
so if int can be delcared as register than why not float(where in and
float are of same size) ?


First, when you declare any variable as a register variable it does not
guarantee the variable will be placed in a register. The C standard
indicates:

A declaration of an identifier for an object with
storage-class specifier register suggests that access
to the object be as fast as possible. The extent to
which such suggestions are effective is implementation-
defined.

So there is nothing stopping you from declaring a float as register. The
compiler will take this as a suggestion. If for any reason the float
cannot be placed in a register than the compiler is free to ignore your
suggestion and make it a regular auto variable.

As to the point that float and int are the same size on your machine so
why not put a float in a register. Size is not the only consideration for
putting data in registers. Some operations may not be possible on
variables in registers. If opcodes relating to floating point values only
work on variables in memory then a float will not be placed in a register.
This relates to the microprocessor architecture and not the C standard so
I will not elaborate further. I will suggest you look into microprocessor
design if you want to understand more about this.

--
Send e-mail to: darrell at cs dot toronto dot edu
Don't send e-mail to vi************@whitehouse.gov
Nov 13 '05 #5

P: n/a
Darrell Grainger wrote:
On Fri, 26 Sep 2003, ajay wrote:
some say floats can't be stored in register.
size of int and float are same ( on some machine am i Wrong?)
so if int can be delcared as register than why not float (where
in and float are of same size) ?


First, when you declare any variable as a register variable it
does not guarantee the variable will be placed in a register. The
C standard indicates:

A declaration of an identifier for an object with
storage-class specifier register suggests that access
to the object be as fast as possible. The extent to
which such suggestions are effective is implementation-
defined.

So there is nothing stopping you from declaring a float as
register. The compiler will take this as a suggestion. If for
any reason the float cannot be placed in a register than the
compiler is free to ignore your suggestion and make it a regular
auto variable.


However, once you have declared it as "register" you can no longer
take its address, regardless of what the compiler does with that
suggestion.

--
Chuck F (cb********@yahoo.com) (cb********@worldnet.att.net)
Available for consulting/temporary embedded and systems.
<http://cbfalconer.home.att.net> USE worldnet address!
Nov 13 '05 #6

P: n/a

"ajay" <aj*************@yahoo.com> wrote in message

some say floats can't be stored in register.
That could well be true on your platform. "register" will have no effect on
a float variable.
size of int and float are same ( on some machine am i Wrong?)
Yes. On an old DOS machine int is typically 16 bits, float 32 bits.
so if int can be delcared as register than why not float(where in and
float are of same size) ?

You could physically store the bit pattern of the float in an integer
register, but there might be no "floating-point multiply" or similar
instructions to use with the integer register, so putting a float there
won't speed things up.
Nov 13 '05 #7

P: n/a
"Malcolm" <ma*****@55bank.freeserve.co.uk> writes:
"ajay" <aj*************@yahoo.com> wrote in message

some say floats can't be stored in register.

That could well be true on your platform. "register" will have no effect on
a float variable.

[...]

That may be true on a specific platform, but it's not true in general.
On the hardware level, almost all platforms that have data registers
at all are able to store floats in registers (possibly in specialized
floating-point registers). A compiler may or may not take advantage
of this; for example, if there are only enough floating-point
registers for expression evaluation, it may not make sense to dedicate
one of them to hold a particular variable.

Others have correctly pointed out that "register" may or may not be
honored by the compiler, even for int variables.

But one thing that's not optional is that you can't take the address
of a "register" variable. A compiler may be able to take advantage of
that guarantee to perform some extra optimizations, even if the
variable is stored in memory.

--
Keith Thompson (The_Other_Keith) ks*@cts.com <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://www.sdsc.edu/~kst>
Schroedinger does Shakespeare: "To be *and* not to be"
Nov 13 '05 #8

P: n/a
Keith Thompson wrote:

"Malcolm" <ma*****@55bank.freeserve.co.uk> writes:
"ajay" <aj*************@yahoo.com> wrote in message

some say floats can't be stored in register.

That could well be true on your platform. "register" will have no effect on
a float variable.

[...]

That may be true on a specific platform, but it's not true in general.
On the hardware level, almost all platforms that have data registers
at all are able to store floats in registers (possibly in specialized
floating-point registers). A compiler may or may not take advantage
of this; for example, if there are only enough floating-point
registers for expression evaluation, it may not make sense to dedicate
one of them to hold a particular variable.

Others have correctly pointed out that "register" may or may not be
honored by the compiler, even for int variables.


My experience has been that with high levels of optimization, is that
'register' is either ignored or my use of it trips up the optimizer.
So I've stopped using it for general coding, only trying it when I'm
trying to get better performance out of a piece of code. But even
then, the optimizer either usually out does me (or outsmarts me).

My experience is weak in the x86 arena, so some folks may have seen
different. On the SUN I'm currently using, there's so many f.p.
registers that you don't have to worry about it.
Nov 13 '05 #9

P: n/a
My experience has been that with high levels of optimization, is that
'register' is either ignored or my use of it trips up the optimizer.
So I've stopped using it for general coding, only trying it when I'm
trying to get better performance out of a piece of code. But even
then, the optimizer either usually out does me (or outsmarts me).

My experience is weak in the x86 arena, so some folks may have seen
different. On the SUN I'm currently using, there's so many f.p.
registers that you don't have to worry about it.


So you're using SPARC then. Scott McNealy's toy.

Bill

-----= Posted via Newsfeeds.Com, Uncensored Usenet News =-----
http://www.newsfeeds.com - The #1 Newsgroup Service in the World!
-----== Over 100,000 Newsgroups - 19 Different Servers! =-----
Nov 13 '05 #10

P: n/a
On Mon, 29 Sep 2003 18:36:49 -0400, in comp.lang.c you
wrote:
My experience has been that with high levels of optimization, is that
'register' is either ignored or my use of it trips up the optimizer.
So I've stopped using it for general coding, only trying it when I'm
trying to get better performance out of a piece of code. But even
then, the optimizer either usually out does me (or outsmarts me).

My experience is weak in the x86 arena, so some folks may have seen
different. On the SUN I'm currently using, there's so many f.p.
registers that you don't have to worry about it.


So you're using SPARC then. Scott McNealy's toy.


What sort of a troll are you?



Nov 13 '05 #11

This discussion thread is closed

Replies have been disabled for this discussion.