public inbox for gcc-help@gcc.gnu.org
 help / color / mirror / Atom feed
* Should rand() return a RAND_MAX value for 32 bit target?
@ 2018-09-18 12:22 Neha Gowda
  2018-09-18 13:09 ` Xi Ruoyao
  2018-09-18 13:46 ` Andrew Haley
  0 siblings, 2 replies; 17+ messages in thread
From: Neha Gowda @ 2018-09-18 12:22 UTC (permalink / raw)
  To: gcc-help

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 <stdio.h>
#include <stdlib.h>

int main()
{
    unsigned int i;
    float f;
    srand(0);
    for (i = 0; i <= RAND_MAX; i++) {
        f = rand();
        if (f == RAND_MAX) {
                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?

Thanks,
Neha

^ permalink raw reply	[flat|nested] 17+ messages in thread

end of thread, other threads:[~2018-09-22 22:12 UTC | newest]

Thread overview: 17+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2018-09-18 12:22 Should rand() return a RAND_MAX value for 32 bit target? Neha Gowda
2018-09-18 13:09 ` Xi Ruoyao
2018-09-18 13:17   ` Xi Ruoyao
2018-09-18 13:49   ` Jonathan Wakely
2018-09-18 13:46 ` Andrew Haley
2018-09-20 14:42   ` Vincent Lefevre
2018-09-20 15:05     ` Vincent Lefevre
2018-09-20 15:21       ` Liu Hao
2018-09-20 15:58         ` Vincent Lefevre
2018-09-20 16:12           ` Vincent Lefevre
2018-09-21  2:49           ` Liu Hao
2018-09-22  8:17             ` Vincent Lefevre
2018-09-22 10:08               ` Vincent Lefevre
2018-09-22 13:52                 ` Liu Hao
2018-09-22 22:12                   ` Vincent Lefevre
2018-09-20 15:46       ` Dennis Clarke
2018-09-20 16:16         ` Vincent Lefevre

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).