Created
April 3, 2025 14:29
-
-
Save bitbank2/9d12e970d092dcf1518b90c12922bbd6 to your computer and use it in GitHub Desktop.
The code to accompany a lesson C optimization
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
// Simple optimization example - performance measurement | |
// | |
// Simulate drawing a 16x16 character block | |
uint8_t bitmap_out_tmp[16*16]; | |
uint8_t bitmap_in[16*8]; | |
static const uint8_t opa4_table[16] = {0, 17, 34, 51, | |
68, 85, 102, 119, | |
136, 153, 170, 187, | |
204, 221, 238, 255 | |
}; | |
static const uint16_t opa4_16_table[256] = { | |
0x0000,0x1100,0x2200,0x3300,0x4400,0x5500,0x6600,0x7700,0x8800,0x9900,0xaa00,0xbb00,0xcc00,0xdd00,0xee00,0xff00, | |
0x0011,0x1111,0x2211,0x3311,0x4411,0x5511,0x6611,0x7711,0x8811,0x9911,0xaa11,0xbb11,0xcc11,0xdd11,0xee11,0xff11, | |
0x0022,0x1122,0x2222,0x3322,0x4422,0x5522,0x6622,0x7722,0x8822,0x9922,0xaa22,0xbb22,0xcc22,0xdd22,0xee22,0xff22, | |
0x0033,0x1133,0x2233,0x3333,0x4433,0x5533,0x6633,0x7733,0x8833,0x9933,0xaa33,0xbb33,0xcc33,0xdd33,0xee33,0xff33, | |
0x0044,0x1144,0x2244,0x3344,0x4444,0x5544,0x6644,0x7744,0x8844,0x9944,0xaa44,0xbb44,0xcc44,0xdd44,0xee44,0xff44, | |
0x0055,0x1155,0x2255,0x3355,0x4455,0x5555,0x6655,0x7755,0x8855,0x9955,0xaa55,0xbb55,0xcc55,0xdd55,0xee55,0xff55, | |
0x0066,0x1166,0x2266,0x3366,0x4466,0x5566,0x6666,0x7766,0x8866,0x9966,0xaa66,0xbb66,0xcc66,0xdd66,0xee66,0xff66, | |
0x0077,0x1177,0x2277,0x3377,0x4477,0x5577,0x6677,0x7777,0x8877,0x9977,0xaa77,0xbb77,0xcc77,0xdd77,0xee77,0xff77, | |
0x0088,0x1188,0x2288,0x3388,0x4488,0x5588,0x6688,0x7788,0x8888,0x9988,0xaa88,0xbb88,0xcc88,0xdd88,0xee88,0xff88, | |
0x0099,0x1199,0x2299,0x3399,0x4499,0x5599,0x6699,0x7799,0x8899,0x9999,0xaa99,0xbb99,0xcc99,0xdd99,0xee99,0xff99, | |
0x00aa,0x11aa,0x22aa,0x33aa,0x44aa,0x55aa,0x66aa,0x77aa,0x88aa,0x99aa,0xaaaa,0xbbaa,0xccaa,0xddaa,0xeeaa,0xffaa, | |
0x00bb,0x11bb,0x22bb,0x33bb,0x44bb,0x55bb,0x66bb,0x77bb,0x88bb,0x99bb,0xaabb,0xbbbb,0xccbb,0xddbb,0xeebb,0xffbb, | |
0x00cc,0x11cc,0x22cc,0x33cc,0x44cc,0x55cc,0x66cc,0x77cc,0x88cc,0x99cc,0xaacc,0xbbcc,0xcccc,0xddcc,0xeecc,0xffcc, | |
0x00dd,0x11dd,0x22dd,0x33dd,0x44dd,0x55dd,0x66dd,0x77dd,0x88dd,0x99dd,0xaadd,0xbbdd,0xccdd,0xdddd,0xeedd,0xffdd, | |
0x00ee,0x11ee,0x22ee,0x33ee,0x44ee,0x55ee,0x66ee,0x77ee,0x88ee,0x99ee,0xaaee,0xbbee,0xccee,0xddee,0xeeee,0xffee, | |
0x00ff,0x11ff,0x22ff,0x33ff,0x44ff,0x55ff,0x66ff,0x77ff,0x88ff,0x99ff,0xaaff,0xbbff,0xccff,0xddff,0xeeff,0xffff | |
}; | |
typedef struct tag_charbox { | |
int box_w; | |
int box_h; | |
} CHARBOX; | |
void original_code(CHARBOX *gdsc, uint8_t *bitmap_in, uint8_t *bitmap_out_tmp, int byte_aligned, int stride) | |
{ | |
int i, x, y; | |
for(y = 0; y < gdsc->box_h; y ++) { | |
for(x = 0; x < gdsc->box_w; x++, i++) { | |
i = i & 0x1; | |
if(i == 0) { | |
bitmap_out_tmp[x] = opa4_table[(*bitmap_in) >> 4]; | |
} | |
else if(i == 1) { | |
bitmap_out_tmp[x] = opa4_table[(*bitmap_in) & 0xF]; | |
bitmap_in++; | |
} | |
} | |
/*Go to the next byte if stopped in the middle of a byte and | |
*the next line is byte aligned*/ | |
if(byte_aligned && i != 0) { | |
i = 0; | |
bitmap_in++; | |
} | |
bitmap_out_tmp += stride; | |
} | |
} /* original_code() */ | |
void new_code(CHARBOX *gdsc, uint8_t *bitmap_in, uint8_t *bitmap_out_tmp, int byte_aligned, int stride) | |
{ | |
int x, iCount; | |
uint16_t *p16; | |
iCount = gdsc->box_h * gdsc->box_w; | |
p16 = (uint16_t *)bitmap_out_tmp; | |
for(x = 0; x < iCount; x += 2) { | |
// Convert pairs of 4-bit pixels to 8-bits (e.g. 0x5 -> 0x55) | |
*p16++ = opa4_16_table[*bitmap_in++]; | |
} | |
} /* new_code() */ | |
void setup() { | |
long l; | |
int i; | |
CHARBOX cb; | |
int stride = 16; | |
int byte_aligned = 0; | |
#define ITERATIONS 10000 | |
Serial.begin(115200); | |
delay(3000); // wait for CDC-Serial to start | |
cb.box_w = cb.box_h = 16; | |
Serial.println("Testing original code"); | |
l = millis(); | |
for (i=0; i<ITERATIONS; i++) { | |
original_code(&cb, bitmap_in, bitmap_out_tmp, byte_aligned, stride); | |
} | |
l = millis() - l; | |
Serial.printf("Original code: %d iterations, total time = %d milliseconds\n", ITERATIONS, (int)l); | |
Serial.println("Testing new code"); | |
l = millis(); | |
for (i=0; i<ITERATIONS; i++) { | |
new_code(&cb, bitmap_in, bitmap_out_tmp, byte_aligned, stride); | |
} | |
l = millis() - l; | |
Serial.printf("New code: %d iterations, total time = %d milliseconds\n", ITERATIONS, (int)l); | |
} /* setup() */ | |
void loop() { | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment