usr/src/lib/libc/port/gen/drand48.c
changeset 6812 febeba71273d
parent 0 68f95e015346
equal deleted inserted replaced
6811:2ed8737b86b7 6812:febeba71273d
     1 /*
     1 /*
     2  * CDDL HEADER START
     2  * CDDL HEADER START
     3  *
     3  *
     4  * The contents of this file are subject to the terms of the
     4  * The contents of this file are subject to the terms of the
     5  * Common Development and Distribution License, Version 1.0 only
     5  * Common Development and Distribution License (the "License").
     6  * (the "License").  You may not use this file except in compliance
     6  * You may not use this file except in compliance with the License.
     7  * with the License.
       
     8  *
     7  *
     9  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
     8  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
    10  * or http://www.opensolaris.org/os/licensing.
     9  * or http://www.opensolaris.org/os/licensing.
    11  * See the License for the specific language governing permissions
    10  * See the License for the specific language governing permissions
    12  * and limitations under the License.
    11  * and limitations under the License.
    17  * fields enclosed by brackets "[]" replaced with your own identifying
    16  * fields enclosed by brackets "[]" replaced with your own identifying
    18  * information: Portions Copyright [yyyy] [name of copyright owner]
    17  * information: Portions Copyright [yyyy] [name of copyright owner]
    19  *
    18  *
    20  * CDDL HEADER END
    19  * CDDL HEADER END
    21  */
    20  */
    22 /*
    21 
    23  * Copyright 2004 Sun Microsystems, Inc.  All rights reserved.
    22 /*
       
    23  * Copyright 2008 Sun Microsystems, Inc.  All rights reserved.
    24  * Use is subject to license terms.
    24  * Use is subject to license terms.
    25  */
    25  */
    26 
       
    27 #pragma ident	"%Z%%M%	%I%	%E% SMI"
       
    28 
    26 
    29 /*	Copyright (c) 1988 AT&T	*/
    27 /*	Copyright (c) 1988 AT&T	*/
    30 /*	  All Rights Reserved  	*/
    28 /*	  All Rights Reserved  	*/
    31 
    29 
       
    30 #pragma ident	"%Z%%M%	%I%	%E% SMI"
    32 
    31 
    33 /*
    32 /*
    34  *	drand48, etc. pseudo-random number generator
    33  *	drand48, etc. pseudo-random number generator
    35  *	This implementation assumes unsigned short integers of at least
    34  *	This implementation assumes unsigned short integers of at least
    36  *	16 bits, long integers of at least 32 bits, and ignores
    35  *	16 bits, long integers of at least 32 bits, and ignores
    45  *	Longs are now 64 bits, and we are bound by standards to return
    44  *	Longs are now 64 bits, and we are bound by standards to return
    46  *	type long, hovever all internal calculations where long was
    45  *	type long, hovever all internal calculations where long was
    47  *	previously used (32 bit precision) are now using the int32_t
    46  *	previously used (32 bit precision) are now using the int32_t
    48  *	type (32 bit precision in both ILP32 and LP64 worlds).
    47  *	type (32 bit precision in both ILP32 and LP64 worlds).
    49  */
    48  */
    50 #pragma weak drand48 = _drand48
    49 
    51 #pragma weak erand48 = _erand48
    50 #include "lint.h"
    52 #pragma weak lrand48 = _lrand48
       
    53 #pragma weak mrand48 = _mrand48
       
    54 #pragma weak srand48 = _srand48
       
    55 #pragma weak seed48 = _seed48
       
    56 #pragma weak lcong48 = _lcong48
       
    57 #pragma weak nrand48 = _nrand48
       
    58 #pragma weak jrand48 = _jrand48
       
    59 
       
    60 #include "synonyms.h"
       
    61 #include <mtlib.h>
    51 #include <mtlib.h>
    62 #include <synch.h>
    52 #include <synch.h>
    63 #include <thread.h>
    53 #include <thread.h>
    64 
    54 
    65 static mutex_t seed_lock = DEFAULTMUTEX;
    55 static mutex_t seed_lock = DEFAULTMUTEX;
   146 	ADDEQU(p[1], carry0, carry1);
   136 	ADDEQU(p[1], carry0, carry1);
   147 	MUL(a[0], x[1], q);
   137 	MUL(a[0], x[1], q);
   148 	ADDEQU(p[1], q[0], carry0);
   138 	ADDEQU(p[1], q[0], carry0);
   149 	MUL(a[1], x[0], r);
   139 	MUL(a[1], x[0], r);
   150 	x[2] = LOW(carry0 + carry1 + CARRY(p[1], r[0]) + q[1] + r[1] +
   140 	x[2] = LOW(carry0 + carry1 + CARRY(p[1], r[0]) + q[1] + r[1] +
   151 		a[0] * x[2] + a[1] * x[1] + a[2] * x[0]);
   141 	    a[0] * x[2] + a[1] * x[1] + a[2] * x[0]);
   152 	x[1] = LOW(p[1] + r[0]);
   142 	x[1] = LOW(p[1] + r[0]);
   153 	x[0] = LOW(p[0]);
   143 	x[0] = LOW(p[0]);
   154 }
   144 }
   155 
   145 
   156 void
   146 void
   157 _srand48(long seedval)
   147 srand48(long seedval)
   158 {
   148 {
   159 	int32_t fixseed = (int32_t)seedval;	/* limit to 32 bits */
   149 	int32_t fixseed = (int32_t)seedval;	/* limit to 32 bits */
   160 
   150 
   161 	lmutex_lock(&seed_lock);
   151 	lmutex_lock(&seed_lock);
   162 	SEED(X0, LOW(fixseed), HIGH(fixseed));
   152 	SEED(X0, LOW(fixseed), HIGH(fixseed));
   185 
   175 
   186 NEST(long, _nrand48_u, _lrand48_u)
   176 NEST(long, _nrand48_u, _lrand48_u)
   187 
   177 
   188 NEST(long, _jrand48_u, _mrand48_u)
   178 NEST(long, _jrand48_u, _mrand48_u)
   189 
   179 
   190 EXPORT0(double, _drand48, _drand48_u)
   180 EXPORT0(double, drand48, _drand48_u)
   191 EXPORT1(double, _erand48, _erand48_u)
   181 EXPORT1(double, erand48, _erand48_u)
   192 
   182 
   193 EXPORT0(long, _lrand48, _lrand48_u)
   183 EXPORT0(long, lrand48, _lrand48_u)
   194 EXPORT1(long, _nrand48, _nrand48_u)
   184 EXPORT1(long, nrand48, _nrand48_u)
   195 
   185 
   196 EXPORT0(long, _mrand48, _mrand48_u)
   186 EXPORT0(long, mrand48, _mrand48_u)
   197 EXPORT1(long, _jrand48, _jrand48_u)
   187 EXPORT1(long, jrand48, _jrand48_u)
   198 
   188 
   199 #ifdef DRIVER
   189 #ifdef DRIVER
   200 /*
   190 /*
   201  *	This should print the sequences of integers in Tables 2
   191  *	This should print the sequences of integers in Tables 2
   202  *		and 1 of the TM:
   192  *		and 1 of the TM: