1
0
mirror of https://github.com/RIOT-OS/RIOT.git synced 2025-12-14 17:13:50 +01:00
19027: sys/fmt: optimize scn_u32_dec scn_u32_hex r=benpicco a=kfessel

### Contribution description

Improves the compilation result for `scn_u32_dec` `scn_u32_hex` especially on `cortex-m` reducing either stack usage and or code size.

This makes use of unsigned int overflow (slightly less better without doing that `hexn`).

See godbolt (original versions got an `o` attached, modified versions got `k`s) all functions are  marked `_S_` defined to `static`

by assigning the global at end the compiled function can be changed (`deco deck  hexo hexk hexkk hexn`)

this PR is `hexkk` and `deck` 

### Testing procedure

run unit-test/test-fmt

```
<RIOT>/tests/unittests$ make tests-fmt
<RIOT>/tests/unittests$ make term
```

### Issues/PRs references

[godbolt](https://godbolt.org/z/MzT1zh4q1)

Co-authored-by: Karl Fessel <karl.fessel@ovgu.de>
This commit is contained in:
bors[bot] 2023-02-17 20:09:53 +00:00 committed by GitHub
commit 8c2a4b43b3
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -471,14 +471,13 @@ uint32_t scn_u32_dec(const char *str, size_t n)
uint32_t res = 0;
while (n--) {
char c = *str++;
if (!fmt_is_digit(c)) {
unsigned c = *str++;
unsigned d = c - (unsigned)'0';
if ( !( '0'<= c && c <= '9') ) {
break;
}
else {
res *= 10;
res += (c - '0');
}
res *= 10U;
res += d;
}
return res;
}
@ -488,21 +487,23 @@ uint32_t scn_u32_hex(const char *str, size_t n)
uint32_t res = 0;
while (n--) {
char c = *str++;
if (!fmt_is_digit(c)) {
if (fmt_is_upper(c)) {
c = _to_lower(c);
}
if (c == '\0' || c > 'f') {
break;
}
res <<= 4;
res |= c - 'a' + 0xa;
unsigned c = *str++;
unsigned d;
if (('0'<= c) && (c <= '9')){
d = c - (unsigned)'0';
}
else if (('A' <= c) && (c <= 'F')) {
d = c - (unsigned)'A' + 0xaU;
}
else if (('a' <= c) && (c <= 'f')) {
d = c - (unsigned)'a' + 0xaU;
}
else {
res <<= 4;
res |= c - '0';
break;
}
res <<= 4U;
res |= d;
}
return res;
}