Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

hoist boundary check code to the out of loop #10

Merged
merged 2 commits into from
Dec 3, 2014
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 14 additions & 2 deletions misc/gen-qrintf.h.pl
Original file line number Diff line number Diff line change
Expand Up @@ -88,10 +88,12 @@ sub build_x {
my ($check, $type, $suffix, $with_width) = @_;
return build_mt(template => << 'EOT', escape_func => undef)->($check, $type, $suffix, $with_width ? '_width' : '');
? my ($check, $type, $suffix, $width) = @_;
? my $push = $check eq 'chk' ? sub { "do { int ch = $_[0]; if (ctx.off < ctx.size) ctx.str[ctx.off] = ch; ++ctx.off; } while (0)" } : sub { "ctx.str[ctx.off++] = $_[0]" };
static inline qrintf_<?= $check ?>_t _qrintf_<?= $check ?><?= $width ?>_<?= $suffix ?>(qrintf_<?= $check ?>_t ctx<?= $width ? ", int fill_ch, int width" : "" ?>, <?= $type ?> v, const char *chars)
{
int len;
? if ($check eq 'chk') {
int rest = 0;
? }
if (v != 0) {
int bits;
if (sizeof(<?= $type ?>) == sizeof(unsigned long long))
Expand All @@ -106,12 +108,22 @@ sub build_x {
}
? if ($width) {
ctx = _qrintf_<?= $check ?>_fill(ctx, fill_ch, len, width);
? }
? if ($check eq 'chk') {
if (ctx.off + len > ctx.size) {
rest = ctx.off + len - ctx.size;
len -= rest;
v >>= rest * 4;
}
? }
len *= 4;
do {
len -= 4;
<?= $push->(q{chars[(v >> len) & 0xf]}) ?>;
ctx.str[ctx.off++] = chars[(v >> len) & 0xf];
} while (len != 0);
? if ($check eq 'chk') {
ctx.off += rest;
? }
return ctx;
}
EOT
Expand Down
90 changes: 80 additions & 10 deletions share/qrintf/qrintf.h
Original file line number Diff line number Diff line change
Expand Up @@ -1091,6 +1091,7 @@ static inline qrintf_chk_t _qrintf_chk_width_zu(qrintf_chk_t ctx, int fill_ch, i
static inline qrintf_chk_t _qrintf_chk_hx(qrintf_chk_t ctx, unsigned short v, const char *chars)
{
int len;
int rest = 0;
if (v != 0) {
int bits;
if (sizeof(unsigned short) == sizeof(unsigned long long))
Expand All @@ -1103,17 +1104,24 @@ static inline qrintf_chk_t _qrintf_chk_hx(qrintf_chk_t ctx, unsigned short v, co
} else {
len = 1;
}
if (ctx.off + len > ctx.size) {
rest = ctx.off + len - ctx.size;
len -= rest;
v >>= rest * 4;
}
len *= 4;
do {
len -= 4;
do { int ch = chars[(v >> len) & 0xf]; if (ctx.off < ctx.size) ctx.str[ctx.off] = ch; ++ctx.off; } while (0);
ctx.str[ctx.off++] = chars[(v >> len) & 0xf];
} while (len != 0);
ctx.off += rest;
return ctx;
}

static inline qrintf_chk_t _qrintf_chk_x(qrintf_chk_t ctx, unsigned v, const char *chars)
{
int len;
int rest = 0;
if (v != 0) {
int bits;
if (sizeof(unsigned) == sizeof(unsigned long long))
Expand All @@ -1126,17 +1134,24 @@ static inline qrintf_chk_t _qrintf_chk_x(qrintf_chk_t ctx, unsigned v, const cha
} else {
len = 1;
}
if (ctx.off + len > ctx.size) {
rest = ctx.off + len - ctx.size;
len -= rest;
v >>= rest * 4;
}
len *= 4;
do {
len -= 4;
do { int ch = chars[(v >> len) & 0xf]; if (ctx.off < ctx.size) ctx.str[ctx.off] = ch; ++ctx.off; } while (0);
ctx.str[ctx.off++] = chars[(v >> len) & 0xf];
} while (len != 0);
ctx.off += rest;
return ctx;
}

static inline qrintf_chk_t _qrintf_chk_lx(qrintf_chk_t ctx, unsigned long v, const char *chars)
{
int len;
int rest = 0;
if (v != 0) {
int bits;
if (sizeof(unsigned long) == sizeof(unsigned long long))
Expand All @@ -1149,17 +1164,24 @@ static inline qrintf_chk_t _qrintf_chk_lx(qrintf_chk_t ctx, unsigned long v, con
} else {
len = 1;
}
if (ctx.off + len > ctx.size) {
rest = ctx.off + len - ctx.size;
len -= rest;
v >>= rest * 4;
}
len *= 4;
do {
len -= 4;
do { int ch = chars[(v >> len) & 0xf]; if (ctx.off < ctx.size) ctx.str[ctx.off] = ch; ++ctx.off; } while (0);
ctx.str[ctx.off++] = chars[(v >> len) & 0xf];
} while (len != 0);
ctx.off += rest;
return ctx;
}

static inline qrintf_chk_t _qrintf_chk_llx(qrintf_chk_t ctx, unsigned long long v, const char *chars)
{
int len;
int rest = 0;
if (v != 0) {
int bits;
if (sizeof(unsigned long long) == sizeof(unsigned long long))
Expand All @@ -1172,17 +1194,24 @@ static inline qrintf_chk_t _qrintf_chk_llx(qrintf_chk_t ctx, unsigned long long
} else {
len = 1;
}
if (ctx.off + len > ctx.size) {
rest = ctx.off + len - ctx.size;
len -= rest;
v >>= rest * 4;
}
len *= 4;
do {
len -= 4;
do { int ch = chars[(v >> len) & 0xf]; if (ctx.off < ctx.size) ctx.str[ctx.off] = ch; ++ctx.off; } while (0);
ctx.str[ctx.off++] = chars[(v >> len) & 0xf];
} while (len != 0);
ctx.off += rest;
return ctx;
}

static inline qrintf_chk_t _qrintf_chk_zx(qrintf_chk_t ctx, size_t v, const char *chars)
{
int len;
int rest = 0;
if (v != 0) {
int bits;
if (sizeof(size_t) == sizeof(unsigned long long))
Expand All @@ -1195,17 +1224,24 @@ static inline qrintf_chk_t _qrintf_chk_zx(qrintf_chk_t ctx, size_t v, const char
} else {
len = 1;
}
if (ctx.off + len > ctx.size) {
rest = ctx.off + len - ctx.size;
len -= rest;
v >>= rest * 4;
}
len *= 4;
do {
len -= 4;
do { int ch = chars[(v >> len) & 0xf]; if (ctx.off < ctx.size) ctx.str[ctx.off] = ch; ++ctx.off; } while (0);
ctx.str[ctx.off++] = chars[(v >> len) & 0xf];
} while (len != 0);
ctx.off += rest;
return ctx;
}

static inline qrintf_chk_t _qrintf_chk_width_hx(qrintf_chk_t ctx, int fill_ch, int width, unsigned short v, const char *chars)
{
int len;
int rest = 0;
if (v != 0) {
int bits;
if (sizeof(unsigned short) == sizeof(unsigned long long))
Expand All @@ -1219,17 +1255,24 @@ static inline qrintf_chk_t _qrintf_chk_width_hx(qrintf_chk_t ctx, int fill_ch, i
len = 1;
}
ctx = _qrintf_chk_fill(ctx, fill_ch, len, width);
if (ctx.off + len > ctx.size) {
rest = ctx.off + len - ctx.size;
len -= rest;
v >>= rest * 4;
}
len *= 4;
do {
len -= 4;
do { int ch = chars[(v >> len) & 0xf]; if (ctx.off < ctx.size) ctx.str[ctx.off] = ch; ++ctx.off; } while (0);
ctx.str[ctx.off++] = chars[(v >> len) & 0xf];
} while (len != 0);
ctx.off += rest;
return ctx;
}

static inline qrintf_chk_t _qrintf_chk_width_x(qrintf_chk_t ctx, int fill_ch, int width, unsigned v, const char *chars)
{
int len;
int rest = 0;
if (v != 0) {
int bits;
if (sizeof(unsigned) == sizeof(unsigned long long))
Expand All @@ -1243,17 +1286,24 @@ static inline qrintf_chk_t _qrintf_chk_width_x(qrintf_chk_t ctx, int fill_ch, in
len = 1;
}
ctx = _qrintf_chk_fill(ctx, fill_ch, len, width);
if (ctx.off + len > ctx.size) {
rest = ctx.off + len - ctx.size;
len -= rest;
v >>= rest * 4;
}
len *= 4;
do {
len -= 4;
do { int ch = chars[(v >> len) & 0xf]; if (ctx.off < ctx.size) ctx.str[ctx.off] = ch; ++ctx.off; } while (0);
ctx.str[ctx.off++] = chars[(v >> len) & 0xf];
} while (len != 0);
ctx.off += rest;
return ctx;
}

static inline qrintf_chk_t _qrintf_chk_width_lx(qrintf_chk_t ctx, int fill_ch, int width, unsigned long v, const char *chars)
{
int len;
int rest = 0;
if (v != 0) {
int bits;
if (sizeof(unsigned long) == sizeof(unsigned long long))
Expand All @@ -1267,17 +1317,24 @@ static inline qrintf_chk_t _qrintf_chk_width_lx(qrintf_chk_t ctx, int fill_ch, i
len = 1;
}
ctx = _qrintf_chk_fill(ctx, fill_ch, len, width);
if (ctx.off + len > ctx.size) {
rest = ctx.off + len - ctx.size;
len -= rest;
v >>= rest * 4;
}
len *= 4;
do {
len -= 4;
do { int ch = chars[(v >> len) & 0xf]; if (ctx.off < ctx.size) ctx.str[ctx.off] = ch; ++ctx.off; } while (0);
ctx.str[ctx.off++] = chars[(v >> len) & 0xf];
} while (len != 0);
ctx.off += rest;
return ctx;
}

static inline qrintf_chk_t _qrintf_chk_width_llx(qrintf_chk_t ctx, int fill_ch, int width, unsigned long long v, const char *chars)
{
int len;
int rest = 0;
if (v != 0) {
int bits;
if (sizeof(unsigned long long) == sizeof(unsigned long long))
Expand All @@ -1291,17 +1348,24 @@ static inline qrintf_chk_t _qrintf_chk_width_llx(qrintf_chk_t ctx, int fill_ch,
len = 1;
}
ctx = _qrintf_chk_fill(ctx, fill_ch, len, width);
if (ctx.off + len > ctx.size) {
rest = ctx.off + len - ctx.size;
len -= rest;
v >>= rest * 4;
}
len *= 4;
do {
len -= 4;
do { int ch = chars[(v >> len) & 0xf]; if (ctx.off < ctx.size) ctx.str[ctx.off] = ch; ++ctx.off; } while (0);
ctx.str[ctx.off++] = chars[(v >> len) & 0xf];
} while (len != 0);
ctx.off += rest;
return ctx;
}

static inline qrintf_chk_t _qrintf_chk_width_zx(qrintf_chk_t ctx, int fill_ch, int width, size_t v, const char *chars)
{
int len;
int rest = 0;
if (v != 0) {
int bits;
if (sizeof(size_t) == sizeof(unsigned long long))
Expand All @@ -1315,11 +1379,17 @@ static inline qrintf_chk_t _qrintf_chk_width_zx(qrintf_chk_t ctx, int fill_ch, i
len = 1;
}
ctx = _qrintf_chk_fill(ctx, fill_ch, len, width);
if (ctx.off + len > ctx.size) {
rest = ctx.off + len - ctx.size;
len -= rest;
v >>= rest * 4;
}
len *= 4;
do {
len -= 4;
do { int ch = chars[(v >> len) & 0xf]; if (ctx.off < ctx.size) ctx.str[ctx.off] = ch; ++ctx.off; } while (0);
ctx.str[ctx.off++] = chars[(v >> len) & 0xf];
} while (len != 0);
ctx.off += rest;
return ctx;
}

Expand Down
5 changes: 5 additions & 0 deletions t/test.c
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,11 @@ void test_simple()
CHECK_SNPRINTF(3, "%d", 12345);
CHECK_SNPRINTF(10, "%u.%u.%u.%u", 12, 34, 56, 78);
CHECK_SNPRINTF(3, "%x", 0xffff);

CHECK_SNPRINTF(3, "%hx", USHRT_MAX);
CHECK_SNPRINTF(3, "%x", UINT_MAX);
CHECK_SNPRINTF(3, "%lx", ULONG_MAX);
CHECK_SNPRINTF(3, "%llx", ULLONG_MAX);
}

void test_composite()
Expand Down