Paul Eggert wrote:
> On 7/23/20 3:15 PM, Bruno Haible wrote:
> > + AH_VERBATIM([micro_optimizations],
> > +[/* _GL_CMP (n1, n2) performs a three-valued comparison on n1 vs. n2.
> > + It returns
> > + 1 if n1 > n2
> > + 0 if n1 == n2
> > + -1 if n1 < n2
>
> While looking into using
Florian Weimer wrote:
> int sign4 (long n1, long n2)
> {
> return (char) ((n1 > n2) - (n1 < n2));
> }
>
> It's one instruction shorter on x86-64 than sign3, but it's worse on
> other architectures.
Yes. In particular with floating-point numbers (and GCC 5), when I compare
int sign3 (double n1
On 7/23/20 3:15 PM, Bruno Haible wrote:
+ AH_VERBATIM([micro_optimizations],
+[/* _GL_CMP (n1, n2) performs a three-valued comparison on n1 vs. n2.
+ It returns
+ 1 if n1 > n2
+ 0 if n1 == n2
+ -1 if n1 < n2
While looking into using this in Emacs I noticed that _GL_CMP (A, B) c
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
Florian Weimer wrote:
> On s390x, all three variants use conditional
> branches, but the first one is the shortest.
Indeed. Surprising.
> There is also this:
>
> int sign4 (long n1, long n2)
> {
> return (char) ((n1 > n2) - (n1 < n2));
> }
The case should be towards (signed char). Otherwise,
* Bruno Haible:
> A comment in Luca Saiu 'jitter' project [1] brought my attention to 3-valued
> comparison and the book "Hacker's Delight".
>
> ===
> int sign1 (long n1, long n2)
> {
> return (n1 > n2 ? 1 : n1 < n2 ? -1 : 0);
> }
>
> int sign2 (long n
printsec=frontcover&dq=%22hacker%27s+delight%22&hl=en&sa=X&ved=2ahUKEwjN7_LSouHqAhVJKewKHREQBZ8Q6AEwAXoECAMQAg#v=onepage&q&f=false
2020-07-23 Bruno Haible
Optimize three-valued comparison between integers.
(a > b ? 1 : a < b ? -1 : 0) is the same as