LCOV - code coverage report
Current view: top level - u8stream/include/u8stream - universal_simple_random.inl (source / functions) Hit Total Coverage
Test: crystal-facet-uml_v1.57.0_covts Lines: 15 15 100.0 %
Date: 2024-04-07 11:14:42 Functions: 4 4 100.0 %

          Line data    Source code
       1             : /* File: universal_simple_random.inl; Copyright and License: see below */
       2             : 
       3             : #include "u8/u8_log.h"
       4             : #include <time.h>
       5             : #include <stdlib.h>
       6             : #include <errno.h>
       7             : #include <stdbool.h>
       8             : 
       9             : extern bool universal_simple_random_initialized;
      10             : 
      11        1886 : static inline void universal_simple_random_init ( universal_simple_random_t *this_ )
      12             : {
      13        1886 :     if ( ! universal_simple_random_initialized )
      14             :     {
      15           1 :         clock_t now = clock();  /* integer represents clocks, to be divided by CLOCKS_PER_SEC */
      16             : #ifdef _WIN32
      17             :         srand( now );
      18             : #else  /* POSIX.1-2001 */
      19           1 :         srandom( now );
      20             : #endif
      21           1 :         universal_simple_random_initialized = true;
      22             :     }
      23             : 
      24        1886 :     (*this_).dummy = 0;  /* prevent warnings on uninitialized usage */
      25        1886 : }
      26             : 
      27        1886 : static inline void universal_simple_random_destroy ( universal_simple_random_t *this_ )
      28             : {
      29        1886 : }
      30             : 
      31        7544 : static inline uint16_t universal_simple_random_get_uint16 ( universal_simple_random_t *this_ )
      32             : {
      33             :     uint16_t result;
      34             : 
      35             : #ifdef _WIN32
      36             :     if ( RAND_MAX >= (uint16_t)(-1) )
      37             :     {
      38             :         result = rand();
      39             :     }
      40             :     else
      41             :     {
      42             :         result = (((uint_fast16_t)rand()) << 8) ^ rand();
      43             :     }
      44             : #else  /* POSIX.1-2001 */
      45             :     if ( RAND_MAX >= (uint16_t)(-1) )
      46             :     {
      47        7544 :         result = random();
      48             :     }
      49             :     else
      50             :     {
      51             :         result = (((uint_fast16_t)random()) << 8) ^ random();
      52             :     }
      53             : #endif
      54             : 
      55             :     /* Note: random() is possibly not available on win32?, preprocessor-if could distinguish __linux__ and _WIN32 */
      56        7544 :     return result;
      57             : }
      58             : 
      59        3772 : static inline uint32_t universal_simple_random_get_uint32 ( universal_simple_random_t *this_ )
      60             : {
      61             :     uint32_t result;
      62             : 
      63             : #ifdef _WIN32
      64             :     if ( RAND_MAX >= (uint32_t)(-1) )
      65             :     {
      66             :         result = rand();
      67             :     }
      68             :     else if ( RAND_MAX >= (uint16_t)(-1) )
      69             :     {
      70             :         result = (((uint_fast32_t)rand()) << 16) ^ rand();
      71             :     }
      72             :     else
      73             :     {
      74             :         result = (((uint_fast32_t)rand()) << 24) ^ (((uint_fast32_t)rand()) << 16)
      75             :                  ^ (((uint_fast32_t)rand()) << 8) ^ rand();
      76             :     }
      77             : #else  /* POSIX.1-2001 */
      78             :     if ( RAND_MAX >= (uint32_t)(-1) )
      79             :     {
      80             :         result = random();
      81             :     }
      82             :     else if ( RAND_MAX >= (uint16_t)(-1) )
      83             :     {
      84        3772 :         result = (((uint_fast32_t)random()) << 16) ^ random();
      85             :     }
      86             :     else
      87             :     {
      88             :         result = (((uint_fast32_t)random()) << 24) ^ (((uint_fast32_t)random()) << 16)
      89             :                  ^ (((uint_fast32_t)random()) << 8) ^ random();
      90             :     }
      91             : #endif
      92             : 
      93             :     /* Note: random() is possibly not available on win32?, preprocessor-if could distinguish __linux__ and _WIN32 */
      94        3772 :     return result;
      95             : }
      96             : 
      97             : 
      98             : /*
      99             : Copyright 2021-2024 Andreas Warnke
     100             : 
     101             : Licensed under the Apache License, Version 2.0 (the "License");
     102             : you may not use this file except in compliance with the License.
     103             : You may obtain a copy of the License at
     104             : 
     105             :     http://www.apache.org/licenses/LICENSE-2.0
     106             : 
     107             : Unless required by applicable law or agreed to in writing, software
     108             : distributed under the License is distributed on an "AS IS" BASIS,
     109             : WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     110             : See the License for the specific language governing permissions and
     111             : limitations under the License.
     112             : */

Generated by: LCOV version 1.16