[U-Boot] [PATCH] Introduce a global bool type
Måns Rullgård
mans at mansr.com
Tue Jan 22 00:08:57 CET 2013
Scott Wood <scottwood at freescale.com> writes:
> On 01/21/2013 04:36:42 PM, Måns Rullgård wrote:
>> Scott Wood <scottwood at freescale.com> writes:
>>
>> > On 01/19/2013 03:30:30 AM, Albert ARIBAUD wrote:
>> >> /* what I favor */
>> >> clk_is_enabled = ((reg_val >> 9) & 1) ? true: false;
>> >> ip_is_enabled = clk_is_enabled && pwd_is_enabled;
>> >> if (clk_is_enabled) { ...
>> >>
>> >> rather than assigning them 'zero/nonzero', or using bitwise ops on
>> >> booleans, or testing against boolean constants (although I concede
>> >> that the first line below wins over its counterpart above as far as
>> >> concision is concerned).
>> >
>> > Conciseness can be improved with "!!((reg_val >> 9) & 1)".
>>
>> x & 1 is already either zero or one. Any further operations are
>> nothing
>> but obfuscation.
>
> The point is to avoid depending on the actual integer values of the
> boolean type,
Boolean expressions are defined to have a value of zero or one, and the
_Bool type (may it burn in hell) must be able to represent those values.
> and make the code more robust against changes (e.g. someone later
> comes along and says "hmm, that 1 should be a 3 because we care about
> that other register bit as well" without noticing that it's being
> assigned to a boolean.
If you stayed away from the silly _Bool type that wouldn't be a problem,
as long as any uses of the value treat all non-zero values equally, i.e.
only use it in a boolean context and not compare against explicit values
or perform arithmetic or bitwise logic operations on it.
In other words, boolifying on use rather than on assignment is generally
safer and usually at least as efficient.
--
Måns Rullgård
mans at mansr.com
More information about the U-Boot
mailing list