tlds
Transactional Operations for Linked Data Structures
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
rand_r_32.h
Go to the documentation of this file.
1 /* Reentrant random function frm POSIX.1c.
2  Copyright (C) 1996, 1999 Free Software Foundation, Inc.
3  This file is part of the GNU C Library.
4  Contributed by Ulrich Drepper <drepper@cygnus.com>, 1996.
5 
6  The GNU C Library is free software; you can redistribute it and/or
7  modify it under the terms of the GNU Lesser General Public
8  License as published by the Free Software Foundation; either
9  version 2.1 of the License, or (at your option) any later version.
10 
11  The GNU C Library is distributed in the hope that it will be useful,
12  but WITHOUT ANY WARRANTY; without even the implied warranty of
13  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14  Lesser General Public License for more details.
15 
16  You should have received a copy of the GNU Lesser General Public
17  License along with the GNU C Library; if not, write to the Free
18  Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
19  02111-1307 USA. */
20 
21 #ifndef RAND_R_32_H__
22 #define RAND_R_32_H__
23 
24 #include <stdlib.h>
25 #include <stm/config.h>
26 
27 /* This algorithm is mentioned in the ISO C standard, here extended
28  for 32 bits. */
29 
30 /**
31  * Changes:
32  *
33  * The file/function name has been changed from rand_r to rand_r_32.
34  *
35  * Added attributes for transactional use.
36  *
37  * Cloned the function to handle the case where the seed is volatile
38  */
39 #if defined(STM_API_CXXTM)
40 [[transaction_safe]]
41 #endif
42 inline int
43 rand_r_32 (unsigned int *seed)
44 {
45  unsigned int next = *seed;
46  int result;
47 
48  next *= 1103515245;
49  next += 12345;
50  result = (unsigned int) (next / 65536) % 2048;
51 
52  next *= 1103515245;
53  next += 12345;
54  result <<= 10;
55  result ^= (unsigned int) (next / 65536) % 1024;
56 
57  next *= 1103515245;
58  next += 12345;
59  result <<= 10;
60  result ^= (unsigned int) (next / 65536) % 1024;
61 
62  *seed = next;
63 
64  return result;
65 }
66 
67 #if defined(STM_API_CXXTM)
68 [[transaction_safe]]
69 #endif
70 inline int
71 rand_r_32 (volatile unsigned int *seed)
72 {
73  unsigned int next = *seed;
74  int result;
75 
76  next *= 1103515245;
77  next += 12345;
78  result = (unsigned int) (next / 65536) % 2048;
79 
80  next *= 1103515245;
81  next += 12345;
82  result <<= 10;
83  result ^= (unsigned int) (next / 65536) % 1024;
84 
85  next *= 1103515245;
86  next += 12345;
87  result <<= 10;
88  result ^= (unsigned int) (next / 65536) % 1024;
89 
90  *seed = next;
91 
92  return result;
93 }
94 
95 #endif // RAND_R_32_H__
static int seed
Definition: mesh.cpp:40
int rand_r_32(unsigned int *seed)
Definition: rand_r_32.h:43