Page MenuHomeFreeBSD

libc: Reimplement the *rand48 family of functions
ClosedPublic

Authored by markj on Sep 8 2025, 7:09 PM.
Tags
None
Referenced Files
Unknown Object (File)
Sat, Oct 11, 12:51 PM
Unknown Object (File)
Sat, Oct 11, 12:51 PM
Unknown Object (File)
Sat, Oct 11, 12:51 PM
Unknown Object (File)
Sat, Oct 11, 12:51 PM
Unknown Object (File)
Sat, Oct 11, 4:42 AM
Unknown Object (File)
Sat, Oct 4, 8:59 AM
Unknown Object (File)
Sun, Sep 28, 4:19 PM
Unknown Object (File)
Thu, Sep 25, 7:47 PM
Subscribers

Details

Summary

Rather than implementing the recurrence using 3 16-bit integers, as was
done in _dorand48() before this patch, provide an equivalent
implementation using 64-bit integers.

For drand48() and erand48(), replace the use of ldexp() with
bit-twiddling assuming IEEE 754 double-precision float layout.

This implementation is significantly faster and requires less code.

Obtained from: https://github.com/apple-oss-distributions/libc

Test Plan

Tested on amd64, i386 (qemu), big-endian powerpc64 (qemu). I spot-checked that the new implementation produces the same values as the old one.

Below are timing differences for a program which generates 100000000 numbers from a random seed. The new implementation is faster in all cases, especially for drand48() and erand48(), which are implemented by converting the LCG output to a floating point number in the range [0, 1).

Using seed: 160565650                                                          
x /tmp/drand48.unmodified                                                      
+ /tmp/drand48.modified                                                        
+-----------------------------------------------------------------------------+
|    +                                                                    x  x|
|    +                                                                    x  x|
|+   +                                                                    x  x|
|+  ++                                                                    x  x|
|+  ++                                                                    x  x|
| |_AM|                                                                   |_A||
+-----------------------------------------------------------------------------+
    N           Min           Max        Median           Avg        Stddev    
x  10          1.06          1.08          1.07          1.07   0.010540926    
+  10          0.54          0.57         0.565         0.559   0.013703203    
Difference at 95.0% confidence                                                 
        -0.511 +/- 0.0114863                                                   
        -47.757% +/- 0.917009%                                                 
        (Student's t, pooled s = 0.0122247)                                    
x /tmp/erand48.unmodified                                                      
+ /tmp/erand48.modified                                                        
+-----------------------------------------------------------------------------+
|+                                                                           x|
|+                                                                           x|
|+                                                                           x|
|+                                                                           x|
|+                                                                           x|
|+                                                                           x|
|+                                                                           x|
|+                                                                           x|
|+                                                                           x|
|+                                                                           x|
|A                                                                           A|
+-----------------------------------------------------------------------------+
    N           Min           Max        Median           Avg        Stddev    
x  10           1.1           1.1           1.1           1.1 2.3405556e-16    
+  10          0.75          0.75          0.75          0.75             0
Difference at 95.0% confidence                                                 
        -0.35 +/- 1.55505e-16                                                  
        -31.8182% +/- 9.63875e-15%                                             
        (Student's t, pooled s = 1.65502e-16)                                  
x /tmp/lrand48.unmodified                                                      
+ /tmp/lrand48.modified                                                        
+-----------------------------------------------------------------------------+
|+                                                                            |                                                                                
|+                                                                     x      |                                                                                
|+                                                                     x      |                                                                                                                                                                                                                                               
|+                                                                     x      |                                                                                                                                                                                                                                               
|+                                                                     x      |
|+                                                                     x      |
|+                                                                     x      |
|+                                                                     x      |
|+                                                                     x      |
|+                                                                     x     x|
|A                                                                   |_A_|    |
+-----------------------------------------------------------------------------+
    N           Min           Max        Median           Avg        Stddev    
x  10          0.35          0.36          0.35         0.351  0.0031622777    
+  10          0.24          0.24          0.24          0.24 5.8513891e-17    
Difference at 95.0% confidence                                                 
        -0.111 +/- 0.002101                                                    
        -31.6239% +/- 0.409282%                                                
        (Student's t, pooled s = 0.00223607)                                   
x /tmp/nrand48.unmodified                                                      
+ /tmp/nrand48.modified                                                        
+-----------------------------------------------------------------------------+
|+                                                                           x|
|+                                                                           x|
|+                                                                           x|
|+                                                                           x|
|+                                                                           x|
|+                                                                           x|
|+                                                                           x|
|+                                                                           x|
|+                                                                           x|
|+                                                                           x|
|A                                                                           A|
+-----------------------------------------------------------------------------+
    N           Min           Max        Median           Avg        Stddev
x  10           0.6           0.6           0.6           0.6 1.1702778e-16
+  10          0.55          0.55          0.55          0.55 1.1702778e-16
Difference at 95.0% confidence                                                 
        -0.05 +/- 1.09959e-16                                                  
        -8.33333% +/- 1.75795e-14%                                             
        (Student's t, pooled s = 1.17028e-16)
x /tmp/mrand48.unmodified                                                      
+ /tmp/mrand48.modified                                                        
+-----------------------------------------------------------------------------+
|+    +                                                                   x   |
|+    +                                                                   x   |
|+    +                                                                   x   |
|+    +                                                                   x   |
|+    +                                                                   x  x|
||__A_|                                                                   MA| |
+-----------------------------------------------------------------------------+
    N           Min           Max        Median           Avg        Stddev
x  10          0.51          0.52          0.51         0.511  0.0031622777
+  10          0.22          0.24          0.23          0.23   0.010540926
Difference at 95.0% confidence                                                 
        -0.281 +/- 0.00731169                                                  
        -54.9902% +/- 1.38295%                                                 
        (Student's t, pooled s = 0.00778175)
x /tmp/jrand48.unmodified                                                      
+ /tmp/jrand48.modified                                                        
+-----------------------------------------------------------------------------+
|+                                                                            |
|+                                                                          x |
|+                                                                          x |
|+                                                                          x |
|+                                                                          x |
|+                                                                          x |
|+                                                                          x |
|+                                                                          x |
|+                                                                          x |
|+                                                                       x  x |
|A                                                                         |A||
+-----------------------------------------------------------------------------+
    N           Min           Max        Median           Avg        Stddev
x  10          0.75          0.76          0.76         0.759  0.0031622777
+  10          0.53          0.53          0.53          0.53 1.1702778e-16
Difference at 95.0% confidence                                                 
        -0.229 +/- 0.002101                                                    
        -30.1713% +/- 0.193294%                                                
        (Student's t, pooled s = 0.00223607)

Diff Detail

Repository
rG FreeBSD src repository
Lint
Lint Not Applicable
Unit
Tests Not Applicable

Event Timeline

markj requested review of this revision.Sep 8 2025, 7:09 PM

Add a STANDARDS section to the man page.

This revision was not accepted when it landed; it landed in state Needs Review.Mon, Oct 6, 7:22 PM
This revision was automatically updated to reflect the committed changes.