code size benchmark: separate morph function to avoid overhead from scanf/printf
This commit is contained in:
parent
959d2f9185
commit
eb29fad4b2
3 changed files with 56 additions and 74 deletions
morphisms
|
@ -1,6 +1,42 @@
|
|||
#include <stdio.h>
|
||||
#include <stdint.h>
|
||||
|
||||
__attribute__((noinline))
|
||||
int morph_posint_radix( char * in, char * out ) {
|
||||
uint64_t value = 0;
|
||||
while( *in ) {
|
||||
if( *in >= '0' && *in <= '9' ) {
|
||||
value *= 10;
|
||||
value += *in - '0';
|
||||
}
|
||||
else
|
||||
return -1;
|
||||
|
||||
in++;
|
||||
}
|
||||
|
||||
uint64_t v = value;
|
||||
while( v ) {
|
||||
out ++;
|
||||
v /= 16;
|
||||
}
|
||||
|
||||
*out-- = '\0';
|
||||
while( value ) {
|
||||
unsigned digit = value % 16;
|
||||
if( digit < 10 ) {
|
||||
*out-- = digit + '0';
|
||||
} else if( digit < 16 ) {
|
||||
*out-- = digit + 'a' - 10;
|
||||
} else {
|
||||
return -1;
|
||||
}
|
||||
value /= 16;
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
int main() {
|
||||
char bufA[1024];
|
||||
char bufB[1024];
|
||||
|
@ -9,38 +45,7 @@ int main() {
|
|||
char * out = bufB;
|
||||
|
||||
scanf("%s", in);
|
||||
|
||||
uint64_t value = 0;
|
||||
while( *in ) {
|
||||
if( *in >= '0' && *in <= '9' ) {
|
||||
value *= 10;
|
||||
value += *in - '0';
|
||||
}
|
||||
else
|
||||
return -1;
|
||||
|
||||
in++;
|
||||
}
|
||||
|
||||
uint64_t v = value;
|
||||
while( v ) {
|
||||
out ++;
|
||||
v /= 16;
|
||||
}
|
||||
|
||||
*out-- = '\0';
|
||||
while( value ) {
|
||||
unsigned digit = value % 16;
|
||||
if( digit < 10 ) {
|
||||
*out-- = digit + '0';
|
||||
} else if( digit < 16 ) {
|
||||
*out-- = digit + 'a' - 10;
|
||||
} else {
|
||||
return -1;
|
||||
}
|
||||
value /= 16;
|
||||
}
|
||||
|
||||
morph_posint_radix( in, out );
|
||||
printf("%s\n", out);
|
||||
|
||||
return 0;
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue