Register Variables

A

ajay

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
 
D

dis

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;
}
 
R

Richard Bos

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
 
O

osmium

ajay said:
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.
 
D

Darrell Grainger

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.
 
C

CBFalconer

Darrell said:
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.
 
M

Malcolm

ajay said:
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.
 
K

Keith Thompson

Malcolm said:
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.
 
K

Kurt Krueger

Keith said:
Malcolm said:
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.
 
B

Bill Cunningham

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads


Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,537
Members
45,022
Latest member
MaybelleMa

Latest Threads

Top