1  /* Copyright (C) 2021-2023 Free Software Foundation, Inc.
       2     Contributed by Oracle.
       3  
       4     This file is part of GNU Binutils.
       5  
       6     This program is free software; you can redistribute it and/or modify
       7     it under the terms of the GNU General Public License as published by
       8     the Free Software Foundation; either version 3, or (at your option)
       9     any later version.
      10  
      11     This program is distributed in the hope that it will be useful,
      12     but WITHOUT ANY WARRANTY; without even the implied warranty of
      13     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
      14     GNU General Public License for more details.
      15  
      16     You should have received a copy of the GNU General Public License
      17     along with this program; if not, write to the Free Software
      18     Foundation, 51 Franklin Street - Fifth Floor, Boston,
      19     MA 02110-1301, USA.  */
      20  
      21  #include <stdio.h>
      22  #include <stdlib.h>
      23  #include "stopwatch.h"
      24  
      25  /* The random number generator below is adapted from Kernighan and Ritchie,
      26   *  "C Programming Language", Second Edition, p. 46.
      27   */
      28  
      29  #define IA 1103515245u
      30  #define IC 12345u
      31  #define IM 2147483648u
      32  
      33  static unsigned int current_random = 1;
      34  
      35  static int
      36  my_irand (int imax)
      37  {
      38    /* Create a random integer between 0 and imax, inclusive.  i.e. [0..imax] */
      39  
      40    /* Use overflow to wrap */
      41    current_random = current_random * IA + IC;
      42    int ival = current_random & (IM - 1); /* Modulus */
      43    ival = (int) ((float) ival * (float) (imax + 0.999) / (float) IM);
      44    return ival;
      45  }
      46  
      47  #define N 6000000
      48  
      49  int
      50  fitos (int n)
      51  {
      52    int i, k, retv;
      53    hrtime_t start = gethrtime ();
      54    hrtime_t vstart = gethrvtime ();
      55  
      56    /* Log the event */
      57    wlog ("start of fitos", NULL);
      58  
      59    if (n <= 0)
      60      n = 1;
      61    int max = N * n;
      62  
      63    long long count = 0;
      64    do
      65      {
      66        for (current_random = 1, i = retv = k = 0; i < max; i++)
      67          {
      68            retv += my_irand (100);
      69            k += (current_random & (IM - 1)) >= (1 << 22);
      70          }
      71        count++;
      72      }
      73    while (start + testtime * 1e9 > gethrtime ());
      74    fprintf (stderr, "\t\t%d out of a total of %d >= 2^22 (%d)\n", k, max, retv);
      75    fprintf (stderr, "   Performed %lld while-loop iterations\n", count);
      76    whrvlog ((gethrtime () - start), (gethrvtime () - vstart), "fitos", NULL);
      77    return 0;
      78  }