Skip to content

Commit

Permalink
Rewrite compareString() and compareStringn() for flash strings to be …
Browse files Browse the repository at this point in the history
…slightly more compact, and defering (int) subtraction until the end
  • Loading branch information
bxparks committed Mar 15, 2018
1 parent fd8c4a7 commit 46fa4cf
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 22 deletions.
8 changes: 4 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -541,19 +541,19 @@ microcontrollers:
```
Platform (resource) | Max | ArduinoUnit | AUnit |
---------------------------+---------+-------------+-------------|
Arduino Nano (flash) | 30720 | 54038 | 18412 |
Arduino Nano (flash) | 30720 | 54038 | 18422 |
Arduino Nano (static) | 2048 | 1061 | 908 |
---------------------------+---------+-------------+-------------|
Teensy LC (flash) | 63488 | 36196 | 25104 |
Teensy LC (flash) | 63488 | 36196 | 25096 |
Teensy LC (static) | 8192 | 2980 | 2768 |
---------------------------+---------+-------------+-------------|
Teensy 3.2 (flash) | 262144 | 51236 | 36136 |
Teensy 3.2 (static) | 65536 | 5328 | 5224 |
---------------------------+---------+-------------+-------------|
ESP8266 - ESP-12E (flash) | 1044464 | does not | 267375 |
ESP8266 - ESP-12E (flash) | 1044464 | does not | 267359 |
ESP8266 - ESP-12E (static) | 81920 | compile | 34564 |
---------------------------+---------+-------------+-------------|
ESP8266 - ESP-01 (flash) | 499696 | does not | 267375 |
ESP8266 - ESP-01 (flash) | 499696 | does not | 267359 |
ESP8266 - ESP-01 (static) | 47356 | compile | 34564 |
---------------------------+---------+-------------+-------------|
```
Expand Down
30 changes: 12 additions & 18 deletions src/aunit/Compare.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -171,20 +171,17 @@ int compareString(const __FlashStringHelper* a, const char* b) {
return -strcmp_P(b, (const char*) a);
}

// On ESP8266, pgm_read_byte() already takes care of 4-byte alignment, and
// memcpy_P(s, p, 4) makes 4 calls to pgm_read_byte() anyway, so don't bother
// optimizing for 4-byte alignment here.
int compareString(const __FlashStringHelper* a, const __FlashStringHelper* b) {
const char* aa = reinterpret_cast<const char*>(a);
const char* bb = reinterpret_cast<const char*>(b);

// On ESP8266, pgm_read_byte() already takes care of 4-byte alignment, and
// memcpy_P(s, p, 4) makes 4 calls to pgm_read_byte() anyway, so don't bother
// optimizing for 4-byte alignment here.
while (true) {
char ca = pgm_read_byte(aa);
char cb = pgm_read_byte(bb);
if (ca < cb) return -1;
if (ca > cb) return 1;
// we hit this condition only if both strings are the same length,
// so no need to check both strings for '\0'
uint8_t ca = pgm_read_byte(aa);
uint8_t cb = pgm_read_byte(bb);
if (ca != cb) return (int) ca - (int) cb;
if (ca == '\0') return 0;
aa++;
bb++;
Expand Down Expand Up @@ -225,21 +222,18 @@ int compareStringN(const __FlashStringHelper* a, const char* b, size_t n) {
return -strncmp_P(b, (const char*)a, n);
}

// On ESP8266, pgm_read_byte() already takes care of 4-byte alignment, and
// memcpy_P(s, p, 4) makes 4 calls to pgm_read_byte() anyway, so don't bother
// optimizing for 4-byte alignment here.
int compareStringN(const __FlashStringHelper* a, const __FlashStringHelper* b,
size_t n) {
const char* aa = reinterpret_cast<const char*>(a);
const char* bb = reinterpret_cast<const char*>(b);

// On ESP8266, pgm_read_byte() already takes care of 4-byte alignment, and
// memcpy_P(s, p, 4) makes 4 calls to pgm_read_byte() anyway, so don't bother
// optimizing for 4-byte alignment here.
while (n > 0) {
char ca = pgm_read_byte(aa);
char cb = pgm_read_byte(bb);
if (ca < cb) return -1;
if (ca > cb) return 1;
// we hit this condition only if both strings are the same length,
// so no need to check both strings for '\0'
uint8_t ca = pgm_read_byte(aa);
uint8_t cb = pgm_read_byte(bb);
if (ca != cb) return (int) ca - (int) cb;
if (ca == '\0') return 0;
aa++;
bb++;
Expand Down

0 comments on commit 46fa4cf

Please sign in to comment.