File tree 4 files changed +69
-0
lines changed
4 files changed +69
-0
lines changed Original file line number Diff line number Diff line change @@ -66,6 +66,7 @@ add_flang_library(FortranRuntime
66
66
stop.cpp
67
67
sum.cpp
68
68
terminator.cpp
69
+ time-intrinsic.cpp
69
70
tools.cpp
70
71
transformational.cpp
71
72
type -code.cpp
Original file line number Diff line number Diff line change
1
+ // ===-- runtime/time-intrinsic.cpp ----------------------------------------===//
2
+ //
3
+ // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4
+ // See https://llvm.org/LICENSE.txt for license information.
5
+ // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6
+ //
7
+ // ===----------------------------------------------------------------------===//
8
+
9
+ // Implements time-related intrinsic subroutines.
10
+
11
+ #include " time-intrinsic.h"
12
+
13
+ #include < ctime>
14
+
15
+ namespace Fortran ::runtime {
16
+ extern " C" {
17
+
18
+ // CPU_TIME (Fortran 2018 16.9.57)
19
+ double RTNAME (CpuTime)() {
20
+ // This is part of the c++ standard, so it should at least exist everywhere.
21
+ // It probably does not have the best resolution, so we prefer other
22
+ // platform-specific alternatives if they exist.
23
+ std::clock_t timestamp{std::clock ()};
24
+ if (timestamp != std::clock_t {-1 }) {
25
+ return static_cast <double >(timestamp) / CLOCKS_PER_SEC;
26
+ }
27
+
28
+ // Return some negative value to represent failure.
29
+ return -1.0 ;
30
+ }
31
+ } // extern "C"
32
+ } // namespace Fortran::runtime
Original file line number Diff line number Diff line change @@ -11,6 +11,7 @@ add_flang_unittest(FlangRuntimeTests
11
11
Random.cpp
12
12
Reduction.cpp
13
13
RuntimeCrashTest.cpp
14
+ Time.cpp
14
15
Transformational.cpp
15
16
)
16
17
Original file line number Diff line number Diff line change
1
+ // ===-- flang/unittests/RuntimeGTest/Time.cpp -----------------------===//
2
+ //
3
+ // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4
+ // See https://llvm.org/LICENSE.txt for license information.
5
+ // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6
+ //
7
+ // ===----------------------------------------------------------------------===//
8
+
9
+ #include " gtest/gtest.h"
10
+ #include " ../../runtime/time-intrinsic.h"
11
+
12
+ using namespace Fortran ::runtime;
13
+
14
+ volatile int x = 0 ;
15
+
16
+ void LookBusy () {
17
+ // We're trying to track actual processor time, so sleeping is not an option.
18
+ // Doing some writes to a volatile variable should do the trick.
19
+ for (int i = 0 ; i < (1 << 8 ); ++i) {
20
+ x = i;
21
+ }
22
+ }
23
+
24
+ TEST (TimeIntrinsics, CpuTime) {
25
+ // We can't really test that we get the "right" result for CPU_TIME, but we
26
+ // can have a smoke test to see that we get something reasonable on the
27
+ // platforms where we expect to support it.
28
+ double start = RTNAME (CpuTime)();
29
+ LookBusy ();
30
+ double end = RTNAME (CpuTime)();
31
+
32
+ ASSERT_GE (start, 0.0 );
33
+ ASSERT_GT (end, 0.0 );
34
+ ASSERT_GT (end, start);
35
+ }
You can’t perform that action at this time.
0 commit comments