diff options
| author | dam <dam@gudinoff> | 2022-11-15 01:52:02 +0000 |
|---|---|---|
| committer | dam <dam@gudinoff> | 2022-11-15 01:52:02 +0000 |
| commit | 6475865459debdd07df27d6a9a3b6679a6fad69d (patch) | |
| tree | cf6ad2186f862f1e4d1fe0fe13856dd885f4b2cf | |
| parent | 01ea9b45bcbb950a51fcb2eedba1b563d8044805 (diff) | |
| download | task-time-tracker-6475865459debdd07df27d6a9a3b6679a6fad69d.tar.zst task-time-tracker-6475865459debdd07df27d6a9a3b6679a6fad69d.zip | |
Optimized add_int64 and sub_int64. Previous commit was done by mistake during WIP.
| -rw-r--r-- | main.c | 131 |
1 files changed, 12 insertions, 119 deletions
@@ -220,23 +220,21 @@ char *sprint_time5_utf8(char *string, intmax_t time, int space) { } int64_t add_int64(int64_t x, int64_t y) { - if (y > 0 && x > INT64_MAX - y) - return INT64_MAX; - - if (y < 0 && x < INT64_MIN - y) - return INT64_MIN; - - return x + y; + int64_t result; + bool overflow = __builtin_add_overflow(x, y, &result); + if (overflow) { + result = ((uint64_t)x >> 63) + INT64_MAX; // Equivalent to (x > 0 ? INT64_MAX : INT64_MIN) + } + return result; } int64_t sub_int64(int64_t x, int64_t y) { - if (y < 0 && x > INT64_MAX + y) - return INT64_MAX; - - if (y > 0 && x < INT64_MIN + y) - return INT64_MIN; - - return x - y; + int64_t result; + bool overflow = __builtin_sub_overflow(x, y, &result); + if (overflow) { + result = ((uint64_t)x >> 63) + INT64_MAX; // Equivalent to (x > 0 ? INT64_MAX : INT64_MIN) + } + return result; } // Returns active task or NULL if none applies. @@ -460,57 +458,10 @@ void update_times(database_st *db) { } } -int64_t super_add(int64_t x, int64_t y) { - int64_t result; - if (__builtin_add_overflow(x, y, &result)) { - result = (x > 0 ? INT64_MAX : INT64_MIN); -// result = (x >> 63) ^ INT64_MAX; -// result = ((uint64_t)x >> 63) + INT64_MAX; - } - return result; -} - // Recalculates database totals. void update_total_times(database_st *db) { assert(db != NULL); - /* - // 100 MOPS - int64_t *total_times = db->total_times; - memset(db->total_times, 0, NUM_WEEK_DAYS * SIZEOF_INT64); - for (size_t idx = 0; idx < db->count; idx++) { - int64_t *times = db->tasks[idx].times; - for (int it = 0; it < NUM_WEEK_DAYS; it++) { - total_times[it] = add_int64(total_times[it], times[it]); - } - } - */ - - /* - // 120 MOPS - int64_t *d0 = &db->total_times[0]; - int64_t *d1 = &db->total_times[1]; - int64_t *d2 = &db->total_times[2]; - int64_t *d3 = &db->total_times[3]; - int64_t *d4 = &db->total_times[4]; - int64_t *d5 = &db->total_times[5]; - int64_t *d6 = &db->total_times[6]; - memset(db->total_times, 0, NUM_WEEK_DAYS * SIZEOF_INT64); - - for (size_t idx = 0; idx < db->count; idx++) { - int64_t *times = db->tasks[idx].times; - *d0 = add_int64(*d0, times[0]); - *d1 = add_int64(*d1, times[1]); - *d2 = add_int64(*d2, times[2]); - *d3 = add_int64(*d3, times[3]); - *d4 = add_int64(*d4, times[4]); - *d5 = add_int64(*d5, times[5]); - *d6 = add_int64(*d6, times[6]); - } - */ - - /* - // 122 MOPS memset(db->total_times, 0, NUM_WEEK_DAYS * SIZEOF_INT64); for (size_t idx = 0; idx < db->count; idx++) { int64_t *times = db->tasks[idx].times; @@ -522,51 +473,6 @@ void update_total_times(database_st *db) { db->total_times[5] = add_int64(db->total_times[5], times[5]); db->total_times[6] = add_int64(db->total_times[6], times[6]); } - */ - - /* - // 140 MOPS - memset(db->total_times, 0, NUM_WEEK_DAYS * SIZEOF_INT64); - for (size_t idx = 0; idx < db->count; idx++) { - int64_t *times = db->tasks[idx].times; - __builtin_add_overflow(db->total_times[0], times[0], &db->total_times[0]); - __builtin_add_overflow(db->total_times[1], times[1], &db->total_times[1]); - __builtin_add_overflow(db->total_times[2], times[2], &db->total_times[2]); - __builtin_add_overflow(db->total_times[3], times[3], &db->total_times[3]); - __builtin_add_overflow(db->total_times[4], times[4], &db->total_times[4]); - __builtin_add_overflow(db->total_times[5], times[5], &db->total_times[5]); - __builtin_add_overflow(db->total_times[6], times[6], &db->total_times[6]); - } - */ - - // 140 MOPS - memset(db->total_times, 0, NUM_WEEK_DAYS * SIZEOF_INT64); - for (size_t idx = 0; idx < db->count; idx++) { - int64_t *times = db->tasks[idx].times; - db->total_times[0] = super_add(db->total_times[0], times[0]); - db->total_times[1] = super_add(db->total_times[1], times[1]); - db->total_times[2] = super_add(db->total_times[2], times[2]); - db->total_times[3] = super_add(db->total_times[3], times[3]); - db->total_times[4] = super_add(db->total_times[4], times[4]); - db->total_times[5] = super_add(db->total_times[5], times[5]); - db->total_times[6] = super_add(db->total_times[6], times[6]); - } - - /* - // 110 MOPS - memset(db->total_times, 0, NUM_WEEK_DAYS * SIZEOF_INT64); - int64_t *totals = db->total_times; - for (size_t idx = 0; idx < db->count; idx++) { - const int64_t *restrict times = db->tasks[idx].times; - totals[0] = add_int64(totals[0], times[0]); - totals[1] = add_int64(totals[1], times[1]); - totals[2] = add_int64(totals[2], times[2]); - totals[3] = add_int64(totals[3], times[3]); - totals[4] = add_int64(totals[4], times[4]); - totals[5] = add_int64(totals[5], times[5]); - totals[6] = add_int64(totals[6], times[6]); - } - */ } // Resets the times of the provided task (and adjusts database totals). @@ -1340,19 +1246,6 @@ int main(int argc, char *argv[]) { load_database(&database, db_file_path); - // TODO WIP WIP - clock_t start = clock() ; - update_total_times(&database); - clock_t end = clock() ; - double time_sec = (end-start) / (double)CLOCKS_PER_SEC; - double speed = (double)database.count / (1000000.0 * time_sec); - double usPM = 1000000.0 * 1000.0 * time_sec / (double)database.count; - endwin(); - fprintf(stderr, "update of %zd took: %9.3fms\n", database.count, time_sec); - fprintf(stderr, "speed: %9.3f MOPS\n", speed); - fprintf(stderr, "tpm : %9.3f usPM\n", usPM); - return EXIT_FAILURE; - flushinp(); ungetch(KEY_RESIZE); for (int key; ((key = getch()) != 'q') && (key != 'Q'); ) { |
