From 2dc4209c0e7df04cbf9746ec064ead2a5191ab3e Mon Sep 17 00:00:00 2001 From: Ken Bannister Date: Mon, 30 Sep 2019 08:15:21 -0400 Subject: [PATCH] net/nanocoap: optimize determination of exponent for block szx --- sys/net/application_layer/nanocoap/nanocoap.c | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/sys/net/application_layer/nanocoap/nanocoap.c b/sys/net/application_layer/nanocoap/nanocoap.c index b764441af0..df0853c349 100644 --- a/sys/net/application_layer/nanocoap/nanocoap.c +++ b/sys/net/application_layer/nanocoap/nanocoap.c @@ -26,6 +26,7 @@ #include #include +#include "bitarithm.h" #include "net/nanocoap.h" #define ENABLE_DEBUG (0) @@ -655,16 +656,15 @@ size_t coap_put_option(uint8_t *buf, uint16_t lastonum, uint16_t onum, const uin static unsigned _size2szx(size_t size) { - unsigned szx = 0; assert(size <= 1024); - while (size) { - size = size >> 1; - szx++; - } - /* Size exponent + 1 */ - assert(szx >= 5); - return szx - 5; + /* We must wait to subract the szx offset of 4 until after the assert below. + * Input should be a power of two, but if not it may have a stray low order + * '1' bit that would invalidate the subtraction. */ + unsigned szx = bitarithm_lsb(size); + + assert(szx >= 4); + return szx - 4; } static unsigned _slicer2blkopt(coap_block_slicer_t *slicer, bool more)