cpu_time.c 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. /* Implementation of the CPU_TIME intrinsic.
  2. Copyright (C) 2003-2015 Free Software Foundation, Inc.
  3. This file is part of the GNU Fortran 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. static void
  22. __cpu_time_1 (long *sec, long *usec)
  23. {
  24. long user_sec, user_usec, system_sec, system_usec;
  25. if (gf_cputime (&user_sec, &user_usec, &system_sec, &system_usec) == 0)
  26. {
  27. *sec = user_sec + system_sec;
  28. *usec = user_usec + system_usec;
  29. }
  30. else
  31. {
  32. *sec = -1;
  33. *usec = 0;
  34. }
  35. }
  36. extern void cpu_time_4 (GFC_REAL_4 *);
  37. iexport_proto(cpu_time_4);
  38. void cpu_time_4 (GFC_REAL_4 *time)
  39. {
  40. long sec, usec;
  41. __cpu_time_1 (&sec, &usec);
  42. *time = sec + usec * GFC_REAL_4_LITERAL(1.e-6);
  43. }
  44. iexport(cpu_time_4);
  45. extern void cpu_time_8 (GFC_REAL_8 *);
  46. export_proto(cpu_time_8);
  47. void cpu_time_8 (GFC_REAL_8 *time)
  48. {
  49. long sec, usec;
  50. __cpu_time_1 (&sec, &usec);
  51. *time = sec + usec * GFC_REAL_8_LITERAL(1.e-6);
  52. }
  53. #ifdef HAVE_GFC_REAL_10
  54. extern void cpu_time_10 (GFC_REAL_10 *);
  55. export_proto(cpu_time_10);
  56. void cpu_time_10 (GFC_REAL_10 *time)
  57. {
  58. long sec, usec;
  59. __cpu_time_1 (&sec, &usec);
  60. *time = sec + usec * GFC_REAL_10_LITERAL(1.e-6);
  61. }
  62. #endif
  63. #ifdef HAVE_GFC_REAL_16
  64. extern void cpu_time_16 (GFC_REAL_16 *);
  65. export_proto(cpu_time_16);
  66. void cpu_time_16 (GFC_REAL_16 *time)
  67. {
  68. long sec, usec;
  69. __cpu_time_1 (&sec, &usec);
  70. *time = sec + usec * GFC_REAL_16_LITERAL(1.e-6);
  71. }
  72. #endif
  73. extern void second_sub (GFC_REAL_4 *);
  74. export_proto(second_sub);
  75. void
  76. second_sub (GFC_REAL_4 *s)
  77. {
  78. cpu_time_4 (s);
  79. }
  80. extern GFC_REAL_4 second (void);
  81. export_proto(second);
  82. GFC_REAL_4
  83. second (void)
  84. {
  85. GFC_REAL_4 s;
  86. cpu_time_4 (&s);
  87. return s;
  88. }