difftime.c 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  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 difftime (time_t time1, time_t time0)
  20. {
  21. /* Algorithm courtesy Paul Eggert (eggert@twinsun.com). */
  22. time_t delta, hibit;
  23. if (sizeof (time_t) < sizeof (double))
  24. return (double) time1 - (double) time0;
  25. if (sizeof (time_t) < sizeof (long double))
  26. return (long double) time1 - (long double) time0;
  27. if (time1 < time0)
  28. return - difftime (time0, time1);
  29. /* As much as possible, avoid loss of precision by computing the
  30. difference before converting to double. */
  31. delta = time1 - time0;
  32. if (delta >= 0)
  33. return delta;
  34. /* Repair delta overflow. */
  35. hibit = (~ (time_t) 0) << ((sizeof (time_t) * CHAR_BIT) - 1);
  36. /* The following expression rounds twice, which means the result may not
  37. be the closest to the true answer. For example, suppose time_t is
  38. 64-bit signed int, long_double is IEEE 754 double with default
  39. rounding, time1 = 9223372036854775807 and time0 = -1536. Then the
  40. true difference is 9223372036854777343, which rounds to
  41. 9223372036854777856 with a total error of 513. But delta overflows to
  42. -9223372036854774273, which rounds to -9223372036854774784, and
  43. correcting this by subtracting 2 * (long_double) hibit (i.e. by adding
  44. 2**64 = 18446744073709551616) yields 9223372036854776832, which rounds
  45. to 9223372036854775808 with a total error of 1535 instead. This
  46. problem occurs only with very large differences. It's too painful to
  47. fix this portably. We are not alone in this problem; many C compilers
  48. round twice when converting large unsigned types to small floating
  49. types, so if time_t is unsigned the "return delta" above has the same
  50. double-rounding problem. */
  51. return delta - 2 * (long double) hibit;
  52. }