C Idioms in ATMEL ARM GNU tools?

Go To Last Post
4 posts / 0 new
Author
Message
#1
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

So in compilers like Keil MDK, there are idioms that it recognizes to ensure you do certain things in a single instruction. For instance, if I program this : 
 

x.b = n;  Keil MDK automatically assembles to the single instruction BFI. Simple example, but an example none the less...

 

I realize the code will still compile either way, but without the recognition of idioms the code isn't guaranteed to be as compact. Yes, I understand this is why people license expensive compilers, but I'm curious if there is something like this built in compilers created from ARM GNU tools. I can't find any documentation on idioms like this so I'm guessing no, so do I just have to hit -O3 and hope for the best? Obviously there are other ways to speed up performance with interleaving loads and stores, etc... but figured it couldn't hurt to ask the question.

 

There is also the option of using intrinsics as well...
 

 

 

 

 

 

 

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

I don't think that that gcc supports any "idioms" directly, but it's got a pretty good optimizer...

 

 

x.b = n;  Keil MDK automatically assembles to the single instruction BFI. Simple example, but an example none the less...

Wait.  What?  Do you mean "structurename.elementname = variable", "structurename.elementname = constant;" for C structures (oh, like ip->version = 4;), or specifically "registername.<constantbitfieldnumber> = constant;" (like uartinterrupts.6 = 1;")??  The latter is supported by some compilers for cpus like the 8051 or 8bit PICs that have specific bit operation on memory instructions that might otherwise be difficult for a compiler to figure out, BUT...

  1. ARM doesn't have memory-oriented instructions, so BFI will at best do a register to register bitfield copy that needs additional instructions to move to and from memory anyway.
  2. for constants, BFI doesn't look any better than BIC/ORR/etc
  3. Keil doesn't seem to support the latter syntax (f.1 = 0) on ARM.

 

There seems to be room in the gnu code generator and/or optimizer for such things.  "ioreg |= 1<<bitno;" will compile to a single AVR instruction when possible...

 

 

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

This all makes sense, and was my assumption. On the BFI, I pulled this right out of the Keil Documentation as an example that they show with M4F. Kind of arbitrary but to illustrate the point, you type this in code, you definitely get this in machine instruction. 
I haven't had a lot of trouble with code size/speed yet, but I'm working on a pretty intense DSP application on the M7 and want to see if I can accelerate it a bit to allow headroom for other processes without buying a license for another compiler. This was one thought, and knowing what the idioms might be would be easier to just use them up front than guessing and looking at disassembly.

 

 

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Well, I've confirmed that Keil and gcc produce similar code (with BFI instructions) for the following small program.

(Hmm.  The code editor thing <> isn't working for me at the moment (!?)  added anyway :-( )

#include <sam3x8c.h>

struct {
    unsigned int a:7;
    unsigned int b:4;
    unsigned int c:8;
    unsigned int d:3;
    unsigned int e:10;
} x;

int main(int argc, char**argv) {

    x.b = argc;
    x.c = *argv[1];
    x.d = 6;
    x.e = z;
}