randdp.c 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. //
  2. // Copyright 2010 Intel Corporation
  3. //
  4. // Licensed under the Apache License, Version 2.0 (the "License");
  5. // you may not use this file except in compliance with the License.
  6. // You may obtain a copy of the License at
  7. //
  8. // http://www.apache.org/licenses/LICENSE-2.0
  9. //
  10. // Unless required by applicable law or agreed to in writing, software
  11. // distributed under the License is distributed on an "AS IS" BASIS,
  12. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. // See the License for the specific language governing permissions and
  14. // limitations under the License.
  15. //
  16. //---------------------------------------------------------------------
  17. // This function is C verson of random number generator randdp.f
  18. //---------------------------------------------------------------------
  19. double randlc(X, A)
  20. double *X;
  21. double *A;
  22. {
  23. static int KS=0;
  24. static double R23, R46, T23, T46;
  25. double T1, T2, T3, T4;
  26. double A1;
  27. double A2;
  28. double X1;
  29. double X2;
  30. double Z;
  31. int i, j;
  32. if (KS == 0)
  33. {
  34. R23 = 1.0;
  35. R46 = 1.0;
  36. T23 = 1.0;
  37. T46 = 1.0;
  38. for (i=1; i<=23; i++)
  39. {
  40. R23 = 0.50 * R23;
  41. T23 = 2.0 * T23;
  42. }
  43. for (i=1; i<=46; i++)
  44. {
  45. R46 = 0.50 * R46;
  46. T46 = 2.0 * T46;
  47. }
  48. KS = 1;
  49. }
  50. /* Break A into two parts such that A = 2^23 * A1 + A2 and set X = N. */
  51. T1 = R23 * *A;
  52. j = T1;
  53. A1 = j;
  54. A2 = *A - T23 * A1;
  55. /* Break X into two parts such that X = 2^23 * X1 + X2, compute
  56. Z = A1 * X2 + A2 * X1 (mod 2^23), and then
  57. X = 2^23 * Z + A2 * X2 (mod 2^46). */
  58. T1 = R23 * *X;
  59. j = T1;
  60. X1 = j;
  61. X2 = *X - T23 * X1;
  62. T1 = A1 * X2 + A2 * X1;
  63. j = R23 * T1;
  64. T2 = j;
  65. Z = T1 - T23 * T2;
  66. T3 = T23 * Z + A2 * X2;
  67. j = R46 * T3;
  68. T4 = j;
  69. *X = T3 - T46 * T4;
  70. return(R46 * *X);
  71. }