|
From: | David Brown |
Subject: | Re: [avr-gcc-list] accessing carry bit |
Date: | Wed, 30 Jan 2008 21:22:08 +0100 |
User-agent: | Thunderbird 2.0.0.9 (Windows/20071031) |
Dave N6NZ wrote:
John Regehr wrote:microcontroller compilers support - the ability to write things like "if (CARRY) ..." can be a big win for some code.A killer app for exposing some of the condition code flags to C may be that this facilitates very efficient integer over/under flow detection. Integer overflows are the source of many nasty security holes.I missed the earlier comments on this, but isn't getting to the condition codes a simple library of inline asm functions? Am I over-simplifying?Of course, there are plenty of ways that could screw up optimization (unless optimization is oblivious, in which case optimizer code motion destroys the flag you are after.)
There are a few problems with trying to access the carry flag via a mixture of C and inline assembly (you can obviously do it with pure inline assembly code which both sets and reads the carry flag). One is that you don't know how the compiler generated C code will affect the flag, and the other is that there is no way (that I can think of) to get the carry flag information back to the C level without turning it into a value in a register - thus losing all potential for good code.
To illustrate the first point, suppose you want to add up an array of ints using saturated arithmetic. A first effort, assuming the magic "CARRY" access, would be:
int arraySum(int *p, int n) { int sum = 0; for (int i = 0; i < n; i++) { sum += *p++; if (CARRY) sum = INT_MAX; } }That would work - unless the "p++" is done after the "sum +=", and messes up the carry.
mvh., David
[Prev in Thread] | Current Thread | [Next in Thread] |