-
Notifications
You must be signed in to change notification settings - Fork 48
/
Rand.scala
86 lines (75 loc) · 2.17 KB
/
Rand.scala
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
package torture
import scala.util.Random
import scala.collection.mutable.ArrayBuffer
object Rand
{
def rand_word: Int = Random.nextInt
def rand_dword: Long = Random.nextLong
def rand_range(low: Int, high: Int): Int =
{
var span = high - low + 1
if (low > high) span = low - high + 1
low + Random.nextInt(span)
}
def rand_shamt() = rand_range(0, 63)
def rand_shamtw() = rand_range(0, 31)
def rand_seglen() = rand_range(0, 7)
def rand_imm() = rand_range(-2048, 2047)
def rand_bigimm() = rand_range(0, 1048575)
def rand_addr_b(memsize: Int) = rand_range(0, memsize-1)
def rand_addr_h(memsize: Int) = rand_range(0, memsize-1) & ~1
def rand_addr_w(memsize: Int) = rand_range(0, memsize-1) & ~3
def rand_addr_d(memsize: Int) = rand_range(0, memsize-1) & ~7
def rand_filter(rand: () => Int, filter: (Int) => Boolean) =
{
var res = rand()
while (!filter(res)) res = rand()
res
}
def rand_pick[T](array: ArrayBuffer[T]) =
{
array(rand_range(0, array.length-1))
}
def rand_permute[T](array: ArrayBuffer[T]) =
{
for (i <- 0 to array.length-1)
{
val j = rand_range(0, array.length-1)
val t = array(i)
array(i) = array(j)
array(j) = t
}
}
def rand_biased: Long =
{
val value = rand_dword
val s = rand_range(0, 17)
if (s < 9)
{
val small = rand_range(0, 9).toLong
s match
{
// return a value with a single bit set
case 0 => (1 << value & 63)
case 1 => (1 << value & 63)
// return a valueue with a single bit clear
case 2 => ~(1 << value & 63)
case 3 => ~(1 << value & 63)
// return a small integer around zero
case 4 => small
// return a very large/very small 8b signed number
case 5 => ((0x80L + small) << 56) >> 56
// return a very large/very small 16b signed number
case 6 => ((0x8000L + small) << 48) >> 48
// return a very large/very small 32b signed number
case 7 => ((0x80000000L + small) << 32) >> 32
// return a very large/very small 64b signed number
case 8 => 0x800000000000000L + small
}
}
else
{
value
}
}
}