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: Bob Paddock
Subject: Re: [avr-libc-dev] RFC: avr/bits.h
Date: Tue, 01 Mar 2005 09:23:32 -0500
User-agent: Opera M2/7.54u1 (Win32, build 3918)

On Tue, 01 Mar 2005 01:15:47 -0800, Erik Walthinsen <address@hidden> 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.

One comment is that if BIT is in upper case should not the other macros
be in upper case?  The name BIT could also cause complications when
porting the code to other systems.  May be why we have _BV for BitValue.
Never say never when it comes to porting code...

This information would be a good introduction to the material:

http://c.snippets.org/snip_lister.php?fname=bitops.how
Bitops.How      16-Sep-2003     any     How the BitXxx() macros in BITOPS.H work
Bitops.H 19-Sep-2003 any Prototypes, plus macros to set, clear, and test bits

http://www.snippets.org/

I wanted to do a test case for using bit_XXX on longs to see if it does the
right thing, and wrote the following test case.  However the results to me
are puzzling, they look down right broken.  What is going on here?:

Test case:
-----
#include <inttypes.h>

#include "bits.h"

uint16_t shorttest_u16 = (uint16_t) 0;
uint32_t longtest_u32  = (uint32_t) 0;

int main( void )
{
    bit_set( shorttest_u16, BIT(0) );
    bit_set( shorttest_u16, BIT(1) );
    bit_set( shorttest_u16, BIT(2) );
    bit_set( shorttest_u16, BIT(3) );
    bit_set( shorttest_u16, BIT(4) );
    bit_set( shorttest_u16, BIT(5) );
    bit_set( shorttest_u16, BIT(6) );
    bit_set( shorttest_u16, BIT(7) );
    bit_set( shorttest_u16, BIT(8) );
    bit_set( shorttest_u16, BIT(9) );
    bit_set( shorttest_u16, BIT(10) );
    bit_set( shorttest_u16, BIT(11) );
    bit_set( shorttest_u16, BIT(12) );
    bit_set( shorttest_u16, BIT(13) );
    bit_set( shorttest_u16, BIT(14) );
    bit_set( shorttest_u16, BIT(15) );

    bit_set( longtest_u32, BIT(0) );
    bit_set( longtest_u32, BIT(7) );
    bit_set( longtest_u32, BIT(8) );
    bit_set( longtest_u32, BIT(15) );
    bit_set( longtest_u32, BIT(16) );
    bit_set( longtest_u32, BIT(30) );
    bit_set( longtest_u32, BIT(31) );
}
-----
Warnings start at  "bit_set( shorttest_u16, BIT(4) );":

main.c: In function `main':
main.c:15: warning: left shift count >= width of type
main.c:16: warning: left shift count >= width of type
main.c:17: warning: left shift count >= width of type
main.c:18: warning: left shift count >= width of type
main.c:19: warning: left shift count >= width of type
main.c:20: warning: left shift count >= width of type
main.c:21: warning: left shift count >= width of type
main.c:22: warning: left shift count >= width of type
main.c:23: warning: left shift count >= width of type
main.c:24: warning: left shift count >= width of type
main.c:25: warning: left shift count >= width of type
main.c:26: warning: left shift count is negative
main.c:29: warning: left shift count >= width of type
main.c:30: warning: left shift count >= width of type
main.c:31: warning: left shift count is negative
main.c:32: warning: left shift count >= width of type
main.c:33: warning: left shift count >= width of type
main.c:34: warning: left shift count >= width of type

3.3.2 and 3.4.3 generate the same code, a bit different in
format appearance, this is the 3.4.3 output:


   1                            .file   "main.c"
   2                            .arch atmega162
   3                    __SREG__ = 0x3f
   4                    __SP_H__ = 0x3e
   5                    __SP_L__ = 0x3d
   6                    __tmp_reg__ = 0
   7                    __zero_reg__ = 1
   8                            .global __do_copy_data
   9                            .global __do_clear_bss
  17                    .Ltext0:
  18                    .global longtest_u32
  19                    .global longtest_u32
  20                            .section .bss
  23                    longtest_u32:
  24 0000 0000 0000             .skip 4,0
  25                    .global shorttest_u16
  26                    .global shorttest_u16
  29                    shorttest_u16:
  30 0004 0000                  .skip 2,0
  31                            .text
  32                    .global main
  34                    main:
  35                    .LFB2:
  36                    .LM1:
  37                    /* prologue: frame size=0 */
  38 0000 C0E0                  ldi r28,lo8(__stack - 0)
  39 0002 D0E0                  ldi r29,hi8(__stack - 0)
  40 0004 DEBF                  out __SP_H__,r29
  41 0006 CDBF                  out __SP_L__,r28
  42                    /* prologue end (size=4) */
  43                    .LM2:
  44 0008 8091 0000             lds r24,shorttest_u16
  45 000c 9091 0000             lds r25,(shorttest_u16)+1
  46                    .LM3:
  47 0010 8761                  ori r24,lo8(279)
  48 0012 9160                  ori r25,hi8(279)
  49 0014 9093 0000             sts (shorttest_u16)+1,r25
  50 0018 8093 0000             sts shorttest_u16,r24
  51                    .LM4:
  52 001c 8091 0000             lds r24,longtest_u32
  53 0020 9091 0000             lds r25,(longtest_u32)+1
  54 0024 A091 0000             lds r26,(longtest_u32)+2
  55 0028 B091 0000             lds r27,(longtest_u32)+3
  56                    .LM5:
  57 002c 8360                  ori r24,lo8(3)
  58                    .LM6:
  59 002e 8093 0000             sts longtest_u32,r24
  60 0032 9093 0000             sts (longtest_u32)+1,r25
  61 0036 A093 0000             sts (longtest_u32)+2,r26
  62 003a B093 0000             sts (longtest_u32)+3,r27
  63                    .LM7:
  64 003e 80E0                  ldi r24,lo8(0)
  65 0040 90E0                  ldi r25,hi8(0)
  66                    /* epilogue: frame size=0 */
  67 0042 0C94 0000             jmp exit
  68                    /* epilogue end (size=2) */
  69                    /* function main size 35 (29) */
  70                    .LFE2:
  72                    .Letext0:
DEFINED SYMBOLS
                            *ABS*:00000000 main.c
                            *ABS*:0000003f __SREG__
                            *ABS*:0000003e __SP_H__
                            *ABS*:0000003d __SP_L__
                            *ABS*:00000000 __tmp_reg__
                            *ABS*:00000001 __zero_reg__
  C:\TEMP/ccWwaaaa.s:23     .bss:00000000 longtest_u32
  C:\TEMP/ccWwaaaa.s:29     .bss:00000004 shorttest_u16
  C:\TEMP/ccWwaaaa.s:34     .text:00000000 main

UNDEFINED SYMBOLS
__do_copy_data
__do_clear_bss
__stack
exit

If the compiler thought it was being smart by collapsing all of
the constants then it came up with the odd answer of 0x117 rather
than the correct answer of 0xFFFF.  Lines 47 -> 50 and 57.

Where did I go wrong here? Is <inttypes.h>/<stdint.h> doing something funkey? The makefile is the default generated by mfile in winavr343 compiled for a Mega162.









reply via email to

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