Commit 84058343bee31432ca52c423187ee558984d2a0e

Thomas de Grivel 2024-03-03T16:54:23

wip random_uniform

diff --git a/libc3/skiplist.c.in b/libc3/skiplist.c.in
index f39a420..4559713 100644
--- a/libc3/skiplist.c.in
+++ b/libc3/skiplist.c.in
@@ -17,6 +17,7 @@
 #include "skiplist_node___NAME$.h"
 #include "skiplist___NAME$.h"
 #include "u32.h"
+#include "u64.h"
 
 /*
   Random height
diff --git a/libc3/skiplist__fact.c b/libc3/skiplist__fact.c
index 6387867..706bfeb 100644
--- a/libc3/skiplist__fact.c
+++ b/libc3/skiplist__fact.c
@@ -17,6 +17,7 @@
 #include "skiplist_node__fact.h"
 #include "skiplist__fact.h"
 #include "u32.h"
+#include "u64.h"
 
 /*
   Random height
diff --git a/libc3/u.c.in b/libc3/u.c.in
index d2484b8..aeca793 100644
--- a/libc3/u.c.in
+++ b/libc3/u.c.in
@@ -14,6 +14,7 @@
 #include "assert.h"
 #include <math.h>
 #include <stdlib.h>
+#include "f128.h"
 #include "integer.h"
 #include "ratio.h"
 #include "tag.h"
@@ -94,16 +95,15 @@ u_bits$ * u_bits$_random (u_bits$ *u)
   return u;
 }
 
-#if _bits$ > 32
+#if _bits$ == 64 || _bits$ == w
 
 u_bits$ * u_bits$_random_uniform (u_bits$ *u, u_bits$ max)
 {
-  // TODO
-  (void) u;
-  (void) max;
-  err_puts("u_bits$_random_uniform: not implemented");
-  assert(! "u_bits$_random_uniform: not implemented");
-  return NULL;
+  f128 x;
+  f128_random(&x);
+  x *= max;
+  *u = (u_bits$) x;
+  return u;
 }
 
 #else
diff --git a/libc3/u16.c b/libc3/u16.c
index f083838..d586c41 100644
--- a/libc3/u16.c
+++ b/libc3/u16.c
@@ -14,6 +14,7 @@
 #include "assert.h"
 #include <math.h>
 #include <stdlib.h>
+#include "f128.h"
 #include "integer.h"
 #include "ratio.h"
 #include "tag.h"
@@ -94,16 +95,15 @@ u16 * u16_random (u16 *u)
   return u;
 }
 
-#if 16 > 32
+#if 16 == 64 || 16 == w
 
 u16 * u16_random_uniform (u16 *u, u16 max)
 {
-  // TODO
-  (void) u;
-  (void) max;
-  err_puts("u16_random_uniform: not implemented");
-  assert(! "u16_random_uniform: not implemented");
-  return NULL;
+  f128 x;
+  f128_random(&x);
+  x *= max;
+  *u = (u16) x;
+  return u;
 }
 
 #else
diff --git a/libc3/u32.c b/libc3/u32.c
index 143c6ca..2c8d06b 100644
--- a/libc3/u32.c
+++ b/libc3/u32.c
@@ -14,6 +14,7 @@
 #include "assert.h"
 #include <math.h>
 #include <stdlib.h>
+#include "f128.h"
 #include "integer.h"
 #include "ratio.h"
 #include "tag.h"
@@ -94,16 +95,15 @@ u32 * u32_random (u32 *u)
   return u;
 }
 
-#if 32 > 32
+#if 32 == 64 || 32 == w
 
 u32 * u32_random_uniform (u32 *u, u32 max)
 {
-  // TODO
-  (void) u;
-  (void) max;
-  err_puts("u32_random_uniform: not implemented");
-  assert(! "u32_random_uniform: not implemented");
-  return NULL;
+  f128 x;
+  f128_random(&x);
+  x *= max;
+  *u = (u32) x;
+  return u;
 }
 
 #else
diff --git a/libc3/u64.c b/libc3/u64.c
index e284b2e..0c01e93 100644
--- a/libc3/u64.c
+++ b/libc3/u64.c
@@ -14,6 +14,7 @@
 #include "assert.h"
 #include <math.h>
 #include <stdlib.h>
+#include "f128.h"
 #include "integer.h"
 #include "ratio.h"
 #include "tag.h"
@@ -94,16 +95,15 @@ u64 * u64_random (u64 *u)
   return u;
 }
 
-#if 64 > 32
+#if 64 == 64 || 64 == w
 
 u64 * u64_random_uniform (u64 *u, u64 max)
 {
-  // TODO
-  (void) u;
-  (void) max;
-  err_puts("u64_random_uniform: not implemented");
-  assert(! "u64_random_uniform: not implemented");
-  return NULL;
+  f128 x;
+  f128_random(&x);
+  x *= max;
+  *u = (u64) x;
+  return u;
 }
 
 #else
diff --git a/libc3/u8.c b/libc3/u8.c
index a7b5b11..337ccce 100644
--- a/libc3/u8.c
+++ b/libc3/u8.c
@@ -14,6 +14,7 @@
 #include "assert.h"
 #include <math.h>
 #include <stdlib.h>
+#include "f128.h"
 #include "integer.h"
 #include "ratio.h"
 #include "tag.h"
@@ -94,16 +95,15 @@ u8 * u8_random (u8 *u)
   return u;
 }
 
-#if 8 > 32
+#if 8 == 64 || 8 == w
 
 u8 * u8_random_uniform (u8 *u, u8 max)
 {
-  // TODO
-  (void) u;
-  (void) max;
-  err_puts("u8_random_uniform: not implemented");
-  assert(! "u8_random_uniform: not implemented");
-  return NULL;
+  f128 x;
+  f128_random(&x);
+  x *= max;
+  *u = (u8) x;
+  return u;
 }
 
 #else
diff --git a/libc3/uw.c b/libc3/uw.c
index 1089e58..d44392d 100644
--- a/libc3/uw.c
+++ b/libc3/uw.c
@@ -14,6 +14,7 @@
 #include "assert.h"
 #include <math.h>
 #include <stdlib.h>
+#include "f128.h"
 #include "integer.h"
 #include "ratio.h"
 #include "tag.h"
@@ -94,16 +95,15 @@ uw * uw_random (uw *u)
   return u;
 }
 
-#if w > 32
+#if w == 64 || w == w
 
 uw * uw_random_uniform (uw *u, uw max)
 {
-  // TODO
-  (void) u;
-  (void) max;
-  err_puts("uw_random_uniform: not implemented");
-  assert(! "uw_random_uniform: not implemented");
-  return NULL;
+  f128 x;
+  f128_random(&x);
+  x *= max;
+  *u = (uw) x;
+  return u;
 }
 
 #else