1 
2 /*============================================================================
3 
4 This C source file is part of TestFloat, Release 3e, a package of programs for
5 testing the correctness of floating-point arithmetic complying with the IEEE
6 Standard for Floating-Point, by John R. Hauser.
7 
8 Copyright 2011, 2012, 2013, 2014 The Regents of the University of California.
9 All rights reserved.
10 
11 Redistribution and use in source and binary forms, with or without
12 modification, are permitted provided that the following conditions are met:
13 
14  1. Redistributions of source code must retain the above copyright notice,
15     this list of conditions, and the following disclaimer.
16 
17  2. Redistributions in binary form must reproduce the above copyright notice,
18     this list of conditions, and the following disclaimer in the documentation
19     and/or other materials provided with the distribution.
20 
21  3. Neither the name of the University nor the names of its contributors may
22     be used to endorse or promote products derived from this software without
23     specific prior written permission.
24 
25 THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS "AS IS", AND ANY
26 EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
27 WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE, ARE
28 DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE FOR ANY
29 DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
30 (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
31 LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
32 ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
33 (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
34 SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
35 
36 =============================================================================*/
37 
38 #include <stdint.h>
39 #include <stdlib.h>
40 #include "platform.h"
41 #include "random.h"
42 
random_ui8(void)43 uint_fast8_t random_ui8( void )
44 {
45 
46     return rand()>>4 & 0xFF;
47 
48 }
49 
random_ui16(void)50 uint_fast16_t random_ui16( void )
51 {
52 
53     return (rand() & 0x0FF0)<<4 | (rand()>>4 & 0xFF);
54 
55 }
56 
random_ui32(void)57 uint_fast32_t random_ui32( void )
58 {
59 
60     return
61           (uint_fast32_t) (rand() & 0x0FF0)<<20
62         | (uint_fast32_t) (rand() & 0x0FF0)<<12
63         | (rand() & 0x0FF0)<<4
64         | (rand()>>4 & 0xFF);
65 
66 }
67 
random_ui64(void)68 uint_fast64_t random_ui64( void )
69 {
70 
71     return (uint_fast64_t) random_ui32()<<32 | random_ui32();
72 
73 }
74 
randomN_ui8(uint_fast8_t N)75 uint_fast8_t randomN_ui8( uint_fast8_t N )
76 {
77     uint_fast8_t scale, z;
78 
79     scale = 0;
80     while ( N < 0x80 ) {
81         ++scale;
82         N <<= 1;
83     }
84     do {
85         z = random_ui8();
86     } while ( N <= z );
87     return z>>scale;
88 
89 }
90 
randomN_ui16(uint_fast16_t N)91 uint_fast16_t randomN_ui16( uint_fast16_t N )
92 {
93     uint_fast16_t scale, z;
94 
95     scale = 0;
96     while ( N < 0x8000 ) {
97         ++scale;
98         N <<= 1;
99     }
100     do {
101         z = random_ui16();
102     } while ( N <= z );
103     return z>>scale;
104 
105 }
106 
randomN_ui32(uint_fast32_t N)107 uint_fast32_t randomN_ui32( uint_fast32_t N )
108 {
109     uint_fast32_t scale, z;
110 
111     scale = 0;
112     while ( N < 0x8000 ) {
113         ++scale;
114         N <<= 1;
115     }
116     do {
117         z = random_ui32();
118     } while ( N <= z );
119     return z>>scale;
120 
121 }
122 
randomN_ui64(uint_fast64_t N)123 uint_fast64_t randomN_ui64( uint_fast64_t N )
124 {
125     uint_fast64_t scale, z;
126 
127     scale = 0;
128     while ( N < 0x8000 ) {
129         ++scale;
130         N <<= 1;
131     }
132     do {
133         z = random_ui64();
134     } while ( N <= z );
135     return z>>scale;
136 
137 }
138 
139