difftime.c 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. /* Copyright (C) 1991, 1994, 1996 Free Software Foundation, Inc.
  2. This file is part of the GNU C Library.
  3. The GNU C Library is free software; you can redistribute it and/or
  4. modify it under the terms of the GNU Library General Public License as
  5. published by the Free Software Foundation; either version 2 of the
  6. License, or (at your option) any later version.
  7. The GNU C Library is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  10. Library General Public License for more details.
  11. You should have received a copy of the GNU Library General Public
  12. License along with the GNU C Library; see the file COPYING.LIB. If not,
  13. write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
  14. Boston, MA 02111-1307, USA. */
  15. #include <time.h>
  16. #include <limits.h>
  17. #include <sys/types.h>
  18. /* Return the difference between TIME1 and TIME0. */
  19. double
  20. difftime (time1, time0)
  21. time_t time1;
  22. time_t time0;
  23. {
  24. /* Algorithm courtesy Paul Eggert (eggert@twinsun.com). */
  25. time_t delta, hibit;
  26. if (sizeof (time_t) < sizeof (double))
  27. return (double) time1 - (double) time0;
  28. if (sizeof (time_t) < sizeof (long double))
  29. return (long double) time1 - (long double) time0;
  30. if (time1 < time0)
  31. return - difftime (time0, time1);
  32. /* As much as possible, avoid loss of precision by computing the
  33. difference before converting to double. */
  34. delta = time1 - time0;
  35. if (delta >= 0)
  36. return delta;
  37. /* Repair delta overflow. */
  38. hibit = (~ (time_t) 0) << ((sizeof (time_t) * CHAR_BIT) - 1);
  39. /* The following expression rounds twice, which means the result may not
  40. be the closest to the true answer. For example, suppose time_t is
  41. 64-bit signed int, long_double is IEEE 754 double with default
  42. rounding, time1 = 9223372036854775807 and time0 = -1536. Then the
  43. true difference is 9223372036854777343, which rounds to
  44. 9223372036854777856 with a total error of 513. But delta overflows to
  45. -9223372036854774273, which rounds to -9223372036854774784, and
  46. correcting this by subtracting 2 * (long_double) hibit (i.e. by adding
  47. 2**64 = 18446744073709551616) yields 9223372036854776832, which rounds
  48. to 9223372036854775808 with a total error of 1535 instead. This
  49. problem occurs only with very large differences. It's too painful to
  50. fix this portably. We are not alone in this problem; many C compilers
  51. round twice when converting large unsigned types to small floating
  52. types, so if time_t is unsigned the "return delta" above has the same
  53. double-rounding problem. */
  54. return delta - 2 * (long double) hibit;
  55. }