Re: Optimize three-valued comparison between integers

2020-07-31 Thread Bruno Haible
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

Re: Optimize three-valued comparison between integers

2020-07-31 Thread Bruno Haible
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

Re: Optimize three-valued comparison between integers

2020-07-30 Thread Paul Eggert
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)

Re: Optimize three-valued comparison between integers

2020-07-25 Thread Bruno Haible
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:

Re: Optimize three-valued comparison between integers

2020-07-24 Thread Bruno Haible
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,

Re: Optimize three-valued comparison between integers

2020-07-24 Thread Florian Weimer
* 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

Optimize three-valued comparison between integers

2020-07-23 Thread Bruno Haible
22hacker%27s+delight%22=en=X=2ahUKEwjN7_LSouHqAhVJKewKHREQBZ8Q6AEwAXoECAMQAg#v=onepage=false 2020-07-23 Bruno Haible Optimize three-valued comparison between integers. (a > b ? 1 : a < b ? -1 : 0) is the same as (a > b) - (a < b). * m4/gnulib-common.m4 (gl_