It is currently Tue May 29, 2012 5:29 am

All times are UTC - 8 hours




 Page 1 of 1 [ 5 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  
 
 Post subject: Re: Inline constants for colors
PostPosted: Fri Apr 27, 2012 11:22 pm 

Joined: Fri Apr 27, 2012 11:18 pm
Posts: 1
Great info and admirable links, i really appreciate your post keep up the good work. Awesome share indeed. I’ve been awaiting for this update.



_________________
Get GED Online
Offline
 Profile  
 
 Post subject: Re: Inline constants for colors
PostPosted: Sun Apr 29, 2012 12:38 pm 

Joined: Thu Dec 01, 2011 6:33 am
Posts: 109
For me I see different behaviour if the function called is large. E.g.


pulse_init_dynamic_text_widget(&mode_notifications_text_widget,
mode_notifications_body_text,
FONT_MULTIFONT_R_SMALL, ((color24_t){255,255,255,0}), PWTS_CENTER);
// FONT_MULTIFONT_R_SMALL, COLOR_WHITE24, PWTS_CENTER);


If change the line being compiled from the inline version to the struct version I see that the inline version is less efficient by 8 bytes and that the struct version is more effective.
Still, it's worth testing to see what works - it may be that the rest of your code makes the optimisations that the compiler is making fit it / pack it more effectively.


Offline
 Profile  
 
 Post subject: And so i would like to pick
PostPosted: Sun May 06, 2012 11:41 pm 

Joined: Sun Apr 22, 2012 4:46 pm
Posts: 7
a number of reduced sites this get exceptional reputation, nonetheless, they're able to deliver the incredible platinum for you personally just in a few minutes. There's also a few websites which can promisewow gold
wow gold
there is a big stock within your hosting host, nevertheless, they should delay a little while to offer the incredible gold, at times might be A single days moment. So it will be another difficulties you to locate a reliable internet site. Some day,when I is a any rush to find a dependable site, my buddy advised a great ideal website toVisit This Link
me.


Offline
 Profile  
 
Display posts from previous:  Sort by  
 Page 1 of 1 [ 5 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: