mirror of
https://github.com/jart/cosmopolitan.git
synced 2025-06-28 15:28:30 +00:00
Improve some timespec functions
This commit is contained in:
parent
2395a9eced
commit
6e582d245b
6 changed files with 16 additions and 10 deletions
|
@ -19,13 +19,9 @@
|
||||||
#include "libc/assert.h"
|
#include "libc/assert.h"
|
||||||
#include "libc/calls/struct/timespec.h"
|
#include "libc/calls/struct/timespec.h"
|
||||||
#include "libc/sysv/consts/clock.h"
|
#include "libc/sysv/consts/clock.h"
|
||||||
#include "libc/sysv/consts/nr.h"
|
|
||||||
#include "libc/time/time.h"
|
|
||||||
|
|
||||||
struct timespec _timespec_mono(void) {
|
struct timespec _timespec_mono(void) {
|
||||||
int ax, dx;
|
|
||||||
struct timespec ts;
|
struct timespec ts;
|
||||||
ax = clock_gettime(CLOCK_MONOTONIC_FAST, &ts);
|
_npassert(!clock_gettime(CLOCK_MONOTONIC_FAST, &ts));
|
||||||
assert(!ax);
|
|
||||||
return ts;
|
return ts;
|
||||||
}
|
}
|
||||||
|
|
|
@ -16,15 +16,12 @@
|
||||||
│ TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR │
|
│ TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR │
|
||||||
│ PERFORMANCE OF THIS SOFTWARE. │
|
│ PERFORMANCE OF THIS SOFTWARE. │
|
||||||
╚─────────────────────────────────────────────────────────────────────────────*/
|
╚─────────────────────────────────────────────────────────────────────────────*/
|
||||||
|
#include "libc/assert.h"
|
||||||
#include "libc/calls/struct/timespec.h"
|
#include "libc/calls/struct/timespec.h"
|
||||||
#include "libc/sysv/consts/clock.h"
|
#include "libc/sysv/consts/clock.h"
|
||||||
#include "libc/sysv/consts/nr.h"
|
|
||||||
#include "libc/time/time.h"
|
|
||||||
|
|
||||||
struct timespec _timespec_real(void) {
|
struct timespec _timespec_real(void) {
|
||||||
int ax, dx;
|
|
||||||
struct timespec ts;
|
struct timespec ts;
|
||||||
ax = clock_gettime(CLOCK_REALTIME_FAST, &ts);
|
_npassert(!clock_gettime(CLOCK_REALTIME_FAST, &ts));
|
||||||
if (ax) notpossible;
|
|
||||||
return ts;
|
return ts;
|
||||||
}
|
}
|
||||||
|
|
|
@ -27,6 +27,8 @@ int64_t _timespec_tomicros(struct timespec x) {
|
||||||
if (!__builtin_mul_overflow(x.tv_sec, 1000000ul, &us) &&
|
if (!__builtin_mul_overflow(x.tv_sec, 1000000ul, &us) &&
|
||||||
!__builtin_add_overflow(us, x.tv_nsec / 1000, &us)) {
|
!__builtin_add_overflow(us, x.tv_nsec / 1000, &us)) {
|
||||||
return us;
|
return us;
|
||||||
|
} else if (x.tv_sec < 0) {
|
||||||
|
return INT64_MIN;
|
||||||
} else {
|
} else {
|
||||||
return INT64_MAX;
|
return INT64_MAX;
|
||||||
}
|
}
|
||||||
|
|
|
@ -27,6 +27,8 @@ int64_t _timespec_tomillis(struct timespec x) {
|
||||||
if (!__builtin_mul_overflow(x.tv_sec, 1000ul, &ms) &&
|
if (!__builtin_mul_overflow(x.tv_sec, 1000ul, &ms) &&
|
||||||
!__builtin_add_overflow(ms, x.tv_nsec / 1000000, &ms)) {
|
!__builtin_add_overflow(ms, x.tv_nsec / 1000000, &ms)) {
|
||||||
return ms;
|
return ms;
|
||||||
|
} else if (x.tv_sec < 0) {
|
||||||
|
return INT64_MIN;
|
||||||
} else {
|
} else {
|
||||||
return INT64_MAX;
|
return INT64_MAX;
|
||||||
}
|
}
|
||||||
|
|
|
@ -27,6 +27,8 @@ int64_t _timespec_tonanos(struct timespec x) {
|
||||||
if (!__builtin_mul_overflow(x.tv_sec, 1000000000ul, &ns) &&
|
if (!__builtin_mul_overflow(x.tv_sec, 1000000000ul, &ns) &&
|
||||||
!__builtin_add_overflow(ns, x.tv_nsec, &ns)) {
|
!__builtin_add_overflow(ns, x.tv_nsec, &ns)) {
|
||||||
return ns;
|
return ns;
|
||||||
|
} else if (x.tv_sec < 0) {
|
||||||
|
return INT64_MIN;
|
||||||
} else {
|
} else {
|
||||||
return INT64_MAX;
|
return INT64_MAX;
|
||||||
}
|
}
|
||||||
|
|
|
@ -17,6 +17,7 @@
|
||||||
│ PERFORMANCE OF THIS SOFTWARE. │
|
│ PERFORMANCE OF THIS SOFTWARE. │
|
||||||
╚─────────────────────────────────────────────────────────────────────────────*/
|
╚─────────────────────────────────────────────────────────────────────────────*/
|
||||||
#include "libc/calls/struct/timespec.h"
|
#include "libc/calls/struct/timespec.h"
|
||||||
|
#include "libc/limits.h"
|
||||||
#include "libc/stdio/rand.h"
|
#include "libc/stdio/rand.h"
|
||||||
#include "libc/testlib/testlib.h"
|
#include "libc/testlib/testlib.h"
|
||||||
|
|
||||||
|
@ -64,14 +65,20 @@ TEST(_timespec_frommicros, test) {
|
||||||
|
|
||||||
TEST(_timespec_tomillis, test) {
|
TEST(_timespec_tomillis, test) {
|
||||||
EXPECT_EQ(2123, _timespec_tomillis((struct timespec){2, 123000000}));
|
EXPECT_EQ(2123, _timespec_tomillis((struct timespec){2, 123000000}));
|
||||||
|
EXPECT_EQ(INT64_MAX, _timespec_tomillis((struct timespec){INT64_MAX, 0}));
|
||||||
|
EXPECT_EQ(INT64_MIN, _timespec_tomillis((struct timespec){INT64_MIN, 0}));
|
||||||
}
|
}
|
||||||
|
|
||||||
TEST(_timespec_tomicros, test) {
|
TEST(_timespec_tomicros, test) {
|
||||||
EXPECT_EQ(2000123, _timespec_tomicros((struct timespec){2, 123000}));
|
EXPECT_EQ(2000123, _timespec_tomicros((struct timespec){2, 123000}));
|
||||||
|
EXPECT_EQ(INT64_MAX, _timespec_tomicros((struct timespec){INT64_MAX, 0}));
|
||||||
|
EXPECT_EQ(INT64_MIN, _timespec_tomicros((struct timespec){INT64_MIN, 0}));
|
||||||
}
|
}
|
||||||
|
|
||||||
TEST(_timespec_tonanos, test) {
|
TEST(_timespec_tonanos, test) {
|
||||||
EXPECT_EQ(2000123000, _timespec_tonanos((struct timespec){2, 123000}));
|
EXPECT_EQ(2000123000, _timespec_tonanos((struct timespec){2, 123000}));
|
||||||
|
EXPECT_EQ(INT64_MAX, _timespec_tonanos((struct timespec){INT64_MAX, 0}));
|
||||||
|
EXPECT_EQ(INT64_MIN, _timespec_tonanos((struct timespec){INT64_MIN, 0}));
|
||||||
}
|
}
|
||||||
|
|
||||||
static long mod(long x, long y) {
|
static long mod(long x, long y) {
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue