Consider following piece of C code –
char sum_char(char a,char b)
{
char c = a+b;
return c;
}
It involves –
- Convert second parameter to sign extension.
- Push signed extension parameter on stack as b.
- Convert first parameter to sign extension.
- Push signed extension parameter on stack as a.
- Add a & b, result cast to char and store it in c.
- C is again sign extended.
- Sign extended c is copied to return value register and function return to caller.
- To store result caller function again convert int to char.
My questions are –
- Who does this ?
- What is necessity of doing so many conversions ?
- Will it reduce/increase the performance of machine/compiler ?
- If it is reducing performance what should we do in order to increase it ?
chars, then you perform arithmetic operations onchars. Let the optimizer take care of removing all unnecessary instructions for your platform. In most cases, CPU has instructions that are compatible with the semantic required by the C language, so the generated code will be very short.Of course if you do not need to perform operations on signed characters, you can perform operations on unsigned characters. This eliminated a good deal of sign extending.