Updated random interface further.

This commit is contained in:
Christoffer Lerno
2023-08-26 13:22:02 +02:00
parent bea9ac010c
commit b1f52cf8a9
4 changed files with 12 additions and 7 deletions

View File

@@ -297,7 +297,7 @@ macro bool @ok(#expr) @builtin
return true;
}
macro char[] @to_byte_view(&value) @builtin
macro char[] @as_char_view(&value) @builtin
{
return ((char*)value)[:$sizeof(*value)];
}

View File

@@ -78,7 +78,7 @@ macro bool is_numerical($Type)
{
var $kind = $Type.kindof;
$if $kind == TypeKind.DISTINCT:
return is_numerical($Type.inner);
return is_numerical($typefrom($Type.inner));
$else
return $kind == TypeKind.SIGNED_INT || $kind == TypeKind.UNSIGNED_INT || $kind == TypeKind.FLOAT
|| $kind == TypeKind.VECTOR;

View File

@@ -76,21 +76,27 @@ fn uint128 Random.next_uint128(&self)
/**
* @param [&inout] self
* @require types::is_numerical($typeof(seed))
**/
fn void Random.set_seed(&self, long seed)
macro void Random.seed_random(&self, seed)
{
self.fns.seed_fn(self, &&bitcast(seed, char[8])) @inline;
self.fns.seed_fn(self, @as_char_view(seed)) @inline;
}
/**
* @param [&inout] random
* @param [in] seed
**/
fn void Random.set_seeds(&random, char[] seed)
fn void Random.set_seed(&random, char[] seed)
{
random.fns.seed_fn(random, seed);
}
fn int Random.next(&random, int max)
{
return (int)(random.next_double() * max);
}
/**
* @param [&inout] self
**/
@@ -105,7 +111,6 @@ fn float Random.next_float(&self)
return val / (float)(1 << 24);
}
fn double Random.next_double(&self)
{
ulong val = self.next_long() & (1UL << 53 - 1);

View File

@@ -7,7 +7,7 @@ const MUL_LCG64 @local = 0xd1342543de82ef95;
macro make_seed($Type, char[] input)
{
$Type return_value;
seeder(input, @to_byte_view(return_value));
seeder(input, @as_char_view(return_value));
return return_value;
}