From db129a9c27c01995e6aad65647d8c89db3f92277 Mon Sep 17 00:00:00 2001 From: Emilio Monti Date: Mon, 18 Mar 2013 17:20:05 +0000 Subject: [PATCH] [KL25Z] Improve division by constant integer in us_ticker implementation. --- .../mbed/vendor/Freescale/capi/us_ticker.c | 13 ++++++++- libraries/tests/mbed/div/main.cpp | 27 +++++++++++++++++++ workspace_tools/make.py | 2 +- workspace_tools/tests.py | 6 +++++ 4 files changed, 46 insertions(+), 2 deletions(-) create mode 100644 libraries/tests/mbed/div/main.cpp diff --git a/libraries/mbed/vendor/Freescale/capi/us_ticker.c b/libraries/mbed/vendor/Freescale/capi/us_ticker.c index 8b51ea1009..88205094cc 100644 --- a/libraries/mbed/vendor/Freescale/capi/us_ticker.c +++ b/libraries/mbed/vendor/Freescale/capi/us_ticker.c @@ -69,7 +69,18 @@ uint32_t us_ticker_read() { uint64_t ticks; ticks = (uint64_t)PIT->LTMR64H << 32; ticks |= (uint64_t)PIT->LTMR64L; - ticks = (~ticks) / 24; + + // More efficient division by constant integer (24): /8 /3 + // complement (because count down timer) and divide by 8 + ticks = (~ticks) >> 3; + + // divide by 3 + if (ticks > 0xFFFFFFFF) { + ticks /= 3; + } else { + ticks = (ticks * 0x55555556) >> 32; + } + return (uint32_t)(0xFFFFFFFF & ticks); } diff --git a/libraries/tests/mbed/div/main.cpp b/libraries/tests/mbed/div/main.cpp new file mode 100644 index 0000000000..5ad7e5e20a --- /dev/null +++ b/libraries/tests/mbed/div/main.cpp @@ -0,0 +1,27 @@ +#include "mbed.h" + +uint32_t test_64(uint64_t ticks) { + ticks >>= 3; // divide by 8 + if (ticks > 0xFFFFFFFF) { + ticks /= 3; + } else { + ticks = (ticks * 0x55555556) >> 32; // divide by 3 + } + return (uint32_t)(0xFFFFFFFF & ticks); +} + +int main() { + // 0xFFFFFFFF * 8 = 0x7fffffff8 + printf("expected : 55555555\n"); + printf("64: 0x7fffffff8: %x\n", test_64(0x7fffffff8)); + + // 0xFFFFFFFF * 24 = 0x17ffffffe8 + printf("expected : ffffffff\n"); + printf("64: 0x17ffffffe8: %x\n", test_64(0x17FFFFFFE8)); + + DigitalOut led(LED1); + while (1) { + led = !led; + wait(0.5); + } +} diff --git a/workspace_tools/make.py b/workspace_tools/make.py index 3b59c72cd7..f464a52a1c 100644 --- a/workspace_tools/make.py +++ b/workspace_tools/make.py @@ -88,7 +88,7 @@ if __name__ == '__main__': copy(bin, options.disk) if options.serial: - if options.mcu in ["M0+", "LPC812"]: + if options.mcu in ["KL25Z", "LPC812"]: # We do not have a flash disk where to store the image, we write # it directly on the target chip, therefore we need to # disconnect the interface: wait for the device to enumerate diff --git a/workspace_tools/tests.py b/workspace_tools/tests.py index a3df46337a..76a14622b9 100644 --- a/workspace_tools/tests.py +++ b/workspace_tools/tests.py @@ -296,6 +296,12 @@ TESTS = [ "source_dir": join(TEST_DIR, "mbed", "time_us"), "dependencies": [MBED_LIBRARIES, TEST_MBED_LIB], }, + { + "id": "MBED_26", "description": "MBED: Integer constant division", + "source_dir": join(TEST_DIR, "mbed", "div"), + "dependencies": [MBED_LIBRARIES, TEST_MBED_LIB], + }, + # CMSIS RTOS tests { "id": "CMSIS_RTOS_1", "description": "CMSIS RTOS: Basic",