Source code of Windows XP (NT5)
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.

79 lines
1.9 KiB

  1. /***
  2. *ceil.c - ceiling
  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 ceil(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 ceil(double x) - ceiling
  21. *
  22. *Purpose:
  23. * Return a double representing the smallest integer that is
  24. * greater than or equal to x
  25. *
  26. *Entry:
  27. *
  28. *Exit:
  29. *
  30. *Exceptions:
  31. * P, I
  32. *******************************************************************************/
  33. static uintptr_t newcw = (ICW & ~IMCW_RC) | (IRC_UP & IMCW_RC);
  34. #if !defined(_M_IX86)
  35. double ceil(double x)
  36. #else
  37. double _ceil_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 up */
  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_CEIL, x, savedcw);
  52. default: //T_SNAN
  53. return _except1(FP_I, OP_CEIL, 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_CEIL, x, result, savedcw);
  67. }
  68. }