First to make the C++ code more readable; I am programming compiler, and I gave it:
var swap = ( int x, y ) => { //Assign method that returns two ints, and gets two ints as parameter to variable named swap.
var NewX = y
var NewY = x
}
var increment = ( int x ) => {
var Result = x + 1
}
NOTE: Functions return any variable that it's first letter is capitalized. swap
can be used like ... = swap( x, y ).NewX
, but increment
can be used as just ... = increment( x )
.
After some optimization it generated: (Made swap
and increment
actual function instead of variables, and optimized swap
's stack)
template<int BytesCount> struct rawdata { //struct from some header
char _[ BytesCount ];
inline char &operator[] (int index) {
return _[ index ];
}
};
//...
rawdata<8> generatedfunction0( rawdata<8> p ) { // var swap = ( int x, y ) => {
return{ p[ 4 ], p[ 5 ], p[ 6 ], p[ 7 ], p[ 0 ], p[ 1 ], p[ 2 ], p[ 3 ] };
}
rawdata<4> generatedfunction1( rawdata<4> p ) { // var increment = ( int x ) => {
rawdata<4> r = { p[ 0 ], p[ 1 ], p[ 2 ], p[ 3 ] };
++*( ( int* )&r[ 0 ] );
return r;
}
I am almost sure that ++*( ( int* )&r[ 0 ] );
won't do useless indirection, but how about return{ p[ 4 ], p[ 5 ], p[ 6 ], p[ 7 ], p[ 0 ], p[ 1 ], p[ 2 ], p[ 3 ] };
? Is there any source that guarantees that it will optimized it as if it was two ints that being put into array instead of 8 or more instructions that put byte by byte? I am not talking about this particular case only, but anything similar.
If it depends, then I am using GCC to compile the generated code.
printf
and etc.) If you want to see similar example check it out. – LyingOnTheSky Nov 20 '14 at 16:28