1
0
Fork 0
forked from forks/qmk_firmware
qmk_firmware/tmk_core/common
Purdea Andrei 6c2c3c13e9
Various fixes to how timer differences are calculated (#8585)
* tmk_core/common: Fixing TIMER_DIFF macro to calculate difference correctly after the timer wraps.

Let's go through an example, using the following macro:

If the first timer read is 0xe4 and the second one is 0x32, the timer wrapped.
If the timer would have had more bits, it's new value would have been 0x132,
and the correct difference in time is 0x132 - 0xe4 = 0x4e

old code TIMER_DIFF_8(0x32, 0xe4) = 0xff - 0xe4 + 0x32 = 0x4d, which is wrong.
new code TIMER_DIFF_8(0x32, 0xe4) = 0xff + 1 - 0xe4 + 0x32 = 0x4e, which is correct.

This also gives a chance for a smart compiler to optimize the code using normal
integer overflow.

For example on AVR, the following C code:
uint8_t __attribute__ ((noinline)) test(uint8_t current_timer, uint8_t start_timer)
{
    return TIMER_DIFF_8(current_timer, start_timer);
}
With the original code, it gets translated to the following list of instructions:
00004c6e <test>:
    4c6e:       98 2f           mov     r25, r24
    4c70:       86 1b           sub     r24, r22
    4c72:       96 17           cp      r25, r22
    4c74:       08 f4           brcc    .+2             ; 0x4c78 <test+0xa>
    4c76:       81 50           subi    r24, 0x01       ; 1
    4c78:       08 95           ret
But with this commit, it gets translated to a single instruction:
00004c40 <test>:
    4c40:       86 1b           sub     r24, r22
    4c42:       08 95           ret

This unfortunately doesn't always work so nicely, for example the following C code:
int __attribute__ ((noinline)) test(uint8_t current_timer, uint8_t start_timer)
{
    return TIMER_DIFF_8(current_timer, start_timer);
}
(Note: return type changed to int)
With the original code it gets translated to:
00004c6e <test>:
    4c6e:       28 2f           mov     r18, r24
    4c70:       30 e0           ldi     r19, 0x00       ; 0
    4c72:       46 2f           mov     r20, r22
    4c74:       50 e0           ldi     r21, 0x00       ; 0
    4c76:       86 17           cp      r24, r22
    4c78:       20 f0           brcs    .+8             ; 0x4c82 <test+0x14>
    4c7a:       c9 01           movw    r24, r18
    4c7c:       84 1b           sub     r24, r20
    4c7e:       95 0b           sbc     r25, r21
    4c80:       08 95           ret
    4c82:       c9 01           movw    r24, r18
    4c84:       84 1b           sub     r24, r20
    4c86:       95 0b           sbc     r25, r21
    4c88:       81 50           subi    r24, 0x01       ; 1
    4c8a:       9f 4f           sbci    r25, 0xFF       ; 255
    4c8c:       08 95           ret
Wth this commit it gets translated to:
00004c40 <test>:
    4c40:       28 2f           mov     r18, r24
    4c42:       30 e0           ldi     r19, 0x00       ; 0
    4c44:       46 2f           mov     r20, r22
    4c46:       50 e0           ldi     r21, 0x00       ; 0
    4c48:       86 17           cp      r24, r22
    4c4a:       20 f0           brcs    .+8             ; 0x4c54 <test+0x14>
    4c4c:       c9 01           movw    r24, r18
    4c4e:       84 1b           sub     r24, r20
    4c50:       95 0b           sbc     r25, r21
    4c52:       08 95           ret
    4c54:       c9 01           movw    r24, r18
    4c56:       84 1b           sub     r24, r20
    4c58:       95 0b           sbc     r25, r21
    4c5a:       93 95           inc     r25
    4c5c:       08 95           ret
There is not much performance improvement in this case, however at least with this
commit it functions correctly.

Note: The following commit will improve compiler output for the latter example.

* tmk_core/common: Improve code generation for TIMER_DIFF* macros

Because of integer promotion the compiler is having a hard time generating
efficient code to calculate TIMER_DIFF* macros in some situations.
In the below example, the return value is "int", and this is causing the
trouble.

Example C code:

int __attribute__ ((noinline)) test(uint8_t current_timer, uint8_t start_timer)
{
    return TIMER_DIFF_8(current_timer, start_timer);
}

BEFORE: (with -Os)

00004c40 <test>:
    4c40:       28 2f           mov     r18, r24
    4c42:       30 e0           ldi     r19, 0x00       ; 0
    4c44:       46 2f           mov     r20, r22
    4c46:       50 e0           ldi     r21, 0x00       ; 0
    4c48:       86 17           cp      r24, r22
    4c4a:       20 f0           brcs    .+8             ; 0x4c54 <test+0x14>
    4c4c:       c9 01           movw    r24, r18
    4c4e:       84 1b           sub     r24, r20
    4c50:       95 0b           sbc     r25, r21
    4c52:       08 95           ret
    4c54:       c9 01           movw    r24, r18
    4c56:       84 1b           sub     r24, r20
    4c58:       95 0b           sbc     r25, r21
    4c5a:       93 95           inc     r25
    4c5c:       08 95           ret

AFTER: (with -Os)

00004c40 <test>:
    4c40:       86 1b           sub     r24, r22
    4c42:       90 e0           ldi     r25, 0x00       ; 0
    4c44:       08 95           ret

Note: the example is showing -Os but improvements can be seen at all optimization levels,
including -O0. We never use -O0, but I tested it to make sure that no extra code is
generated in that case.OA

* quantum/debounce: Fix custom wrapping timers in eager_pr and eager_pk debounce algorithms

Please see the below simulated sequence of events:
Column A is the 16-bit value returned by read_timer();
Column B is the value returned by custom_wrap_timer_read();
Column C is the original code: (timer_read() % MAX_DEBOUNCE)

    A,     B,     C
65530,    19,    30
65531,    20,    31
65532,    21,    32
65533,    22,    33
65534,    23,    34
65535,    24,    35
    0     25,     0
    1,    26,     1
    2,    27,     2
    3,    28,     3
    4,    29,     4
    5,    30,     5

read_timer() wraps about every 1.09 seconds, and so debouncing might
fail at these times without this commit.

* quantum/debounce/eager_pr and eager_pk: modifications for code readability according to code review.

* quantum/debounce/eager_pr and eager_pk: modifications for code readability according to code review. (2)
2020-04-11 10:14:28 +10:00
..
arm_atsam format code according to conventions [skip ci] 2020-01-24 02:15:29 +00:00
avr format code according to conventions [skip ci] 2020-04-02 21:59:37 +00:00
chibios Refactor rgblight_reconfig.h (#7773) 2020-03-10 01:46:03 -07:00
test Add customisable EEPROM driver selection (#7274) 2020-01-24 12:45:58 +11:00
action.c Strip out features to allow minimum firmware sizes (#8645) 2020-04-01 21:06:22 +01:00
action.h Fix compile issues related to NO_ACTION_MACRO/FUNCTION and LTO_ENABLE (#8663) 2020-04-09 00:18:30 +01:00
action_code.h Remove ACT_COMMAND (#8487) 2020-03-21 16:17:43 +11:00
action_layer.c Fix layer debug calls (#8370) 2020-03-10 20:23:26 +11:00
action_layer.h Strip out features to allow minimum firmware sizes (#8645) 2020-04-01 21:06:22 +01:00
action_macro.c clang-format changes 2019-08-30 15:01:52 -07:00
action_macro.h clang-format changes 2019-08-30 15:01:52 -07:00
action_tapping.c format code according to conventions [skip ci] 2020-02-25 18:57:18 +00:00
action_tapping.h clang-format changes 2019-08-30 15:01:52 -07:00
action_util.c clang-format changes 2019-08-30 15:01:52 -07:00
action_util.h clang-format changes 2019-08-30 15:01:52 -07:00
bootloader.h clang-format changes 2019-08-30 15:01:52 -07:00
bootmagic.c ARM split - Add bootmagic/magic keycodes for setting handedness (#6545) 2019-09-24 15:24:12 +01:00
bootmagic.h ARM split - Add bootmagic/magic keycodes for setting handedness (#6545) 2019-09-24 15:24:12 +01:00
bootmagic_lite.c format code according to conventions [skip ci] 2020-03-21 23:44:27 +00:00
command.c 2020 February 29 Breaking Changes Update (#8064) 2020-02-29 11:59:30 -08:00
command.h clang-format changes 2019-08-30 15:01:52 -07:00
debug.c clang-format changes 2019-08-30 15:01:52 -07:00
debug.h clang-format changes 2019-08-30 15:01:52 -07:00
eeconfig.c Add customisable EEPROM driver selection (#7274) 2020-01-24 12:45:58 +11:00
eeconfig.h VIA Configurator Refactor (#7268) 2020-01-03 12:52:00 -08:00
eeprom.h Add customisable EEPROM driver selection (#7274) 2020-01-24 12:45:58 +11:00
host.c Run clang-format manually to fix recently changed files 2019-11-17 08:25:58 -08:00
host.h New and improved lock LED callbacks (#7215) 2019-11-06 00:42:16 +00:00
host_driver.h clang-format changes 2019-08-30 15:01:52 -07:00
keyboard.c Refactor rgblight_reconfig.h (#7773) 2020-03-10 01:46:03 -07:00
keyboard.h clang-format changes 2019-08-30 15:01:52 -07:00
keycode.h Add 4-character aliases for sendstring keycodes (#7409) 2019-11-22 14:11:28 +11:00
led.h Run clang-format manually to fix recently changed files 2019-11-17 08:25:58 -08:00
magic.c clang-format changes 2019-08-30 15:01:52 -07:00
magic.h MAGIC functionality, AG swap in default layout 2016-04-16 18:51:58 -04:00
matrix.h Allow 30us matrix delay to be keyboard/user overridable (#8216) 2020-02-21 14:49:33 +11:00
mousekey.c format code according to conventions [skip ci] 2020-03-13 17:24:38 +00:00
mousekey.h Decouple mouse cursor and mouse wheel in accelerated mode (#6685) 2020-03-13 12:49:44 -04:00
nodebug.h clang-format changes 2019-08-30 15:01:52 -07:00
print.c clang-format changes 2019-08-30 15:01:52 -07:00
print.h Remove mbed files (#7605) 2019-12-11 11:36:00 -08:00
progmem.h Fix pgm_read_ptr() define for ARM (#8658) 2020-04-02 16:52:16 +01:00
raw_hid.h clang-format changes 2019-08-30 15:01:52 -07:00
report.c format code according to conventions [skip ci] 2020-03-13 18:44:56 +00:00
report.h Fix formatting for report.h (#8512) 2020-03-22 06:19:01 +11:00
sendchar.h clang-format changes 2019-08-30 15:01:52 -07:00
sendchar_null.c clang-format changes 2019-08-30 15:01:52 -07:00
sendchar_uart.c clang-format changes 2019-08-30 15:01:52 -07:00
sleep_led.h clang-format changes 2019-08-30 15:01:52 -07:00
suspend.h clang-format changes 2019-08-30 15:01:52 -07:00
timer.h Various fixes to how timer differences are calculated (#8585) 2020-04-11 10:14:28 +10:00
uart.c format code according to conventions [skip ci] 2020-03-06 13:28:11 +00:00
uart.h clang-format changes 2019-08-30 15:01:52 -07:00
util.c Strip out features to allow minimum firmware sizes (#8645) 2020-04-01 21:06:22 +01:00
util.h Wrap util.h functions in extern "C" (#6762) 2019-10-04 09:24:47 +10:00
virtser.h Added USB Virtual Serial support 2016-08-23 04:16:35 +00:00
wait.h Remove mbed files (#7605) 2019-12-11 11:36:00 -08:00