/*
Description: This function saturate input 32 bit number into a 16 bit number.
If input number is greater than 0x7fff then output is saturated to 0x7fff.
else if input number is less than 0xffff8000 then output is saturated to 0xffff8000
else output is same as input.
*/
int16 qm_sat32(int32 op)
{
int16 result;
if (op > (int32) 0x7fff) {
result = 0x7fff;
} else if (op < (int32) 0xffff8000) {
result = (int16) (0x8000);
} else {
result = (int16) op;
}
return result;
}
/*
Description: This function saturate input 32 bit number into a 16 bit number.
If input number is greater than 0x7fff then output is saturated to 0x7fff.
else if input number is less than 0xffff8000 then output is saturated to 0xffff8000
else output is same as input.
*/
int16 qm_sat32(int32 op) {
return (op > (int32) 0x7fff) ? 0x7fff : (op < (int32) 0xffff8000) ? (int16) (0x8000) : (int16) op;
}
>>8 The first form is easier to read and the object code will be the same in both cases.
The object code will be the same for both if your compiler is smart. Do you really need variable assigning? As for readability, I will answer this as soon as I stop laughing.
>>9
Code is for programmers to read and only incidentally for computers to execute. >>1 is written in C rather than assembly for the sake of the programmer. >>2 doesn't take much effort to read, but I would that most programmers that have a stake in reading the driver code would have require a larger cognitive effort to comprehend it.
The optimized code of >>1 and >>2 will be the same.
Name:
Anonymous2010-09-11 13:31
The ternary operator ultimately ends up being pretty useless. It's always the case when using the ternary operator that you lose readability for the sake of reducing the number of lines written, while gaining sometimes literally nothing in terms of efficiency and certainly not clarity. It's one of those nice programming ideas that ends up not being very useful in practice. It seems more suitable for functional programming languages than for imperative languages like C.
>>13
Better to just get rid of statements and have everything be expressions, including conditionals, which isn't necessarily tied to functional programming.
Name:
Anonymous2010-09-11 18:36
>>13 It's always the case when using the ternary operator that you lose readability
Only if you habitually underuse it. Once you get over the surprise of seeing it, the ternary operator nearly always provides a net readability gain by reducing the number of obfuscatory temporary variables needed.
>>35
You're stupid for using a macro in an unsafe way.
The fact that you're stupid enough to insert a chainsaw into your anus doesn't mean that everyone who uses a chainsaw is stupid.
>>38
The problem is, >>33 might not have know it was a macro. Perhaps he was just told in the documentation that it is a thing that clamps its first argument to between its second and third arguments. He then does CLAMP(x++, y++, z++);. Is this his fault?