Skip to content

Commit

Permalink
simplify the code, by using -(unsigned)int_value for obtaining the …
Browse files Browse the repository at this point in the history
…absolute value
  • Loading branch information
kazuho committed Dec 1, 2014
1 parent 3ba87ca commit 3cf4fc8
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 157 deletions.
44 changes: 15 additions & 29 deletions misc/gen-qrintf.h.pl
Original file line number Diff line number Diff line change
Expand Up @@ -26,18 +26,11 @@

sub build_d {
return build_mt(template => << 'EOT', escape_func => undef)->(@_);
? my ($check, $type, $suffix, $min, $max) = @_;
? my ($check, $type, $suffix) = @_;
static inline qrintf_<?= $check ?>_t _qrintf_<?= $check ?>_<?= $suffix ?>(qrintf_<?= $check ?>_t ctx, <?= $type ?> v)
{
unsigned <?= $type ?> val = v;
unsigned <?= $type ?> val = v >= 0 ? v : -(unsigned <?= $type ?>)v;
int sign = v < 0;
if (v < 0) {
if (v == <?= $min ?>) {
val = (unsigned <?= $type ?>)(<?= $max ?>) + 1;
} else {
val = (unsigned <?= $type ?>)(-v);
}
}
if (sizeof(<?= $type ?>) < sizeof(long long)) {
return _qrintf_<?= $check ?>_long_core(ctx, 0, 0, (unsigned long)val, sign);
}
Expand All @@ -49,15 +42,8 @@ sub build_d {
static inline qrintf_<?= $check ?>_t _qrintf_<?= $check ?>_width_<?= $suffix ?>(qrintf_<?= $check ?>_t ctx, int fill_ch, int width, <?= $type ?> v)
{
unsigned <?= $type ?> val = v;
unsigned <?= $type ?> val = v >= 0 ? v : -(unsigned <?= $type ?>)v;
int sign = v < 0;
if (v < 0) {
if (v == <?= $min ?>) {
val = (unsigned <?= $type ?>)(<?= $max ?>) + 1;
} else {
val = (unsigned <?= $type ?>)(-v);
}
}
if (sizeof(<?= $type ?>) < sizeof(long long)) {
return _qrintf_<?= $check ?>_long_core(ctx, fill_ch, width, (unsigned long)val, sign);
}
Expand All @@ -70,9 +56,9 @@ sub build_d {
}

sub build_u {
my ($check, $type, $suffix, $max) = @_;
return build_mt(template => << 'EOT', escape_func => undef)->($check, $type, $suffix, $max);
? my ($check, $type, $suffix, $max) = @_;
my ($check, $type, $suffix) = @_;
return build_mt(template => << 'EOT', escape_func => undef)->($check, $type, $suffix);
? my ($check, $type, $suffix) = @_;
static inline qrintf_<?= $check ?>_t _qrintf_<?= $check ?>_<?= $suffix ?>(qrintf_<?= $check ?>_t ctx, <?= $type ?> v)
{
Expand Down Expand Up @@ -538,15 +524,15 @@ sub build_x {
}
? for my $check (qw(nck chk)) {
<?= $build_d->($check, "short", "hd", "SHRT_MIN", "SHRT_MAX") ?>
<?= $build_d->($check, "int", "d", "INT_MIN", "INT_MAX") ?>
<?= $build_d->($check, "long", "ld", "LONG_MIN", "LONG_MAX") ?>
<?= $build_d->($check, "long long", "lld", "LLONG_MIN", "LLONG_MAX") ?>
<?= $build_u->($check, "unsigned short", "hu", "USHRT_MAX") ?>
<?= $build_u->($check, "unsigned", "u", "UINT_MAX") ?>
<?= $build_u->($check, "unsigned long", "lu", "ULONG_MAX") ?>
<?= $build_u->($check, "unsigned long long", "llu", "ULLONG_MAX") ?>
<?= $build_u->($check, "size_t", "zu", "SIZE_MAX") ?>
<?= $build_d->($check, "short", "hd") ?>
<?= $build_d->($check, "int", "d",) ?>
<?= $build_d->($check, "long", "ld") ?>
<?= $build_d->($check, "long long", "lld") ?>
<?= $build_u->($check, "unsigned short", "hu") ?>
<?= $build_u->($check, "unsigned", "u") ?>
<?= $build_u->($check, "unsigned long", "lu") ?>
<?= $build_u->($check, "unsigned long long", "llu") ?>
<?= $build_u->($check, "size_t", "zu") ?>
? for my $with_width (0..1) {
<?= $build_x->($check, "unsigned short", "hx", $with_width) ?>
<?= $build_x->($check, "unsigned", "x", $with_width) ?>
Expand Down
144 changes: 16 additions & 128 deletions share/qrintf/qrintf.h
Original file line number Diff line number Diff line change
Expand Up @@ -417,15 +417,8 @@ static inline qrintf_chk_t _qrintf_chk_long_long_core(qrintf_chk_t ctx, int fill

static inline qrintf_nck_t _qrintf_nck_hd(qrintf_nck_t ctx, short v)
{
unsigned short val = v;
unsigned short val = v >= 0 ? v : -(unsigned short)v;
int sign = v < 0;
if (v < 0) {
if (v == SHRT_MIN) {
val = (unsigned short)(SHRT_MAX) + 1;
} else {
val = (unsigned short)(-v);
}
}
if (sizeof(short) < sizeof(long long)) {
return _qrintf_nck_long_core(ctx, 0, 0, (unsigned long)val, sign);
}
Expand All @@ -437,15 +430,8 @@ static inline qrintf_nck_t _qrintf_nck_hd(qrintf_nck_t ctx, short v)

static inline qrintf_nck_t _qrintf_nck_width_hd(qrintf_nck_t ctx, int fill_ch, int width, short v)
{
unsigned short val = v;
unsigned short val = v >= 0 ? v : -(unsigned short)v;
int sign = v < 0;
if (v < 0) {
if (v == SHRT_MIN) {
val = (unsigned short)(SHRT_MAX) + 1;
} else {
val = (unsigned short)(-v);
}
}
if (sizeof(short) < sizeof(long long)) {
return _qrintf_nck_long_core(ctx, fill_ch, width, (unsigned long)val, sign);
}
Expand All @@ -457,15 +443,8 @@ static inline qrintf_nck_t _qrintf_nck_width_hd(qrintf_nck_t ctx, int fill_ch, i

static inline qrintf_nck_t _qrintf_nck_d(qrintf_nck_t ctx, int v)
{
unsigned int val = v;
unsigned int val = v >= 0 ? v : -(unsigned int)v;
int sign = v < 0;
if (v < 0) {
if (v == INT_MIN) {
val = (unsigned int)(INT_MAX) + 1;
} else {
val = (unsigned int)(-v);
}
}
if (sizeof(int) < sizeof(long long)) {
return _qrintf_nck_long_core(ctx, 0, 0, (unsigned long)val, sign);
}
Expand All @@ -477,15 +456,8 @@ static inline qrintf_nck_t _qrintf_nck_d(qrintf_nck_t ctx, int v)

static inline qrintf_nck_t _qrintf_nck_width_d(qrintf_nck_t ctx, int fill_ch, int width, int v)
{
unsigned int val = v;
unsigned int val = v >= 0 ? v : -(unsigned int)v;
int sign = v < 0;
if (v < 0) {
if (v == INT_MIN) {
val = (unsigned int)(INT_MAX) + 1;
} else {
val = (unsigned int)(-v);
}
}
if (sizeof(int) < sizeof(long long)) {
return _qrintf_nck_long_core(ctx, fill_ch, width, (unsigned long)val, sign);
}
Expand All @@ -497,15 +469,8 @@ static inline qrintf_nck_t _qrintf_nck_width_d(qrintf_nck_t ctx, int fill_ch, in

static inline qrintf_nck_t _qrintf_nck_ld(qrintf_nck_t ctx, long v)
{
unsigned long val = v;
unsigned long val = v >= 0 ? v : -(unsigned long)v;
int sign = v < 0;
if (v < 0) {
if (v == LONG_MIN) {
val = (unsigned long)(LONG_MAX) + 1;
} else {
val = (unsigned long)(-v);
}
}
if (sizeof(long) < sizeof(long long)) {
return _qrintf_nck_long_core(ctx, 0, 0, (unsigned long)val, sign);
}
Expand All @@ -517,15 +482,8 @@ static inline qrintf_nck_t _qrintf_nck_ld(qrintf_nck_t ctx, long v)

static inline qrintf_nck_t _qrintf_nck_width_ld(qrintf_nck_t ctx, int fill_ch, int width, long v)
{
unsigned long val = v;
unsigned long val = v >= 0 ? v : -(unsigned long)v;
int sign = v < 0;
if (v < 0) {
if (v == LONG_MIN) {
val = (unsigned long)(LONG_MAX) + 1;
} else {
val = (unsigned long)(-v);
}
}
if (sizeof(long) < sizeof(long long)) {
return _qrintf_nck_long_core(ctx, fill_ch, width, (unsigned long)val, sign);
}
Expand All @@ -537,15 +495,8 @@ static inline qrintf_nck_t _qrintf_nck_width_ld(qrintf_nck_t ctx, int fill_ch, i

static inline qrintf_nck_t _qrintf_nck_lld(qrintf_nck_t ctx, long long v)
{
unsigned long long val = v;
unsigned long long val = v >= 0 ? v : -(unsigned long long)v;
int sign = v < 0;
if (v < 0) {
if (v == LLONG_MIN) {
val = (unsigned long long)(LLONG_MAX) + 1;
} else {
val = (unsigned long long)(-v);
}
}
if (sizeof(long long) < sizeof(long long)) {
return _qrintf_nck_long_core(ctx, 0, 0, (unsigned long)val, sign);
}
Expand All @@ -557,15 +508,8 @@ static inline qrintf_nck_t _qrintf_nck_lld(qrintf_nck_t ctx, long long v)

static inline qrintf_nck_t _qrintf_nck_width_lld(qrintf_nck_t ctx, int fill_ch, int width, long long v)
{
unsigned long long val = v;
unsigned long long val = v >= 0 ? v : -(unsigned long long)v;
int sign = v < 0;
if (v < 0) {
if (v == LLONG_MIN) {
val = (unsigned long long)(LLONG_MAX) + 1;
} else {
val = (unsigned long long)(-v);
}
}
if (sizeof(long long) < sizeof(long long)) {
return _qrintf_nck_long_core(ctx, fill_ch, width, (unsigned long)val, sign);
}
Expand Down Expand Up @@ -927,15 +871,8 @@ static inline qrintf_nck_t _qrintf_nck_width_zx(qrintf_nck_t ctx, int fill_ch, i

static inline qrintf_chk_t _qrintf_chk_hd(qrintf_chk_t ctx, short v)
{
unsigned short val = v;
unsigned short val = v >= 0 ? v : -(unsigned short)v;
int sign = v < 0;
if (v < 0) {
if (v == SHRT_MIN) {
val = (unsigned short)(SHRT_MAX) + 1;
} else {
val = (unsigned short)(-v);
}
}
if (sizeof(short) < sizeof(long long)) {
return _qrintf_chk_long_core(ctx, 0, 0, (unsigned long)val, sign);
}
Expand All @@ -947,15 +884,8 @@ static inline qrintf_chk_t _qrintf_chk_hd(qrintf_chk_t ctx, short v)

static inline qrintf_chk_t _qrintf_chk_width_hd(qrintf_chk_t ctx, int fill_ch, int width, short v)
{
unsigned short val = v;
unsigned short val = v >= 0 ? v : -(unsigned short)v;
int sign = v < 0;
if (v < 0) {
if (v == SHRT_MIN) {
val = (unsigned short)(SHRT_MAX) + 1;
} else {
val = (unsigned short)(-v);
}
}
if (sizeof(short) < sizeof(long long)) {
return _qrintf_chk_long_core(ctx, fill_ch, width, (unsigned long)val, sign);
}
Expand All @@ -967,15 +897,8 @@ static inline qrintf_chk_t _qrintf_chk_width_hd(qrintf_chk_t ctx, int fill_ch, i

static inline qrintf_chk_t _qrintf_chk_d(qrintf_chk_t ctx, int v)
{
unsigned int val = v;
unsigned int val = v >= 0 ? v : -(unsigned int)v;
int sign = v < 0;
if (v < 0) {
if (v == INT_MIN) {
val = (unsigned int)(INT_MAX) + 1;
} else {
val = (unsigned int)(-v);
}
}
if (sizeof(int) < sizeof(long long)) {
return _qrintf_chk_long_core(ctx, 0, 0, (unsigned long)val, sign);
}
Expand All @@ -987,15 +910,8 @@ static inline qrintf_chk_t _qrintf_chk_d(qrintf_chk_t ctx, int v)

static inline qrintf_chk_t _qrintf_chk_width_d(qrintf_chk_t ctx, int fill_ch, int width, int v)
{
unsigned int val = v;
unsigned int val = v >= 0 ? v : -(unsigned int)v;
int sign = v < 0;
if (v < 0) {
if (v == INT_MIN) {
val = (unsigned int)(INT_MAX) + 1;
} else {
val = (unsigned int)(-v);
}
}
if (sizeof(int) < sizeof(long long)) {
return _qrintf_chk_long_core(ctx, fill_ch, width, (unsigned long)val, sign);
}
Expand All @@ -1007,15 +923,8 @@ static inline qrintf_chk_t _qrintf_chk_width_d(qrintf_chk_t ctx, int fill_ch, in

static inline qrintf_chk_t _qrintf_chk_ld(qrintf_chk_t ctx, long v)
{
unsigned long val = v;
unsigned long val = v >= 0 ? v : -(unsigned long)v;
int sign = v < 0;
if (v < 0) {
if (v == LONG_MIN) {
val = (unsigned long)(LONG_MAX) + 1;
} else {
val = (unsigned long)(-v);
}
}
if (sizeof(long) < sizeof(long long)) {
return _qrintf_chk_long_core(ctx, 0, 0, (unsigned long)val, sign);
}
Expand All @@ -1027,15 +936,8 @@ static inline qrintf_chk_t _qrintf_chk_ld(qrintf_chk_t ctx, long v)

static inline qrintf_chk_t _qrintf_chk_width_ld(qrintf_chk_t ctx, int fill_ch, int width, long v)
{
unsigned long val = v;
unsigned long val = v >= 0 ? v : -(unsigned long)v;
int sign = v < 0;
if (v < 0) {
if (v == LONG_MIN) {
val = (unsigned long)(LONG_MAX) + 1;
} else {
val = (unsigned long)(-v);
}
}
if (sizeof(long) < sizeof(long long)) {
return _qrintf_chk_long_core(ctx, fill_ch, width, (unsigned long)val, sign);
}
Expand All @@ -1047,15 +949,8 @@ static inline qrintf_chk_t _qrintf_chk_width_ld(qrintf_chk_t ctx, int fill_ch, i

static inline qrintf_chk_t _qrintf_chk_lld(qrintf_chk_t ctx, long long v)
{
unsigned long long val = v;
unsigned long long val = v >= 0 ? v : -(unsigned long long)v;
int sign = v < 0;
if (v < 0) {
if (v == LLONG_MIN) {
val = (unsigned long long)(LLONG_MAX) + 1;
} else {
val = (unsigned long long)(-v);
}
}
if (sizeof(long long) < sizeof(long long)) {
return _qrintf_chk_long_core(ctx, 0, 0, (unsigned long)val, sign);
}
Expand All @@ -1067,15 +962,8 @@ static inline qrintf_chk_t _qrintf_chk_lld(qrintf_chk_t ctx, long long v)

static inline qrintf_chk_t _qrintf_chk_width_lld(qrintf_chk_t ctx, int fill_ch, int width, long long v)
{
unsigned long long val = v;
unsigned long long val = v >= 0 ? v : -(unsigned long long)v;
int sign = v < 0;
if (v < 0) {
if (v == LLONG_MIN) {
val = (unsigned long long)(LLONG_MAX) + 1;
} else {
val = (unsigned long long)(-v);
}
}
if (sizeof(long long) < sizeof(long long)) {
return _qrintf_chk_long_core(ctx, fill_ch, width, (unsigned long)val, sign);
}
Expand Down

0 comments on commit 3cf4fc8

Please sign in to comment.