avr-gcc-list
[Top][All Lists]
Advanced

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

Re: [avr-gcc-list] Detecting ++/-- overflow


From: Wallace White
Subject: Re: [avr-gcc-list] Detecting ++/-- overflow
Date: Fri, 30 May 2003 08:40:45 -0700
User-agent: Mozilla/5.0 (Windows; U; Windows NT 5.0; en-US; rv:1.3.1) Gecko/20030425

Thanks for the ideas, David. Now I just need a free minute to try them out...

- Wallace

David Brown wrote:

From your first example, it looks like you were looking for singed types.
For unsigned types, I think it might be a bit easier.  Here's some ideas - I
haven't tried compiling them to see the code generated, but they might be of
interest.

1) Use a long/struct typedef:
    union { unsigned long big; struct { unsigned int counter, overflow; }; }
bigCounter;

    Then use "bigCounter.big++" for your increment, and read
bigCounter.counter or bigCounter.overflow as needed.  You could make "big"
and "overflow" signed, but not counter (your overflows would occur on the -1
to 0 transition).

2) For unsigned, you can use:
    if (!(++counter)) overflow++;
I don't know whether gcc will generate a seperate test for zero, or whether
it can use the flags from the ++counter directly.

3) Your "bit_is_set(SREG, V)" trick should work fine.  Use "V" for signed
counters, "C" for unsigned counters.


mvh.

David



reply via email to

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