| 1 | #ifndef _COMPAT_RAND_H |
| 2 | #define _COMPAT_RAND_H |
| 3 | |
| 4 | /* |
| 5 | * compat-rand.h |
| 6 | * |
| 7 | * Userspace RCU library - rand/rand_r Compatibility Header |
| 8 | * |
| 9 | * Copyright 1996 - Ulrich Drepper <drepper@cygnus.com > |
| 10 | * Copyright 2013 - Pierre-Luc St-Charles <pierre-luc.st-charles@polymtl.ca> |
| 11 | * |
| 12 | * Note: this file is only used to simplify the code required to |
| 13 | * use the 'rand_r(...)' system function across multiple platforms, |
| 14 | * which might not always be referenced the same way. |
| 15 | * |
| 16 | * This library is free software; you can redistribute it and/or |
| 17 | * modify it under the terms of the GNU Lesser General Public |
| 18 | * License as published by the Free Software Foundation; either |
| 19 | * version 2.1 of the License, or (at your option) any later version. |
| 20 | * |
| 21 | * This library is distributed in the hope that it will be useful, |
| 22 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 23 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 24 | * Lesser General Public License for more details. |
| 25 | * |
| 26 | * You should have received a copy of the GNU Lesser General Public |
| 27 | * License along with this library; if not, write to the Free Software |
| 28 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA |
| 29 | */ |
| 30 | |
| 31 | #ifndef HAVE_RAND_R |
| 32 | /* |
| 33 | * Reentrant random function from POSIX.1c. |
| 34 | * Copyright (C) 1996, 1999 Free Software Foundation, Inc. |
| 35 | * This file is part of the GNU C Library. |
| 36 | * Contributed by Ulrich Drepper <drepper@cygnus.com <mailto:drepper@cygnus.com>>, 1996. |
| 37 | */ |
| 38 | static inline int rand_r(unsigned int *seed) |
| 39 | { |
| 40 | unsigned int next = *seed; |
| 41 | int result; |
| 42 | |
| 43 | next *= 1103515245; |
| 44 | next += 12345; |
| 45 | result = (unsigned int) (next / 65536) % 2048; |
| 46 | |
| 47 | next *= 1103515245; |
| 48 | next += 12345; |
| 49 | result <<= 10; |
| 50 | result ^= (unsigned int) (next / 65536) % 1024; |
| 51 | |
| 52 | next *= 1103515245; |
| 53 | next += 12345; |
| 54 | result <<= 10; |
| 55 | result ^= (unsigned int) (next / 65536) % 1024; |
| 56 | |
| 57 | *seed = next; |
| 58 | |
| 59 | return result; |
| 60 | } |
| 61 | #endif /* HAVE_RAND_R */ |
| 62 | |
| 63 | #endif /* _COMPAT_RAND_H */ |