Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Does it ever make sense for a compiler to pass a structure like this in a cpu register to a function?

I'd like to know if some kind of structure contains more than one primitive but its total size is less than or equal to size of a single cpu register like a 4-byte register, does it ever make sense for a compiler to put it in one of those 4-byte registers when passing it by value or reference to a function instead of making a copy of it on the callee stack or passing a pointer to it and in general when passing something more than a single primitive to a function like an array or an structure would passing in a cpu register ever come in handy?

sample of such structure:

struct sample{
 public:
  char char1;
  char char2;
};

sample of passing the structure to a function:

void someFunc(const sample input){
 //whatever
}
void someFunc(sample input){
 //whatever
}
void someFunc(sample & input){
 //whatever
}
void someFunc(const sample & input){
 //whatever
}
like image 757
Pooria Avatar asked Nov 18 '10 09:11

Pooria


1 Answers

This is defined in the application binary interface (ABI) of your execution environment. The standard does not say anything about processor registers when a function is called, so it is legal to create an environment where small structs are packed into a single processor register.

For the reference part, they are very likely to be passed as pointers anyway, since when inside the called function the address of a reference is taken, it must resolve to the address of the referenced object.

like image 152
Rudi Avatar answered Sep 27 '22 19:09

Rudi



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!