]> git.friedersdorff.com Git - max/tmk_keyboard.git/commit
tmk_core/common/timer.h: Improve code generation for TIMER_DIFF* macros
authorPurdea Andrei <andrei@purdea.ro>
Wed, 1 Apr 2020 04:46:18 +0000 (07:46 +0300)
committerPurdea Andrei <andrei@purdea.ro>
Wed, 1 Apr 2020 04:46:18 +0000 (07:46 +0300)
commit061e6623453477c6a128c674a20696693ddc7c71
treeb3a63f2a65b45ddd76b0e41a1ca49ceaddb7d942
parent338a1506dc522f81a8386ab8848c8e37a1e3e3b5
tmk_core/common/timer.h: 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.
tmk_core/common/timer.h