avr-libc-dev
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: [avr-libc-dev] RFC: avr/bits.h


From: E. Weddington
Subject: Re: [avr-libc-dev] RFC: avr/bits.h
Date: Tue, 01 Mar 2005 10:25:57 -0700
User-agent: Mozilla Thunderbird 0.7.3 (Windows/20040803)

Erik Walthinsen wrote:

As per E. Weddinton's suggestion a month ago, I'd like to start the process of getting avr/bits.h included in avr-libc. A first pass at the file and its documentation are attached.

Comments, enhancements, and additional documentation requested. Let's get this finished and the sbi/cbi mess behind us.


IIRC, additional macros are also needed according to the size of the operands. For example this is really what should be done:

=================================================================
#define bit_set_8(var, mask) ((uint8_t)(var) |= (uint8_t)(mask)) #define bit_clear_8(var, mask) ((uint8_t)(var) &= (uint8_t)~(mask)) #define bit_toggle_8(var, mask) ((uint8_t)(var) ^= (uint8_t)(mask)) #define bit_read_8(var, mask) ((uint8_t)(var) & (uint8_t)(mask))

#define bit_set_16(var, mask) ((uint16_t)(var) |= (uint16_t)(mask)) #define bit_clear_16(var, mask) ((uint16_t)(var) &= (uint16_t)~(mask)) #define bit_toggle_16(var, mask) ((uint16_t)(var) ^= (uint16_t)(mask)) #define bit_read_16(var, mask) ((uint16_t)(var) & (uint16_t)(mask))

#define bit_set_32(var, mask) ((uint32_t)(var) |= (uint32_t)(mask)) #define bit_clear_32(var, mask) ((uint32_t)(var) &= (uint32_t)~(mask)) #define bit_toggle_32(var, mask) ((uint32_t)(var) ^= (uint32_t)(mask)) #define bit_read_32(var, mask) ((uint32_t)(var) & (uint32_t)(mask))

#define bit_set(var, mask)                      bit_set_8(var, mask)
#define bit_clear(var, mask)                    bit_clear_8(var, mask)
#define bit_toggle(var, mask)                   bit_toggle_8(var, mask)
#define bit_read(var, mask)                     bit_read_8(var, mask)
=================================================================

The reason why this should be done is to get around the automatic integer promotion. The C language bit operators automatically promote it's operands to an int, 16 bits. This is unacceptable for operations on 8 bit values which are commonly used when operating on the AVR registers. Typecasting is necessary to tell the compiler to optimize the generated assembly. There's a FAQ item in the avr-libc user manual about this issue.

Eric




reply via email to

[Prev in Thread] Current Thread [Next in Thread]