Skip to content

Instantly share code, notes, and snippets.

@kbob
Created June 10, 2016 20:22
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kbob/02024f75c16a00afdd204beeb9783c81 to your computer and use it in GitHub Desktop.
Save kbob/02024f75c16a00afdd204beeb9783c81 to your computer and use it in GitHub Desktop.
Silly unrolled print function
#include <assert.h>
#include <stdint.h>
#include <unistd.h>
/* print nine digit positive */
char *p9dp(uint32_t n, char *p)
{
uint32_t a = n % 10;
n /= 10;
if (n) {
uint32_t b = n % 10;
n /= 10;
if (n) {
uint32_t c = n % 10;
n /= 10;
if (n) {
uint32_t d = n % 10;
n /= 10;
if (n) {
uint32_t e = n % 10;
n /= 10;
if (n) {
uint32_t f = n % 10;
n /= 10;
if (n) {
uint32_t g = n % 10;
n /= 10;
if (n) {
uint32_t h = n % 10;
n /= 10;
if (n) {
uint32_t i = n;
*p++ = '0' + i;
}
*p++ = '0' + h;
}
*p++ = '0' + g;
}
*p++ = '0' + f;
}
*p++ = '0' + e;
}
*p++ = '0' + d;
}
*p++ = '0' + c;
}
*p++ = '0' + b;
}
*p++ = '0' + a;
return p;
}
#define N 15000000
char buffer[10 * (size_t)N];
int main()
{
char *p = buffer;
for (uint32_t i = 1; i <= N; i++) {
p = p9dp(i, p);
*p++ = '\n';
}
write(1, buffer, p - buffer);
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment