xref: /llvm-project/libcxx/include/__support/ibm/gettod_zos.h (revision 9783f28cbb155e4a8d49c12e1c60ce14dcfaf0c7)
1 // -*- C++ -*-
2 //===----------------------------------------------------------------------===//
3 //
4 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
5 // See https://llvm.org/LICENSE.txt for license information.
6 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
7 //
8 //===----------------------------------------------------------------------===//
9 
10 #ifndef _LIBCPP___SUPPORT_IBM_GETTOD_ZOS_H
11 #define _LIBCPP___SUPPORT_IBM_GETTOD_ZOS_H
12 
13 #include <time.h>
14 
gettimeofdayMonotonic(struct timespec64 * Output)15 inline _LIBCPP_HIDE_FROM_ABI int gettimeofdayMonotonic(struct timespec64* Output) {
16   // The POSIX gettimeofday() function is not available on z/OS. Therefore,
17   // we will call stcke and other hardware instructions in implement equivalent.
18   // Note that nanoseconds alone will overflow when reaching new epoch in 2042.
19 
20   struct _t {
21     uint64_t Hi;
22     uint64_t Lo;
23   };
24   struct _t Value = {0, 0};
25   uint64_t CC     = 0;
26   asm(" stcke %0\n"
27       " ipm %1\n"
28       " srlg %1,%1,28\n"
29       : "=m"(Value), "+r"(CC)::);
30 
31   if (CC != 0) {
32     errno = EMVSTODNOTSET;
33     return CC;
34   }
35   uint64_t us = (Value.Hi >> 4);
36   uint64_t ns = ((Value.Hi & 0x0F) << 8) + (Value.Lo >> 56);
37   ns          = (ns * 1000) >> 12;
38   us          = us - 2208988800000000;
39 
40   register uint64_t DivPair0 asm("r0"); // dividend (upper half), remainder
41   DivPair0 = 0;
42   register uint64_t DivPair1 asm("r1"); // dividend (lower half), quotient
43   DivPair1         = us;
44   uint64_t Divisor = 1000000;
45   asm(" dlgr %0,%2" : "+r"(DivPair0), "+r"(DivPair1) : "r"(Divisor) :);
46 
47   Output->tv_sec  = DivPair1;
48   Output->tv_nsec = DivPair0 * 1000 + ns;
49   return 0;
50 }
51 
52 #endif // _LIBCPP___SUPPORT_IBM_GETTOD_ZOS_H
53