delorie.com/archives/browse.cgi   search  
Mail Archives: cygwin/2017/06/19/07:09:54

X-Recipient: archive-cygwin AT delorie DOT com
DomainKey-Signature: a=rsa-sha1; c=nofws; d=sourceware.org; h=list-id
:list-unsubscribe:list-subscribe:list-archive:list-post
:list-help:sender:from:to:subject:date:message-id:content-type
:content-transfer-encoding:mime-version; q=dns; s=default; b=Hbb
KMgkXb06ae7zi43i8CQoOLP+l36FjffU2/rY8cQsmeee+kNZBmCQy3ayNrh7O8aE
NmeeXpxyH9rx4VaxkgHnrbg7bgAF5AlHH9RJTmq/lgdcsOaXB3YmyV9BYPDdgRNm
z3ZUfrIrQ5AMT2RWsJhBqfBnJl0PxCG91cLjcbvs=
DKIM-Signature: v=1; a=rsa-sha1; c=relaxed; d=sourceware.org; h=list-id
:list-unsubscribe:list-subscribe:list-archive:list-post
:list-help:sender:from:to:subject:date:message-id:content-type
:content-transfer-encoding:mime-version; s=default; bh=c3dQxHGeZ
ZqpAh8+YiOF0clnK1g=; b=LaxbyyqxMmQBx9n70rf4EWcJmP7MGxa/z9ATAUlyf
/llOcKTIhhpj3M4fqShVYoShwh8ZI+QdrrP+2DtSMDnHazEnH/8xlw7ZOx9XFOwV
Ck7+HQHObMPytC/doK2YSrZdeSQJiPeNjElhdmrijzi5HpwrpRYAOV7Lit4GTw/D
fE=
Mailing-List: contact cygwin-help AT cygwin DOT com; run by ezmlm
List-Id: <cygwin.cygwin.com>
List-Subscribe: <mailto:cygwin-subscribe AT cygwin DOT com>
List-Archive: <http://sourceware.org/ml/cygwin/>
List-Post: <mailto:cygwin AT cygwin DOT com>
List-Help: <mailto:cygwin-help AT cygwin DOT com>, <http://sourceware.org/ml/#faqs>
Sender: cygwin-owner AT cygwin DOT com
Mail-Followup-To: cygwin AT cygwin DOT com
Delivered-To: mailing list cygwin AT cygwin DOT com
Authentication-Results: sourceware.org; auth=none
X-Virus-Found: No
X-Spam-SWARE-Status: No, score=-0.3 required=5.0 tests=AWL,BAYES_00,KAM_LAZY_DOMAIN_SECURITY,RCVD_IN_DNSWL_NONE,T_RP_MATCHES_RCVD autolearn=no version=3.3.2 spammy=H*F:D*no, addressee, H*r:15.00.1076.000, H*RU:15.0.1076.9
X-HELO: mailgw1.ngi.no
From: Carl Fredrik Forsberg <Carl DOT Fredrik DOT Forsberg AT ngi DOT no>
To: "cygwin AT cygwin DOT com" <cygwin AT cygwin DOT com>
Subject: Re: bug in lrint [was: FW: Printing long int in C program under cygwin64]
Date: Mon, 19 Jun 2017 11:09:28 +0000
Message-ID: <00fce7dbcc14455b891f050d7b44fe25@CASMBX02.oslo.ngi.no>
x-ms-exchange-transport-fromentityheader: Hosted
MIME-Version: 1.0
X-MIME-Autoconverted: from quoted-printable to 8bit by delorie.com id v5JB9qY8013795

Hello again.
I have performed a few more tests with the program below.
The lrint() family of functions I tested all appear tohave problems with negative numbers. There seem to be an unsigned vs. signed integer problems.

I could not find  cygwin/math/lrint.c  on my Cygwin installation where patches were applied by Corinna Vinschen as discussed below. This probably  reflects that I am not familiar
with processes that go on "behind the scenes".

A workaround I applied to the program was to  add:

#define lrint(x) (long int)(int)lrint(x)

to the program. This seems to work, but I expect it will fail if all the long int digits are needed.

Program:
#include <stdio.h>      /* printf */
#include <math.h>       /* lrint */
// define statement put in here

int main ()
{

  printf ( "long int -1 = %i\n", -1 );
  printf ( "type cast -1 = %li\n", (long int)-1 );
  printf ( "type cast lrint(-1.0) = %li\n", (long int)(int)lrint(-1.0) );
  printf ( "rint(-1.0) = %f\n", rint(-1.0) );
  printf ( "int of rint(-1.4)-0.5 = %i\n", (int)(rint(-1.4) - 0.5) );
  printf ( "lrint(-1.0) = %li\n", lrint(-1.0) );
  printf ( "lrintf(-1.0) = %li\n", lrintf(-1.0) );
  printf ( "lrintl(-1.0) = %li\n", lrintl(-1.0) );
  printf ( "llrintl(-1.0) = %lli\n", llrintl(-1.0) );

  printf ( "(int)lrint(-1.0) = %i\n", (int)lrint(-1.0) );
  printf ( "Typecasting llrint(-1.0) = %lli\n", (long long)(int)llrint(-1.0) );
  printf ( "lrint(1.0) = %li\n", lrint(1.0) );
  printf ( "llrint(1.0) = %lli\n", llrint(1.0) );
  printf ( "Type cast long long unsigned -1.0 = %llu\n", (unsigned long long)-1 );
  return 0;
}

/*compiled by:
gcc -Wall lrint_test.c -o lrint_test.exe

Result:

long int -1 = -1
type cast -1 = -1
type cast lrint(-1.0) = -1
rint(-1.0) = -1.000000
int of rint(-1.4)-0.5 = -1
lrint(-1.0) = 4294967295
lrintf(-1.0) = 4294967295
lrintl(-1.0) = 4294967295
(int)lrint(-1.0) = -1
Typecasting llrint(-1.0) = -1
lrint(1.0) = 1
llrint(1.0) = 1
Type cast long long unsigned -1.0 = 18446744073709551615

Result with #define statement above.
long int -1 = -1
type cast -1 = -1
type cast lrint(-1.0) = -1
rint(-1.0) = -1.000000
int of rint(-1.4)-0.5 = -1
lrint(-1.0) = -1
lrintf(-1.0) = 4294967295
lrintl(-1.0) = 4294967295
llrintl(-1.0) = 4294967295
(int)lrint(-1.0) = -1
Typecasting llrint(-1.0) = -1
lrint(1.0) = 1
llrint(1.0) = 1
Type cast long long unsigned -1.0 = 18446744073709551615

gcc version:
gcc (GCC) 5.4.0
Copyright (C) 2015 Free Software Foundation, Inc.
This is free software; see the source for copying conditions.  There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.*/

On May 24 07:33, Eric Blake wrote:
> On 05/24/2017 07:00 AM, Carl Fredrik Forsberg wrote:
> > type cast lrint(-1.0) = 4294967295
>
> Now that's an interesting one - it may be that cygwin1.dll actually has
> buggy behavior in lrint().  In the source code, cygwin/math/lrint.c is
> dropping down to assembly; it could very well be that the assembly code
> is incorrectly truncating things at 32 bits (where it is just fine for
> 32-bit Cygwin, but wrong for 64-bit):
>
> long lrint (double x)
> {
>   long retval = 0L;
> #if defined(_AMD64_) || defined(__x86_64__) || defined(_X86_) ||
> defined(__i386__)
>   __asm__ __volatile__ ("fistpl %0"  : "=m" (retval) : "t" (x) : "st");
> #elif defined(__arm__) || defined(_ARM_)
>     retval = __lrint_internal(x);
> #endif
>   return retval;
> }
>
> But I'm not an assembly coding expert, so perhaps someone else will spot
> the fix faster.

I just applied a patch to fix this.  Using fistpl is fine and dandy for
Mingw because sizeof(long) == 4, but on 64 bit Cygwin this function
has to use fistpll to account for sizeof(long) == 8.


Thanks,
Corinna

--
Corinna Vinschen                  Please, send mails regarding Cygwin to
Cygwin Maintainer                 cygwin AT cygwin DOT com
Red Hat
The confidentiality or integrity of this message can not be guaranteed following transmission on the Internet. The addressee should be aware of this before using the contents of this message.

--
Problem reports:       http://cygwin.com/problems.html
FAQ:                   http://cygwin.com/faq/
Documentation:         http://cygwin.com/docs.html
Unsubscribe info:      http://cygwin.com/ml/#unsubscribe-simple


- Raw text -


  webmaster     delorie software   privacy  
  Copyright © 2019   by DJ Delorie     Updated Jul 2019