Leaked source code of windows server 2003
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

119 lines
2.2 KiB

  1. /***
  2. *powhlp.c - pow() helper routines for handling special cases
  3. *
  4. * Copyright (c) 1991-2001, Microsoft Corporation. All rights reserved.
  5. *
  6. *Purpose:
  7. * pow(x,y) helper routine. Handles +inf, -inf
  8. *
  9. *Revision History:
  10. * 11-09-91 GDP
  11. * 06-23-92 GDP adjusted return values according to the NCEG spec
  12. * 02-06-95 JWM Mac merge
  13. * 02-07-95 JWM powhlp() restored to Intel version.
  14. *
  15. *******************************************************************************/
  16. #include <trans.h>
  17. #include <float.h>
  18. /***
  19. *int _powhlp(double x, double y, double * result) - pow() helper
  20. *
  21. *Purpose:
  22. * Calculate x^(sign)inf
  23. *
  24. *Entry:
  25. * double x: the base
  26. * int sign: the sign of the infinite exponent (0: pos, non-0: neg)
  27. * double *result: pointer to the result
  28. *
  29. *Exit:
  30. * 0: normal exit
  31. * -1: indicates domain error for pow(x,inf)
  32. *
  33. *Exceptions:
  34. *
  35. ***************************************************************************/
  36. int _powhlp(double x, double y, double * result)
  37. {
  38. double absx;
  39. int err = 0;
  40. absx = ABS(x);
  41. if (IS_D_INF(y)) {
  42. if (absx > 1.0) {
  43. *result = D_INF;
  44. }
  45. else if (absx < 1.0) {
  46. *result = 0.0;
  47. }
  48. else {
  49. *result = D_IND;
  50. err = 1;
  51. }
  52. }
  53. else if (IS_D_MINF(y)) {
  54. if (absx > 1.0) {
  55. *result = 0.0;
  56. }
  57. else if (absx < 1.0) {
  58. *result = D_INF;
  59. }
  60. else {
  61. *result = D_IND;
  62. err = 1;
  63. }
  64. }
  65. else if (IS_D_INF(x)) {
  66. if (y > 0)
  67. *result = D_INF;
  68. else if (y < 0.0)
  69. *result = 0.0;
  70. else {
  71. *result = 1.0;
  72. }
  73. }
  74. else if (IS_D_MINF(x)) {
  75. int type;
  76. type = _d_inttype(y);
  77. if (y > 0.0) {
  78. *result = (type == _D_ODD ? -D_INF : D_INF);
  79. }
  80. else if (y < 0.0) {
  81. *result = (type == _D_ODD ? D_MZERO : 0.0);
  82. }
  83. else {
  84. *result = 1;
  85. }
  86. }
  87. return err;
  88. }
  89. int _d_inttype(double y)
  90. {
  91. double rounded;
  92. /* check if y is an integral value */
  93. if (_fpclass(y) & (_FPCLASS_PD | _FPCLASS_ND))
  94. return _D_NOINT;
  95. rounded = _frnd(y);
  96. if (rounded == y) {
  97. if (_frnd(y/2.0) == y/2.0)
  98. return _D_EVEN;
  99. else
  100. return _D_ODD;
  101. }
  102. return _D_NOINT;
  103. }