8000 unix/modtime: Implement ticks_ms(), ticks_us() and ticks_diff(). · micropython/micropython@8ee153f · GitHub
[go: up one dir, main page]

Skip to content

Commit 8ee153f

Browse files
author
Paul Sokolovsky
committed
unix/modtime: Implement ticks_ms(), ticks_us() and ticks_diff().
All of these functions return positive small int, thus range is 2 bits less than word size (30 bit on 32-bit systems, 62 bit on 64-bit systems).
1 parent fd379db commit 8ee153f

File tree

3 files changed

+34
-0
lines changed

3 files changed

+34
-0
lines changed

py/smallint.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,11 +36,15 @@
3636

3737
#define MP_SMALL_INT_MIN ((mp_int_t)(((mp_int_t)WORD_MSBIT_HIGH) >> 1))
3838
#define MP_SMALL_INT_FITS(n) ((((n) ^ ((n) << 1)) & WORD_MSBIT_HIGH) == 0)
39+
// Mask to truncate mp_int_t to positive value
40+
#define MP_SMALL_INT_POSITIVE_MASK ~(WORD_MSBIT_HIGH | (WORD_MSBIT_HIGH >> 1))
3941

4042
#elif MICROPY_OBJ_REPR == MICROPY_OBJ_REPR_B
4143

4244
#define MP_SMALL_INT_MIN ((mp_int_t)(((mp_int_t)WORD_MSBIT_HIGH) >> 2))
4345
#define MP_SMALL_INT_FITS(n) ((((n) & MP_SMALL_INT_MIN) == 0) || (((n) & MP_SMALL_INT_MIN) == MP_SMALL_INT_MIN))
46+
// Mask to truncate mp_int_t to positive value
47+
#define MP_SMALL_INT_POSITIVE_MASK ~(WORD_MSBIT_HIGH | (WORD_MSBIT_HIGH >> 1) | (WORD_MSBIT_HIGH >> 2))
4448

4549
#endif
4650

unix/modtime.c

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
#include <math.h>
3232

3333
#include "py/runtime.h"
34+
#include "py/smallint.h"
3435

3536
#ifdef _WIN32
3637
void msec_sleep_tv(struct timeval *tv) {
@@ -69,6 +70,29 @@ STATIC mp_obj_t mod_time_time(void) {
6970
}
7071
STATIC MP_DEFINE_CONST_FUN_OBJ_0(mod_time_time_obj, mod_time_time);
7172

73+
STATIC mp_obj_t mod_time_ticks_us(void) {
74+
struct timeval tv;
75+
gettimeofday(&tv, NULL);
76+
mp_uint_t us = tv.tv_sec * 1000000 + tv.tv_usec;
77+
return MP_OBJ_NEW_SMALL_INT(us & MP_SMALL_INT_POSITIVE_MASK);
78+
}
79+
STATIC MP_DEFINE_CONST_FUN_OBJ_0(mod_time_ticks_us_obj, mod_time_ticks_us);
80+
81+
STATIC mp_obj_t mod_time_ticks_ms(void) {
82+
struct timeval tv;
83+
gettimeofday(&tv, NULL);
84+
mp_uint_t ms = tv.tv_sec * 1000 + tv.tv_usec / 1000;
85+
return MP_OBJ_NEW_SMALL_INT(ms & MP_SMALL_INT_POSITIVE_MASK);
86+
}
87+
STATIC MP_DEFINE_CONST_FUN_OBJ_0(mod_time_ticks_ms_obj, mod_time_ticks_ms);
88+
89+
STATIC mp_obj_t mod_time_ticks_diff(mp_obj_t oldval, mp_obj_t newval) {
90+
mp_uint_t old = MP_OBJ_SMALL_INT_VALUE(oldval);
91+
mp_uint_t new = MP_OBJ_SMALL_INT_VALUE(newval);
92+
return MP_OBJ_NEW_SMALL_INT((new - old) & MP_SMALL_INT_POSITIVE_MASK);
93+
}
94+
STATIC MP_DEFINE_CONST_FUN_OBJ_2(mod_time_ticks_diff_obj, mod_time_ticks_diff);
95+
7296
// Note: this is deprecated since CPy3.3, but pystone still uses it.
7397
STATIC mp_obj_t mod_time_clock(void) {
7498
#if MICROPY_PY_BUILTINS_FLOAT
@@ -116,6 +140,9 @@ STATIC const mp_map_elem_t mp_module_time_globals_table[] = {
116140
{ MP_OBJ_NEW_QSTR(MP_QSTR_sleep_ms), (mp_obj_t)&mod_time_sleep_ms_obj },
117141
{ MP_OBJ_NEW_QSTR(MP_QSTR_sleep_us), (mp_obj_t)&mod_time_sleep_us_obj },
118142
{ MP_OBJ_NEW_QSTR(MP_QSTR_time), (mp_obj_t)&mod_time_time_obj },
143+
{ MP_OBJ_NEW_QSTR(MP_QSTR_ticks_ms), (mp_obj_t)&mod_time_ticks_ms_obj },
144+
{ MP_OBJ_NEW_QSTR(MP_QSTR_ticks_us), (mp_obj_t)&mod_time_ticks_us_obj },
145+
{ MP_OBJ_NEW_QSTR(MP_QSTR_ticks_diff), (mp_obj_t)&mod_time_ticks_diff_obj },
119146
};
120147

121148
STATIC MP_DEFINE_CONST_DICT(mp_module_time_globals, mp_module_time_globals_table);

unix/qstrdefsport.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,9 @@ Q(clock)
6262
Q(sleep)
6363
Q(sleep_ms)
6464
Q(sleep_us)
65+
Q(ticks_ms)
66+
Q(ticks_us)
67+
Q(ticks_diff)
6568

6669
Q(socket)
6770
Q(sockaddr_in)

0 commit comments

Comments
 (0)
0