bug-gnulib
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Optimize three-valued comparison between integers


From: Bruno Haible
Subject: Re: Optimize three-valued comparison between integers
Date: Sat, 25 Jul 2020 08:36:30 +0200
User-agent: KMail/5.1.3 (Linux/4.4.0-179-generic; KDE/5.18.0; x86_64; ; )

Florian Weimer wrote:
> On s390x, all three variants use conditional
> branches, but the first one is the shortest.

On s390x in 64-bit mode (at least with gcc-4.9), and for argument types
that are smaller than 64 bits, it is possible to avoid the conditional branches
by coding a 63-bit shift:

int lessthan1 (int a, int b)
{
  return a < b;
}

int lessthan2 (int a, int b)
{
  return - (((long long) a - (long long) b) >> 63);
}

int lessthan3 (int a, int b)
{
  return ((unsigned long long) ((long long) a - (long long) b)) >> 63;
}

lessthan2 and lessthan3 produce branch-free code.

But I would not like to put this info into _GL_CMP. Such things belong in
the compiler's machine description. Maybe a newer version of GCC already
has it? (I don't have a newer GCC on this machine.)

Bruno




reply via email to

[Prev in Thread] Current Thread [Next in Thread]