It is currently Wed Jul 06, 2011 12:51 pm

All times are UTC - 8 hours




 Page 1 of 1 [ 2 posts ] 
Author Message
 Post subject: Inline constants for colors
PostPosted: Sat Mar 26, 2011 4:34 pm 

Joined: Sun Feb 27, 2011 11:32 am
Posts: 71
Since memory is at a premium, it saves a few bytes to use C99 inline struct constants for colors instead of static const variables:
--- include/pulse_types.h-orig   2011-03-26 20:41:27.000000000 -0400
+++ include/pulse_types.h   2011-03-26 20:41:41.000000000 -0400
@@ -12,8 +12,8 @@
} __attribute__((packed)) color24_t;

/* Some handy colors to use. */
-static const color24_t COLOR_BLACK24 = {0, 0, 0, 0};
-static const color24_t COLOR_WHITE24 = {0xff, 0xff, 0xff, 0};
+#define COLOR_BLACK24 ((color24_t){0,0,0,0})
+#define COLOR_WHITE24 ((color24_t){255,255,255,0})


// This struct is almost identical to a struct tm time struct (in C), but with some key differences.

In my single file test case this saved 16 bytes (8 bytes for the data and 8 bytes of loading instructions replaced with a single load immediate); if the constants are used in more than one file or more than once per file, then the savings can be even larger. There are some cases, however, where colors that are more complex than all zero or all ones might require more instructions to build immediate values. But white and black are definitely good candidates for inlining.


Offline
 Profile  
 
 Post subject: Re: Inline constants for colors
PostPosted: Sat Mar 26, 2011 4:45 pm 

Joined: Sun Feb 27, 2011 11:32 am
Posts: 71
The actual code generated is pretty crazy:
#include <pulse_types.h>

extern void foo(color24_t c);

void const_color(void)
{
        foo(COLOR_BLACK24);
}

void inline_color(void)
{
        foo((color24_t){0,0,0,0});
}

Produces the following functions, for a savings of 16 bytes of code + 4 bytes of data:

00000000 <inline_color>:
   0:   e3a03000        mov     r3, #0
   4:   e24dd004        sub     sp, sp, #4
   8:   e1a00003        mov     r0, r3
   c:   e58d3000        str     r3, [sp]
  10:   e28dd004        add     sp, sp, #4
  14:   eafffffe        b       0 <foo>

versus:
00000000 <const_color>:
   0:   e59f301c        ldr     r3, [pc, #28]   ; 24 <const_color+0x24>
   4:   e5d30000        ldrb    r0, [r3]
   8:   e5d32001        ldrb    r2, [r3, #1]
   c:   e5d31002        ldrb    r1, [r3, #2]
  10:   e1800402        orr     r0, r0, r2, lsl #8
  14:   e5d33003        ldrb    r3, [r3, #3]
  18:   e1800801        orr     r0, r0, r1, lsl #16
  1c:   e1800c03        orr     r0, r0, r3, lsl #24
  20:   eafffffe        b       0 <foo>
  24:   00000000        andeq   r0, r0, r0

Disassembly of section .rodata:

00000000 <COLOR_BLACK24>:
   0:   00000000        andeq   r0, r0, r0


Offline
 Profile  
 
Display posts from previous:  Sort by  
 Page 1 of 1 [ 2 posts ] 

All times are UTC - 8 hours


Who is online

Users browsing this forum: No registered users and 1 guest


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to: