From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: (qmail 101592 invoked by alias); 18 Sep 2018 13:09:18 -0000 Mailing-List: contact gcc-help-help@gcc.gnu.org; run by ezmlm Precedence: bulk List-Id: List-Archive: List-Post: List-Help: Sender: gcc-help-owner@gcc.gnu.org Received: (qmail 101582 invoked by uid 89); 18 Sep 2018 13:09:18 -0000 Authentication-Results: sourceware.org; auth=none X-Spam-SWARE-Status: No, score=-1.1 required=5.0 tests=BAYES_00,KAM_ASCII_DIVIDERS,KAM_SHORT,SPF_HELO_PASS,SPF_PASS autolearn=no version=3.3.2 spammy=science, Aerospace, aerospace, H*r:2001 X-HELO: mengyan1223.wang Received: from mengyan1223.wang (HELO mengyan1223.wang) (89.208.246.23) by sourceware.org (qpsmtpd/0.93/v0.84-503-g423c35a) with ESMTP; Tue, 18 Sep 2018 13:09:16 +0000 Received: from [IPv6:ddf7:6a95:58c8::95e] (unknown [IPv6:2001:250:1006:dff0:c5c4:546e:2203:2b91]) (using TLSv1.2 with cipher ECDHE-ECDSA-AES256-GCM-SHA384 (256/256 bits)) (Client did not present a certificate) (Authenticated sender: xry111@mengyan1223.wang) by mengyan1223.wang (Postfix) with ESMTPSA id 8386D667CF; Tue, 18 Sep 2018 09:09:14 -0400 (EDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=mengyan1223.wang; s=mail; t=1537276155; bh=5qUeVAcJjjhTRKhoIXFIIKvcJTv69cRXkCrDONYRzRY=; h=Subject:From:To:Cc:Date:In-Reply-To:References:From; b=wOqw+LQ1jmYDNQKiKeTq5KXym/TBUzO/2viBcnD88tbFWT00UwN6jTL61azrFD4M5 IUBWIMiqg70K6YcraJK5mx0GZ+dgRfQIrxytmWphwcDS2zFjeATWynnE0Yk6Ft+s4M PSbu45dS7I0ssBW0ATUSr/y+T0EN3oZwaPjB9nA86Ns6Zt3dRF2Y/tbSArurROcGL6 vL/4M5caf1S+eJmondZx+3jgR8hCUIUlni5EKwHLaPLoiVYuG9BmRinBvA9EosLSPA voXXE64Xjbpsy8pNnhGW6yMhLZhr7MTwqLScIfG0MGo61HNseNogHhP71V6ckiBH0E 1dk90Opwp/DCg== Message-ID: <4f0b1402f9c0dd7ecec3fc0b23ae46bc5387af2d.camel@mengyan1223.wang> Subject: Re: Should rand() return a RAND_MAX value for 32 bit target? From: Xi Ruoyao To: Neha Gowda Cc: gcc-help@gcc.gnu.org Date: Tue, 18 Sep 2018 13:09:00 -0000 In-Reply-To: References: Content-Type: text/plain; charset="UTF-8" Mime-Version: 1.0 Content-Transfer-Encoding: 7bit X-IsSubscribed: yes X-SW-Source: 2018-09/txt/msg00050.txt.bz2 On 2018-09-18 17:52 +0530, Neha Gowda wrote: > Hi, > > I am trying to run the following testcase for Random Number Generator > function. > The testcase returns true when generated number is RAND_MAX and returns > false when its not generated. > > Command :- gcc -m32 -O2 test.c > ====================================================== > #include > #include > > int main() > { > unsigned int i; > float f; > srand(0); > for (i = 0; i <= RAND_MAX; i++) { > f = rand(); There is a rounding error introduced since a float can not represent all integer values in [0, RAND_MAX]. > if (f == RAND_MAX) { RAND_MAX is 2147483647 on 32-bit GNU/Linux. It can not be represented by a float. > printf("True\n"); > return 0; > } > } > printf("False\n"); > return 1; > } > ====================================================== > Tried them on the latest source and observed that the RAND_MAX is generated > when > the optimization is not enabled. When optimization is enabled for 32 bit > target RAND_MAX > is not being generated. However, 64 bit generates RAND_MAX with or without > optimization. > > On further investigation, I found that the expand phase is optimizing the > following and hence > the RAND_MAX value is not being generated. > ========================================= > Replacing Expressions > f_10 replace with --> f_10 = (float) _1; > ========================================= > > Can you please let me know if its the expected behavior or some bug? See https://gcc.gnu.org/wiki/FAQ#PR323. -- Xi Ruoyao School of Aerospace Science and Technology, Xidian University