/build/gcc-4.5.3/libgfortran/intrinsics/cpu_time.c

https://github.com/att14/ultraxomb · C · 137 lines · 93 code · 17 blank · 27 comment · 9 complexity · 94ec12fab73b35490d19af97a601f337 MD5 · raw file

  1. /* Implementation of the CPU_TIME intrinsic.
  2. Copyright (C) 2003, 2007, 2009 Free Software Foundation, Inc.
  3. This file is part of the GNU Fortran 95 runtime library (libgfortran).
  4. Libgfortran is free software; you can redistribute it and/or
  5. modify it under the terms of the GNU General Public
  6. License as published by the Free Software Foundation; either
  7. version 3 of the License, or (at your option) any later version.
  8. Libgfortran is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU General Public License for more details.
  12. Under Section 7 of GPL version 3, you are granted additional
  13. permissions described in the GCC Runtime Library Exception, version
  14. 3.1, as published by the Free Software Foundation.
  15. You should have received a copy of the GNU General Public License and
  16. a copy of the GCC Runtime Library Exception along with this program;
  17. see the files COPYING3 and COPYING.RUNTIME respectively. If not, see
  18. <http://www.gnu.org/licenses/>. */
  19. #include "libgfortran.h"
  20. #include "time_1.h"
  21. /* The most accurate way to get the CPU time is getrusage ().
  22. If we have times(), that's good enough, too. */
  23. #if !defined (HAVE_GETRUSAGE) || !defined (HAVE_SYS_RESOURCE_H)
  24. /* For times(), we _must_ know the number of clock ticks per second. */
  25. # if defined (HAVE_TIMES) && (defined (HZ) || defined (_SC_CLK_TCK) || defined (CLK_TCK))
  26. # ifdef HAVE_SYS_PARAM_H
  27. # include <sys/param.h>
  28. # endif
  29. # if defined (HAVE_SYS_TIMES_H)
  30. # include <sys/times.h>
  31. # endif
  32. # ifndef HZ
  33. # if defined _SC_CLK_TCK
  34. # define HZ sysconf(_SC_CLK_TCK)
  35. # else
  36. # define HZ CLK_TCK
  37. # endif
  38. # endif
  39. # endif /* HAVE_TIMES etc. */
  40. #endif /* !HAVE_GETRUSAGE || !HAVE_SYS_RESOURCE_H */
  41. static inline void __cpu_time_1 (long *, long *) ATTRIBUTE_ALWAYS_INLINE;
  42. static inline void
  43. __cpu_time_1 (long *sec, long *usec)
  44. {
  45. #if defined(__MINGW32__) || defined (HAVE_GETRUSAGE) && defined (HAVE_SYS_RESOURCE_H)
  46. long user_sec, user_usec, system_sec, system_usec;
  47. __time_1 (&user_sec, &user_usec, &system_sec, &system_usec);
  48. *sec = user_sec + system_sec;
  49. *usec = user_usec + system_usec;
  50. #else /* ! HAVE_GETRUSAGE || ! HAVE_SYS_RESOURCE_H */
  51. #ifdef HAVE_TIMES
  52. struct tms buf;
  53. times (&buf);
  54. *sec = 0;
  55. *usec = (buf.tms_utime + buf.tms_stime) * (1000000 / HZ);
  56. #else /* ! HAVE_TIMES */
  57. /* We have nothing to go on. Return -1. */
  58. *sec = -1;
  59. *usec = 0;
  60. #endif /* HAVE_TIMES */
  61. #endif /* __MINGW32__ || HAVE_GETRUSAGE */
  62. }
  63. extern void cpu_time_4 (GFC_REAL_4 *);
  64. iexport_proto(cpu_time_4);
  65. void cpu_time_4 (GFC_REAL_4 *time)
  66. {
  67. long sec, usec;
  68. __cpu_time_1 (&sec, &usec);
  69. *time = sec + usec * (GFC_REAL_4)1.e-6;
  70. }
  71. iexport(cpu_time_4);
  72. extern void cpu_time_8 (GFC_REAL_8 *);
  73. export_proto(cpu_time_8);
  74. void cpu_time_8 (GFC_REAL_8 *time)
  75. {
  76. long sec, usec;
  77. __cpu_time_1 (&sec, &usec);
  78. *time = sec + usec * (GFC_REAL_8)1.e-6;
  79. }
  80. #ifdef HAVE_GFC_REAL_10
  81. extern void cpu_time_10 (GFC_REAL_10 *);
  82. export_proto(cpu_time_10);
  83. void cpu_time_10 (GFC_REAL_10 *time)
  84. {
  85. long sec, usec;
  86. __cpu_time_1 (&sec, &usec);
  87. *time = sec + usec * (GFC_REAL_10)1.e-6;
  88. }
  89. #endif
  90. #ifdef HAVE_GFC_REAL_16
  91. extern void cpu_time_16 (GFC_REAL_16 *);
  92. export_proto(cpu_time_16);
  93. void cpu_time_16 (GFC_REAL_16 *time)
  94. {
  95. long sec, usec;
  96. __cpu_time_1 (&sec, &usec);
  97. *time = sec + usec * (GFC_REAL_16)1.e-6;
  98. }
  99. #endif
  100. extern void second_sub (GFC_REAL_4 *);
  101. export_proto(second_sub);
  102. void
  103. second_sub (GFC_REAL_4 *s)
  104. {
  105. cpu_time_4 (s);
  106. }
  107. extern GFC_REAL_4 second (void);
  108. export_proto(second);
  109. GFC_REAL_4
  110. second (void)
  111. {
  112. GFC_REAL_4 s;
  113. cpu_time_4 (&s);
  114. return s;
  115. }