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.

78 lines
2.0 KiB

  1. /***
  2. *floor.c - floor
  3. *
  4. * Copyright (c) 1991-2001, Microsoft Corporation. All rights reserved.
  5. *
  6. *Purpose:
  7. *
  8. *Revision History:
  9. * 8-24-91 GDP written
  10. * 1-09-92 GDP support IEEE exceptions
  11. * 6-23-92 GDP floor(INF) now returns INF (NCEG spec)
  12. * 02-06-95 JWM Mac merge
  13. * 01-26-01 PML Pentium4 merge
  14. *
  15. *******************************************************************************/
  16. #include <math.h>
  17. #include <trans.h>
  18. extern double _frnd(double);
  19. /***
  20. *double floor(double x) - floor
  21. *
  22. *Purpose:
  23. * Return a double representing the largest integer that is
  24. * less than or equal to x
  25. *
  26. *Entry:
  27. *
  28. *Exit:
  29. *
  30. *Exceptions:
  31. * I, P
  32. *******************************************************************************/
  33. static unsigned int newcw = (ICW & ~IMCW_RC) | (IRC_DOWN & IMCW_RC);
  34. #if !defined(_M_IX86)
  35. double floor(double x)
  36. #else
  37. double _floor_default(double x)
  38. #endif
  39. {
  40. uintptr_t savedcw;
  41. double result;
  42. /* save user fp control word */
  43. savedcw = _ctrlfp(newcw,IMCW); /* round down */
  44. /* check for infinity or NAN */
  45. if (IS_D_SPECIAL(x)){
  46. switch (_sptype(x)) {
  47. case T_PINF:
  48. case T_NINF:
  49. RETURN(savedcw,x);
  50. case T_QNAN:
  51. return _handle_qnan1(OP_FLOOR, x, savedcw);
  52. default: //T_SNAN
  53. return _except1(FP_I, OP_FLOOR, x, _s2qnan(x), savedcw);
  54. }
  55. }
  56. result = _frnd(x); /* round according to the current rounding mode */
  57. // In general, the Precision Exception should be raised if
  58. // _frnd reports a precision loss. In order to detect this with
  59. // masked exceptions, the status word needs to be cleared.
  60. // However, we want to avoid this, since the 387 instruction
  61. // set does not provide an fast way to restore the status word
  62. if (result == x) {
  63. RETURN(savedcw,result);
  64. }
  65. else {
  66. RETURN_INEXACT1(OP_FLOOR, x, result, savedcw);
  67. }
  68. }