[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.c

Register storage type

unknown

5/12/2011 8:56:00 PM

Hello

Say the CPU has only AL, BL, CL, DL or eight 8 bit registers, and if
want to declare 10 register variables in my code, is it possible to do
it?

Say size_of(float) is the same as size_of(int), is it possible to define
a "register float" variable? How will the register get converted between
int and float?

Thanks.
42 Answers

Kenneth Brody

5/12/2011 9:02:00 PM

0

On 5/12/2011 4:55 PM, Free Willy wrote:
> Hello
>
> Say the CPU has only AL, BL, CL, DL or eight 8 bit registers, and if
> want to declare 10 register variables in my code, is it possible to do
> it?
>
> Say size_of(float) is the same as size_of(int), is it possible to define
> a "register float" variable? How will the register get converted between
> int and float?

The "register" keyword is just a hint to the compiler, which it is free to
use or ignore as it sees fit.

--
Kenneth Brody

unknown

5/12/2011 9:14:00 PM

0

Kenneth Brody writes:

> On 5/12/2011 4:55 PM, Free Willy wrote:
>> Hello
>>
>> Say the CPU has only AL, BL, CL, DL or eight 8 bit registers, and if
>> want to declare 10 register variables in my code, is it possible to do
>> it?
>>
>> Say size_of(float) is the same as size_of(int), is it possible to
>> define a "register float" variable? How will the register get converted
>> between int and float?
>
> The "register" keyword is just a hint to the compiler, which it is free
> to use or ignore as it sees fit.

OK, but will it always give a warning if you try to assign too many
register variables for the CPU (or the wrong type of variable, if this
isn't supported), or just compile it possibly with errors or undefined
behaviors at runtime?

Another question is array variables declared as register type - could the
compiler put a small array say of 3 elements into different registers and
translate array accesses to the right register?

Angel

5/12/2011 9:15:00 PM

0

On 2011-05-12, Free Willy <nospam@nospam.com> wrote:
>
> Say the CPU has only AL, BL, CL, DL or eight 8 bit registers, and if
> want to declare 10 register variables in my code, is it possible to do
> it?

The"register" specifier is a hint for the compiler; the compiler
is not required to follow it. If it's not possible on a given
architecture, then of course the compiler will ignore it and treat the
variable like any other.

> Say size_of(float) is the same as size_of(int), is it possible to define
> a "register float" variable? How will the register get converted between
> int and float?

Size or type don't matter, you can give any automatic variable the register
specifier. The compiler will assign an actual register if it is feasible
to do so. This varies per implementation.

Internally, any type of variable is represented as a number of bits,
which is the only thing a computer can work with. If the number of bits
required to represent the variable can fit in a register, the compiler
may do so. No special conversion is needed; a variable in a register is no
different from a variable in normal memory, except that you cannot have a
pointer to a register.


--
"C provides a programmer with more than enough rope to hang himself.
C++ provides a firing squad, blindfold and last cigarette."
- seen in comp.lang.c

Keith Thompson

5/12/2011 9:20:00 PM

0

Free Willy <nospam@nospam.com> writes:
> Say the CPU has only AL, BL, CL, DL or eight 8 bit registers, and if
> want to declare 10 register variables in my code, is it possible to do
> it?

Certainly, you can declare as many register variables as you like.

> Say size_of(float) is the same as size_of(int), is it possible to define
> a "register float" variable? How will the register get converted between
> int and float?

(It's "sizeof", not "size_of".)

You can define a "register float" variable regardless of the relative
sizes of int and float. No conversion is implied. You can use the
"register" specifier for any declaration, as long as it's inside a
function (though declaring an array as "register" means you can't
do anything with it other than applying "sizeof").

The thing to remember is that "register" doesn't mean "store this
object in a CPU register". All the standard says is that

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 isimplementation-defined.

and that you can't take the address of a register object, or declare
one outside a function.

The common wisdom these days is that the compiler can do a better
job of deciding what to store in a register than you're likely to,
and that declaring objects as "register" can even hurt performance
if the compiler pays attention to it. I'm actually not sure whether
this is correct in general.

--
Keith Thompson (The_Other_Keith) kst-u@mib.org <http://www.ghoti.ne...
Nokia
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"

Angel

5/12/2011 9:20:00 PM

0

On 2011-05-12, Free Willy <nospam@nospam.com> wrote:
> Kenneth Brody writes:
>
>> The "register" keyword is just a hint to the compiler, which it is free
>> to use or ignore as it sees fit.
>
> OK, but will it always give a warning if you try to assign too many
> register variables for the CPU (or the wrong type of variable, if this
> isn't supported), or just compile it possibly with errors or undefined
> behaviors at runtime?

Since it's just a hint, no warnings will be given if the compiler
cannot, or chooses not to follow it. This will not lead to errors or
undefined behaviour; the variables will simply be treated like all
others.

> Another question is array variables declared as register type - could the
> compiler put a small array say of 3 elements into different registers and
> translate array accesses to the right register?

I might, if doing so is feasible. The language makes no guarantee
either way, it's up to the compiler to decide.


--
"C provides a programmer with more than enough rope to hang himself.
C++ provides a firing squad, blindfold and last cigarette."
- seen in comp.lang.c

Keith Thompson

5/12/2011 9:28:00 PM

0

Free Willy <nospam@nospam.com> writes:
> Kenneth Brody writes:
>> On 5/12/2011 4:55 PM, Free Willy wrote:
>>> Say the CPU has only AL, BL, CL, DL or eight 8 bit registers, and if
>>> want to declare 10 register variables in my code, is it possible to do
>>> it?
>>>
>>> Say size_of(float) is the same as size_of(int), is it possible to
>>> define a "register float" variable? How will the register get converted
>>> between int and float?
>>
>> The "register" keyword is just a hint to the compiler, which it is free
>> to use or ignore as it sees fit.
>
> OK, but will it always give a warning if you try to assign too many
> register variables for the CPU (or the wrong type of variable, if this
> isn't supported), or just compile it possibly with errors or undefined
> behaviors at runtime?

A compiler can warn about anything it likes, and some might give such a
warning, either by default or by the use of some command-line option.
But no such warning is required, and as far as I know most compilers
won't bother.

Declaring "too many" register variables won't cause errors or undefined
behavior. The compiler isn't disobeying the "register" specifier, since
the specifier (in spite of the name) doesn't say to store the variable
in a register.

> Another question is array variables declared as register type - could the
> compiler put a small array say of 3 elements into different registers and
> translate array accesses to the right register?

If you declare an array variable as "register", you can't take its
address. Since array indexing is defined in terms of addresses, such an
array variable is pretty much useless.

If you declare an array variable *without* "register", the compiler can
store it in one or more registers, as long as the behavior of the
program is not affected. For example, if you take the address of one of
the elements of the array and pass it to some function, the compiler
isn't likely to be able to store it in a register, since the called
function woudnl't know how to access it.

--
Keith Thompson (The_Other_Keith) kst-u@mib.org <http://www.ghoti.ne...
Nokia
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"

Seebs

5/12/2011 10:00:00 PM

0

On 2011-05-12, Keith Thompson <kst-u@mib.org> wrote:
> The common wisdom these days is that the compiler can do a better
> job of deciding what to store in a register than you're likely to,
> and that declaring objects as "register" can even hurt performance
> if the compiler pays attention to it. I'm actually not sure whether
> this is correct in general.

It can have one positive effect:

Because it guarantees that the address of a thing can't be taken, it can
in some cases allow a compiler to optimize better whether or not it puts
the object in question "in a register", just because there's no need to worry
about things modifying the object through pointers.

-s
--
Copyright 2011, all wrongs reversed. Peter Seebach / usenet-nospam@seebs.net
http://www.seeb... <-- lawsuits, religion, and funny pictures
http://en.wikipedia.org/wiki/...(Scientology) <-- get educated!
I am not speaking for my employer, although they do rent some of my opinions.

China Blue Veins

5/12/2011 10:24:00 PM

0

In article <iqhhgg$d8j$1@speranza.aioe.org>, Free Willy <nospam@nospam.com>
wrote:

> Hello
>
> Say the CPU has only AL, BL, CL, DL or eight 8 bit registers, and if
> want to declare 10 register variables in my code, is it possible to do
> it?
>
> Say size_of(float) is the same as size_of(int), is it possible to define
> a "register float" variable? How will the register get converted between
> int and float?
>
> Thanks.

You can only suggest register usage to the compiler, not require it. If you need
that kind of specificity, you have to switch to another language such as
assembly.

--
Damn the living - It's a lovely life. I'm whoever you want me to be.
Silver silverware - Where is the love? At least I can stay in character.
Oval swimming pool - Where is the love? Annoying Usenet one post at a time.
Damn the living - It's a lovely life. In 1492....

Keith Thompson

5/12/2011 10:49:00 PM

0

Seebs <usenet-nospam@seebs.net> writes:
> On 2011-05-12, Keith Thompson <kst-u@mib.org> wrote:
>> The common wisdom these days is that the compiler can do a better
>> job of deciding what to store in a register than you're likely to,
>> and that declaring objects as "register" can even hurt performance
>> if the compiler pays attention to it. I'm actually not sure whether
>> this is correct in general.
>
> It can have one positive effect:
>
> Because it guarantees that the address of a thing can't be taken, it can
> in some cases allow a compiler to optimize better whether or not it puts
> the object in question "in a register", just because there's no need to worry
> about things modifying the object through pointers.

Ah, but "register" can be applied only to block-scope. Presumably
any decent optimizing compiler can already tell that a block-scope
variable's address is never taken, just by analyzing the code.

--
Keith Thompson (The_Other_Keith) kst-u@mib.org <http://www.ghoti.ne...
Nokia
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"

cr88192

5/12/2011 10:51:00 PM

0

On 5/12/2011 2:59 PM, Seebs wrote:
> On 2011-05-12, Keith Thompson<kst-u@mib.org> wrote:
>> The common wisdom these days is that the compiler can do a better
>> job of deciding what to store in a register than you're likely to,
>> and that declaring objects as "register" can even hurt performance
>> if the compiler pays attention to it. I'm actually not sure whether
>> this is correct in general.
>
> It can have one positive effect:
>
> Because it guarantees that the address of a thing can't be taken, it can
> in some cases allow a compiler to optimize better whether or not it puts
> the object in question "in a register", just because there's no need to worry
> about things modifying the object through pointers.
>

yes, but a compiler can easily enough infer that the address isn't taken.

for example:
void func()
{
int x;
...
}

if the compiler doesn't ever see an "&x" or similar in the function,
then it can generally assume that the address of 'x' is not taken.


well, except maybe if the compiler is single-pass or lacks any sort of
optimizing stages...


it is much like, there are optimizations that can be broken by the
ability to freely 'goto' here and there, but an absence of any labels or
gotos in the area can make these optimizations safe.

say:
if(0) { ... }

an absence of labels/gotos will then allow the compiler to simply omit
the code indicated by "...".

or such...