drand48-iter.c 2.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. /* Copyright (C) 1995, 1996, 2001 Free Software Foundation, Inc.
  2. This file is part of the GNU C Library.
  3. Contributed by Ulrich Drepper <drepper@gnu.ai.mit.edu <mailto:drepper@gnu.ai.mit.edu>>, August 1995.
  4. The GNU C Library is free software; you can redistribute it and/or
  5. modify it under the terms of the GNU Lesser General Public
  6. License as published by the Free Software Foundation; either
  7. version 2.1 of the License, or (at your option) any later version.
  8. The GNU C Library is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  11. Lesser General Public License for more details.
  12. You should have received a copy of the GNU Lesser General Public
  13. License along with the GNU C Library; if not, write to the Free
  14. Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
  15. 02111-1307 USA. */
  16. #include <errno.h>
  17. #include <stdlib.h>
  18. #include <limits.h>
  19. #include <stdint.h>
  20. #include <sys/types.h>
  21. /* Global state for non-reentrant functions. */
  22. struct drand48_data __libc_drand48_data;
  23. #ifdef __UCLIBC_HAS_LONG_LONG__
  24. int
  25. __drand48_iterate (unsigned short int xsubi[3], struct drand48_data *buffer)
  26. {
  27. uint64_t X;
  28. uint64_t result;
  29. /* Initialize buffer, if not yet done. */
  30. if (unlikely(!buffer->__init))
  31. {
  32. buffer->__a = 0x5deece66dull;
  33. buffer->__c = 0xb;
  34. buffer->__init = 1;
  35. }
  36. /* Do the real work. We choose a data type which contains at least
  37. 48 bits. Because we compute the modulus it does not care how
  38. many bits really are computed. */
  39. X = (uint64_t) xsubi[2] << 32 | (uint32_t) xsubi[1] << 16 | xsubi[0];
  40. result = X * buffer->__a + buffer->__c;
  41. xsubi[0] = result & 0xffff;
  42. xsubi[1] = (result >> 16) & 0xffff;
  43. xsubi[2] = (result >> 32) & 0xffff;
  44. return 0;
  45. }
  46. #else
  47. int
  48. __drand48_iterate (unsigned short int xsubi[3], struct drand48_data *buffer)
  49. {
  50. uint32_t X0, X1;
  51. uint32_t result0, result1;
  52. /* Initialize buffer, if not yet done. */
  53. if (unlikely(!buffer->__init))
  54. {
  55. buffer->__a1 = 0x5;
  56. buffer->__a0 = 0xdeece66d;
  57. buffer->__c = 0xb;
  58. buffer->__init = 1;
  59. }
  60. /* Do the real work. We choose a data type which contains at least
  61. 48 bits. Because we compute the modulus it does not care how
  62. many bits really are computed. */
  63. /* X = X1*base32 + X0 */
  64. X1 = xsubi[2];
  65. X0 = xsubi[0] | (uint32_t) xsubi[1] << 16;
  66. result0 = buffer->__a0 * X0;
  67. result1 = (result0 > -buffer->__c ); /* Carry */
  68. /* If this overflows, the carry is already in result1 */
  69. result0 += buffer->__c;
  70. result1 += buffer->__a1*X0 + buffer->__a0*X1;
  71. xsubi[0] = result0 & 0xffff;
  72. xsubi[1] = result0 >> 16;
  73. xsubi[2] = result1 & 0xffff;
  74. return 0;
  75. }
  76. #endif