[PATCH v8] Cygwin: pipe: Switch pipe mode to blocking mode by default

Corinna Vinschen corinna-cygwin@cygwin.com
Thu Oct 31 17:07:48 GMT 2024


On Nov  1 01:25, Takashi Yano wrote:
> Hi Corinna,
> 
> On Thu, 31 Oct 2024 11:15:59 +0100
> Corinna Vinschen wrote:
> > Hi Takashi,
> > [...]
> > Yes, I will, but this is still puzzeling. While negative shift values
> > are undefined in C, there's this:
> > 
> >   The Intel Pentium SAL instruction (generated by both gcc and Microsoft
> >   C++ to evaluate left-shifts) only uses the bottom five bits of the
> >   shift amount
> > 
> > The last 5 bits of 63 - 54 =   9 are 01001,
> > the last 5 bits of 31 - 54 = -23 are 01001 as well.
> > [...]
> > I get the same result, with and without -O2 (but then again, with -O2
> > the sall instructions are folded into a single instruction again).
> > 
> > Do you get a different result?  Do you run this on an AMD CPU perhaps,
> > and the AMDs implement the SAL instruction differently?
> 
> Please try this:
> 
> #include <stdio.h>
> #include <stdlib.h>
> 
> #define PIPE_BUF 4096
> int
> main (int argc, char **argv)
> {
>   ssize_t avail = atol (argv[1]);
>   unsigned long len1;
> 
>   if (avail < 1)
>     return 0;
>   if (avail == 1)
>     len1 >>= 1;
>   else if (avail >= PIPE_BUF)
>     len1 = avail & ~(PIPE_BUF -1);
>   else
>     len1 = 1 << (31 - __builtin_clzl (avail));
> 
>   printf ("%ld %lu\n", avail, len1);
>   return 0;
> }
> 
> If the test case is compiled without optimization option,
> $ ./a.exe 960
> 960 512
> 
> however, with -O2 option
> $ ./a.exe 960
> 960 0
> 
> I am using gcc (GCC) 12.4.0 of cygwin gcc package.
> 
> It seems that the calcualtion of
>     len1 = 1 << (31 - __builtin_clzl (avail));
> is completely omitted.
> 
> In this case, avail == 1 or 1 < avail < 4096 for the last "else".
> Therefore __builtin_clzl (avail) is always larger thatn 31.
> 
> I guess the compiler ommitted the undefined calculation.

This makes a lot of sense.


Corinna


More information about the Cygwin-patches mailing list