From: Zach W. <zw...@us...> - 2009-11-16 10:27:38
|
This is an automated email from the git hooks/post-receive script. It was generated because a ref change was pushed to the repository containing the project "Main OpenOCD repository". The branch, master has been updated via 45527ee82c9e7c93b29b79f01f52d663960649c2 (commit) via d6348d4316ea71b9ad232fd63a0a39ed84d8e8b7 (commit) via 21b452cf675bd3bb307c0d58c6cc45a028d7df3a (commit) via d8d8c5d8c3e47a38059952c3407bb819c5c33d05 (commit) via d09e308130619f8667b6f08399cfc7d16dddab36 (commit) via e4ee891759b08d3edb258b34f00b4ae8e3298d06 (commit) via d50caa97d17187ed96746cc1527e5dbf57d4a81a (commit) via 5a43bd2e185bf469561a8370dcd543cc4813c33f (commit) via 82fc2f9628e0f1dbd9010e0146ff63832e4a77a1 (commit) from b695cb75220100cd9bbfaec5bd1740958454130d (commit) Those revisions listed above that are new to this repository have not appeared on any other notification email; so we list those revisions in full, below. - Log ----------------------------------------------------------------- commit 45527ee82c9e7c93b29b79f01f52d663960649c2 Author: Zachary T Welch <zw...@su...> Date: Mon Nov 16 01:10:46 2009 -0800 binarybuffer: add API documentation Adds Doxygen documentation for a number of the binarybuffer APIs, including "unexpected" behavior exposed during review on the list. diff --git a/src/helper/binarybuffer.h b/src/helper/binarybuffer.h index e8931ab..9e0cc9b 100644 --- a/src/helper/binarybuffer.h +++ b/src/helper/binarybuffer.h @@ -29,6 +29,15 @@ * Support functions to access arbitrary bits in a byte array */ +/** + * Sets @c num bits in @c _buffer, starting at the @c first bit, + * using the bits in @c value. This routine fast-paths writes + * of little-endian, byte-aligned, 32-bit words. + * @param _buffer The buffer whose bits will be set. + * @param first The bit offset in @c _buffer to start writing (0-31). + * @param num The number of bits from @c value to copy (1-32). + * @param value Up to 32 bits that will be copied to _buffer. + */ static inline void buf_set_u32(void *_buffer, unsigned first, unsigned num, uint32_t value) { @@ -48,6 +57,15 @@ static inline void buf_set_u32(void *_buffer, } } } +/** + * Retrieves @c num bits from @c _buffer, starting at the @c first bit, + * returning the bits in a 32-bit word. This routine fast-paths reads + * of little-endian, byte-aligned, 32-bit words. + * @param _buffer The buffer whose bits will be read. + * @param first The bit offset in @c _buffer to start reading (0-31). + * @param num The number of bits from @c _buffer to read (1-32). + * @returns Up to 32-bits that were read from @c _buffer. + */ static inline uint32_t buf_get_u32(const void *_buffer, unsigned first, unsigned num) { @@ -68,16 +86,36 @@ static inline uint32_t buf_get_u32(const void *_buffer, } } -/// flip_u32 inverts the bit order inside a 32-bit word (31..0 -> 0..31) -uint32_t flip_u32(uint32_t value, unsigned num); +/** + * Inverts the ordering of bits inside a 32-bit word (e.g. 31..0 -> 0..31). + * This routine can be used to flip smaller data types by using smaller + * values for @c width. + * @param value The word to flip. + * @param width The number of bits in value (2-32). + * @returns A 32-bit word with @c value in reversed bit-order. + */ +uint32_t flip_u32(uint32_t value, unsigned width); bool buf_cmp(const void *buf1, const void *buf2, unsigned size); bool buf_cmp_mask(const void *buf1, const void *buf2, const void *mask, unsigned size); +/** + * Copies @c size bits out of @c from and into @c to. Any extra + * bits in the final byte will be set to zero. + * @param from The buffer to copy into @c to. + * @param to The buffer that will receive the copy of @c from. + * @param size The number of bits to copy. + */ void* buf_cpy(const void *from, void *to, unsigned size); -void* buf_set_ones(void *buf, unsigned count); +/** + * Set the contents of @c buf with @c count bits, all set to 1. + * @param buf The buffer to fill with ones. + * @param size The number of bits. + * @returns The original buffer (@c buf). + */ +void* buf_set_ones(void *buf, unsigned size); void* buf_set_buf(const void *src, unsigned src_start, void *dst, unsigned dst_start, unsigned len); commit d6348d4316ea71b9ad232fd63a0a39ed84d8e8b7 Author: Zachary T Welch <zw...@su...> Date: Sat Nov 14 06:26:55 2009 -0800 improve inline binarybuffer helpers Use void*, unsigned, and bool types with inline helpers. diff --git a/src/helper/binarybuffer.h b/src/helper/binarybuffer.h index 9397fcf..e8931ab 100644 --- a/src/helper/binarybuffer.h +++ b/src/helper/binarybuffer.h @@ -29,10 +29,10 @@ * Support functions to access arbitrary bits in a byte array */ -/* inlining this will help show what fn that is taking time during profiling. */ -static inline void buf_set_u32(uint8_t* buffer, - unsigned int first, unsigned int num, uint32_t value) +static inline void buf_set_u32(void *_buffer, + unsigned first, unsigned num, uint32_t value) { + char *buffer = (char *)_buffer; if ((num == 32) && (first == 0)) { buffer[3] = (value >> 24) & 0xff; buffer[2] = (value >> 16) & 0xff; @@ -48,9 +48,10 @@ static inline void buf_set_u32(uint8_t* buffer, } } } -static inline uint32_t buf_get_u32(const uint8_t* buffer, - unsigned int first, unsigned int num) +static inline uint32_t buf_get_u32(const void *_buffer, + unsigned first, unsigned num) { + char *buffer = (char *)_buffer; if ((num == 32) && (first == 0)) { return (((uint32_t)buffer[3]) << 24) | (((uint32_t)buffer[2]) << 16) | @@ -68,7 +69,7 @@ static inline uint32_t buf_get_u32(const uint8_t* buffer, } /// flip_u32 inverts the bit order inside a 32-bit word (31..0 -> 0..31) -uint32_t flip_u32(uint32_t value, unsigned int num); +uint32_t flip_u32(uint32_t value, unsigned num); bool buf_cmp(const void *buf1, const void *buf2, unsigned size); bool buf_cmp_mask(const void *buf1, const void *buf2, @@ -88,7 +89,7 @@ char* buf_to_str(const void *buf, unsigned size, unsigned radix); #define CEIL(m, n) (((m) + (n) - 1) / (n)) /* read a uint32_t from a buffer in target memory endianness */ -static inline uint32_t fast_target_buffer_get_u32(const uint8_t *p, int le) +static inline uint32_t fast_target_buffer_get_u32(const void *p, bool le) { return le ? le_to_h_u32(p) : be_to_h_u32(p); } commit 21b452cf675bd3bb307c0d58c6cc45a028d7df3a Author: Zachary T Welch <zw...@su...> Date: Sat Nov 14 10:44:37 2009 -0800 improve buf_set_buf helper Use void * and unsigned types for buffer and their sizes. Allows it to be used with more than uint8_t * without casts. diff --git a/src/helper/binarybuffer.c b/src/helper/binarybuffer.c index 8275d12..53ad4d3 100644 --- a/src/helper/binarybuffer.c +++ b/src/helper/binarybuffer.c @@ -128,11 +128,14 @@ void* buf_set_ones(void *_buf, unsigned size) return buf; } -uint8_t* buf_set_buf(const uint8_t *src, int src_start, uint8_t *dst, int dst_start, int len) +void* buf_set_buf(const void *_src, unsigned src_start, + void *_dst, unsigned dst_start, unsigned len) { - int src_idx = src_start, dst_idx = dst_start; + const uint8_t *src = _src; + uint8_t *dst = _dst; - for (int i = 0; i < len; i++) + unsigned src_idx = src_start, dst_idx = dst_start; + for (unsigned i = 0; i < len; i++) { if (((src[src_idx / 8] >> (src_idx % 8)) & 1) == 1) dst[dst_idx / 8] |= 1 << (dst_idx % 8); diff --git a/src/helper/binarybuffer.h b/src/helper/binarybuffer.h index b988e40..9397fcf 100644 --- a/src/helper/binarybuffer.h +++ b/src/helper/binarybuffer.h @@ -78,8 +78,8 @@ void* buf_cpy(const void *from, void *to, unsigned size); void* buf_set_ones(void *buf, unsigned count); -uint8_t* buf_set_buf(const uint8_t *src, int src_start, - uint8_t *dst, int dst_start, int len); +void* buf_set_buf(const void *src, unsigned src_start, + void *dst, unsigned dst_start, unsigned len); int str_to_buf(const char *str, unsigned len, void *bin_buf, unsigned buf_size, unsigned radix); commit d8d8c5d8c3e47a38059952c3407bb819c5c33d05 Author: Zachary T Welch <zw...@su...> Date: Sat Nov 14 10:41:35 2009 -0800 improve buf_set_ones Use memset instead of loop. Improve types, using void * and unsigned. diff --git a/src/helper/binarybuffer.c b/src/helper/binarybuffer.c index e5f9854..8275d12 100644 --- a/src/helper/binarybuffer.c +++ b/src/helper/binarybuffer.c @@ -113,17 +113,17 @@ bool buf_cmp_mask(const void *_buf1, const void *_buf2, } -uint8_t* buf_set_ones(uint8_t *buf, int count) +void* buf_set_ones(void *_buf, unsigned size) { - for (unsigned i = 0, num_bytes = CEIL(count, 8); i < num_bytes; i++) - { - if (count >= 8) - buf[i] = 0xff; - else - buf[i] = (1 << count) - 1; + uint8_t *buf = _buf; + if (!buf) + return NULL; - count -= 8; - } + memset(buf, 0xff, size / 8); + + unsigned trailing_bits = size % 8; + if (trailing_bits) + buf[size / 8] = (1 << trailing_bits) - 1; return buf; } diff --git a/src/helper/binarybuffer.h b/src/helper/binarybuffer.h index 07a5862..b988e40 100644 --- a/src/helper/binarybuffer.h +++ b/src/helper/binarybuffer.h @@ -76,7 +76,8 @@ bool buf_cmp_mask(const void *buf1, const void *buf2, void* buf_cpy(const void *from, void *to, unsigned size); -uint8_t* buf_set_ones(uint8_t *buf, int count); +void* buf_set_ones(void *buf, unsigned count); + uint8_t* buf_set_buf(const uint8_t *src, int src_start, uint8_t *dst, int dst_start, int len); commit d09e308130619f8667b6f08399cfc7d16dddab36 Author: Zachary T Welch <zw...@su...> Date: Sat Nov 14 10:36:57 2009 -0800 improve buf_cpy helper Use memcpy for bulk of copy, improve final byte handling. Improve types by using void * for buffers and unsigned for size. diff --git a/src/helper/binarybuffer.c b/src/helper/binarybuffer.c index 865d3a3..e5f9854 100644 --- a/src/helper/binarybuffer.c +++ b/src/helper/binarybuffer.c @@ -48,21 +48,22 @@ const unsigned char bit_reverse_table256[] = }; -uint8_t* buf_cpy(const uint8_t *from, uint8_t *to, int size) +void* buf_cpy(const void *from, void *_to, unsigned size) { - if (from == NULL) + if (NULL == from || NULL == _to) return NULL; - for (unsigned i = 0, num_bytes = CEIL(size, 8); i < num_bytes; i++) - to[i] = from[i]; + // copy entire buffer + memcpy(_to, from, CEIL(size, 8)); /* mask out bits that don't belong to the buffer */ - if (size % 8) + unsigned trailing_bits = size % 8; + if (trailing_bits) { - to[size / 8] &= (0xff >> (8 - (size % 8))); + uint8_t *to = _to; + to[size / 8] &= (1 << trailing_bits) - 1; } - - return to; + return _to; } static bool buf_cmp_masked(uint8_t a, uint8_t b, uint8_t m) diff --git a/src/helper/binarybuffer.h b/src/helper/binarybuffer.h index a51c2e5..07a5862 100644 --- a/src/helper/binarybuffer.h +++ b/src/helper/binarybuffer.h @@ -73,7 +73,8 @@ uint32_t flip_u32(uint32_t value, unsigned int num); bool buf_cmp(const void *buf1, const void *buf2, unsigned size); bool buf_cmp_mask(const void *buf1, const void *buf2, const void *mask, unsigned size); -uint8_t* buf_cpy(const uint8_t *from, uint8_t *to, int size); + +void* buf_cpy(const void *from, void *to, unsigned size); uint8_t* buf_set_ones(uint8_t *buf, int count); uint8_t* buf_set_buf(const uint8_t *src, int src_start, commit e4ee891759b08d3edb258b34f00b4ae8e3298d06 Author: Zachary T Welch <zw...@su...> Date: Sat Nov 14 10:27:34 2009 -0800 improve buf_cmp and buf_cmp_mask helpers Rewrite buf_cmp to use memcpy for bulk of comparison. Add static helper to perform comparison of trailing byte, which uses another static helper to perform a maksed comparison. The masked comparison helper is used by the buf_cmp_mask to simplify its loop. Improve types to use void *, unsigned, and return bool. diff --git a/src/helper/binarybuffer.c b/src/helper/binarybuffer.c index 0def948..865d3a3 100644 --- a/src/helper/binarybuffer.c +++ b/src/helper/binarybuffer.c @@ -65,52 +65,53 @@ uint8_t* buf_cpy(const uint8_t *from, uint8_t *to, int size) return to; } -int buf_cmp(const uint8_t *buf1, const uint8_t *buf2, int size) +static bool buf_cmp_masked(uint8_t a, uint8_t b, uint8_t m) { - if (!buf1 || !buf2) - return 1; + return (a & m) != (b & m); +} +static bool buf_cmp_trailing(uint8_t a, uint8_t b, uint8_t m, unsigned trailing) +{ + uint8_t mask = (1 << trailing) - 1; + return buf_cmp_masked(a, b, mask & m); +} - for (unsigned i = 0, num_bytes = CEIL(size, 8); i < num_bytes; i++) - { - /* last byte */ - /* mask out bits that don't really belong to the buffer if size isn't a multiple of 8 bits */ - if ((size % 8) && (i == num_bytes -1)) - { - if ((buf1[i] & ((1 << (size % 8)) - 1)) != (buf2[i] & ((1 << (size % 8)) - 1))) - return 1; - } - else - { - if (buf1[i] != buf2[i]) - return 1; - } - } +bool buf_cmp(const void *_buf1, const void *_buf2, unsigned size) +{ + if (!_buf1 || !_buf2) + return _buf1 != _buf2; - return 0; + unsigned last = size / 8; + if (memcmp(_buf1, _buf2, last) != 0) + return false; + + unsigned trailing = size % 8; + if (!trailing) + return false; + + const uint8_t *buf1 = _buf1, *buf2 = _buf2; + return buf_cmp_trailing(buf1[last], buf2[last], 0xff, trailing); } -int buf_cmp_mask(const uint8_t *buf1, const uint8_t *buf2, const uint8_t *mask, int size) +bool buf_cmp_mask(const void *_buf1, const void *_buf2, + const void *_mask, unsigned size) { - for (unsigned i = 0, num_bytes = CEIL(size, 8); i < num_bytes; i++) + if (!_buf1 || !_buf2) + return _buf1 != _buf2 || _buf1 != _mask; + + const uint8_t *buf1 = _buf1, *buf2 = _buf2, *mask = _mask; + unsigned last = size / 8; + for (unsigned i = 0; i < last; i++) { - /* last byte */ - /* mask out bits that don't really belong to the buffer if size isn't a multiple of 8 bits */ - if ((size % 8) && (i == num_bytes -1)) - { - if ((buf1[i] & ((1 << (size % 8)) - 1) & mask[i]) != - (buf2[i] & ((1 << (size % 8)) - 1) & mask[i])) - return 1; - } - else - { - if ((buf1[i] & mask[i]) != (buf2[i] & mask[i])) - return 1; - } + if (buf_cmp_masked(buf1[i], buf2[i], mask[i])) + return true; } - - return 0; + unsigned trailing = size % 8; + if (!trailing) + return false; + return buf_cmp_trailing(buf1[last], buf2[last], mask[last], trailing); } + uint8_t* buf_set_ones(uint8_t *buf, int count) { for (unsigned i = 0, num_bytes = CEIL(count, 8); i < num_bytes; i++) diff --git a/src/helper/binarybuffer.h b/src/helper/binarybuffer.h index 4905007..a51c2e5 100644 --- a/src/helper/binarybuffer.h +++ b/src/helper/binarybuffer.h @@ -70,9 +70,9 @@ static inline uint32_t buf_get_u32(const uint8_t* buffer, /// flip_u32 inverts the bit order inside a 32-bit word (31..0 -> 0..31) uint32_t flip_u32(uint32_t value, unsigned int num); -int buf_cmp(const uint8_t *buf1, const uint8_t *buf2, int size); -int buf_cmp_mask(const uint8_t *buf1, const uint8_t *buf2, - const uint8_t *mask, int size); +bool buf_cmp(const void *buf1, const void *buf2, unsigned size); +bool buf_cmp_mask(const void *buf1, const void *buf2, + const void *mask, unsigned size); uint8_t* buf_cpy(const uint8_t *from, uint8_t *to, int size); uint8_t* buf_set_ones(uint8_t *buf, int count); commit d50caa97d17187ed96746cc1527e5dbf57d4a81a Author: Zachary T Welch <zw...@su...> Date: Sat Nov 14 10:14:04 2009 -0800 improve str_to_buf and buf_to_str helpers Improve types: use void * and unsigned. Move all variables to point of first use. Move radix guessing logic to new str_radix_guess helper. diff --git a/src/helper/binarybuffer.c b/src/helper/binarybuffer.c index 95e084e..0def948 100644 --- a/src/helper/binarybuffer.c +++ b/src/helper/binarybuffer.c @@ -169,43 +169,36 @@ int ceil_f_to_u32(float x) return y; } -char* buf_to_str(const uint8_t *buf, int buf_len, int radix) +char* buf_to_str(const void *_buf, unsigned buf_len, unsigned radix) { - const char *DIGITS = "0123456789ABCDEF"; float factor; - char *str; - int str_len; - int b256_len = CEIL(buf_len, 8); - uint32_t tmp; - - int j; /* base-256 digits */ - int i; /* output digits (radix) */ - - if (radix == 16) - { + switch (radix) { + case 16: factor = 2.0; /* log(256) / log(16) = 2.0 */ - } - else if (radix == 10) - { + break; + case 10: factor = 2.40824; /* log(256) / log(10) = 2.40824 */ - } - else if (radix == 8) - { + break; + case 8: factor = 2.66667; /* log(256) / log(8) = 2.66667 */ - } - else + break; + default: return NULL; + } - str_len = ceil_f_to_u32(CEIL(buf_len, 8) * factor); - str = calloc(str_len + 1, 1); + unsigned str_len = ceil_f_to_u32(CEIL(buf_len, 8) * factor); + char *str = calloc(str_len + 1, 1); - for (i = b256_len - 1; i >= 0; i--) + const uint8_t *buf = _buf; + int b256_len = CEIL(buf_len, 8); + for (int i = b256_len - 1; i >= 0; i--) { - tmp = buf[i]; - if ((i == (buf_len / 8)) && (buf_len % 8)) + uint32_t tmp = buf[i]; + if (((unsigned)i == (buf_len / 8)) && (buf_len % 8)) tmp &= (0xff >> (8 - (buf_len % 8))); - for (j = str_len; j > 0; j--) + /* base-256 digits */ + for (unsigned j = str_len; j > 0; j--) { tmp += (uint32_t)str[j-1] * 256; str[j-1] = (uint8_t)(tmp % radix); @@ -213,44 +206,49 @@ char* buf_to_str(const uint8_t *buf, int buf_len, int radix) } } - for (j = 0; j < str_len; j++) + const char *DIGITS = "0123456789ABCDEF"; + for (unsigned j = 0; j < str_len; j++) str[j] = DIGITS[(int)str[j]]; return str; } -int str_to_buf(const char *str, int str_len, uint8_t *buf, int buf_len, int radix) +/// identify radix, and skip radix-prefix (0, 0x or 0X) +static void str_radix_guess(const char **_str, unsigned *_str_len, + unsigned *_radix) { - char *charbuf; - uint32_t tmp; - float factor; - uint8_t *b256_buf; - int b256_len; - - int j; /* base-256 digits */ - int i; /* input digits (ASCII) */ - - if (radix == 0) + unsigned radix = *_radix; + if (0 != radix) + return; + const char *str = *_str; + unsigned str_len = *_str_len; + if (str[0] == '0' && (str[1] == 'x' || str[1] == 'X')) { - /* identify radix, and skip radix-prefix (0, 0x or 0X) */ - if ((str[0] == '0') && (str[1] && ((str[1] == 'x') || (str[1] == 'X')))) - { - radix = 16; - str += 2; - str_len -= 2; - } - else if ((str[0] == '0') && (str_len != 1)) - { - radix = 8; - str += 1; - str_len -= 1; - } - else - { - radix = 10; - } + radix = 16; + str += 2; + str_len -= 2; } + else if ((str[0] == '0') && (str_len != 1)) + { + radix = 8; + str += 1; + str_len -= 1; + } + else + { + radix = 10; + } + *_str = str; + *_str_len = str_len; + *_radix = radix; +} + +int str_to_buf(const char *str, unsigned str_len, + void *_buf, unsigned buf_len, unsigned radix) +{ + str_radix_guess(&str, &str_len, &radix); + float factor; if (radix == 16) factor = 0.5; /* log(16) / log(256) = 0.5 */ else if (radix == 10) @@ -261,18 +259,20 @@ int str_to_buf(const char *str, int str_len, uint8_t *buf, int buf_len, int radi return 0; /* copy to zero-terminated buffer */ - charbuf = malloc(str_len + 1); + char *charbuf = malloc(str_len + 1); memcpy(charbuf, str, str_len); charbuf[str_len] = '\0'; /* number of digits in base-256 notation */ - b256_len = ceil_f_to_u32(str_len * factor); - b256_buf = calloc(b256_len, 1); + unsigned b256_len = ceil_f_to_u32(str_len * factor); + uint8_t *b256_buf = calloc(b256_len, 1); /* go through zero terminated buffer */ + /* input digits (ASCII) */ + unsigned i; for (i = 0; charbuf[i]; i++) { - tmp = charbuf[i]; + uint32_t tmp = charbuf[i]; if ((tmp >= '0') && (tmp <= '9')) tmp = (tmp - '0'); else if ((tmp >= 'a') && (tmp <= 'f')) @@ -281,10 +281,11 @@ int str_to_buf(const char *str, int str_len, uint8_t *buf, int buf_len, int radi tmp = (tmp - 'A' + 10); else continue; /* skip characters other than [0-9,a-f,A-F] */ - if (tmp >= (uint32_t)radix) + if (tmp >= radix) continue; /* skip digits invalid for the current radix */ - for (j = 0; j < b256_len; j++) + /* base-256 digits */ + for (unsigned j = 0; j < b256_len; j++) { tmp += (uint32_t)b256_buf[j] * radix; b256_buf[j] = (uint8_t)(tmp & 0xFF); @@ -293,7 +294,8 @@ int str_to_buf(const char *str, int str_len, uint8_t *buf, int buf_len, int radi } - for (j = 0; j < CEIL(buf_len, 8); j++) + uint8_t *buf = _buf; + for (unsigned j = 0; j < CEIL(buf_len, 8); j++) { if (j < b256_len) buf[j] = b256_buf[j]; diff --git a/src/helper/binarybuffer.h b/src/helper/binarybuffer.h index 400cbbe..4905007 100644 --- a/src/helper/binarybuffer.h +++ b/src/helper/binarybuffer.h @@ -79,9 +79,9 @@ uint8_t* buf_set_ones(uint8_t *buf, int count); uint8_t* buf_set_buf(const uint8_t *src, int src_start, uint8_t *dst, int dst_start, int len); -int str_to_buf(const char *str, int len, - uint8_t *bin_buf, int buf_size, int radix); -char* buf_to_str(const uint8_t *buf, int size, int radix); +int str_to_buf(const char *str, unsigned len, + void *bin_buf, unsigned buf_size, unsigned radix); +char* buf_to_str(const void *buf, unsigned size, unsigned radix); #define CEIL(m, n) (((m) + (n) - 1) / (n)) commit 5a43bd2e185bf469561a8370dcd543cc4813c33f Author: Zachary T Welch <zw...@su...> Date: Sat Nov 14 08:19:24 2009 -0800 binarybuffer: move variables to point of first use Reduce some noise from subsequent patches. diff --git a/src/helper/binarybuffer.c b/src/helper/binarybuffer.c index d813ecf..95e084e 100644 --- a/src/helper/binarybuffer.c +++ b/src/helper/binarybuffer.c @@ -50,13 +50,10 @@ const unsigned char bit_reverse_table256[] = uint8_t* buf_cpy(const uint8_t *from, uint8_t *to, int size) { - unsigned int num_bytes = CEIL(size, 8); - unsigned int i; - if (from == NULL) return NULL; - for (i = 0; i < num_bytes; i++) + for (unsigned i = 0, num_bytes = CEIL(size, 8); i < num_bytes; i++) to[i] = from[i]; /* mask out bits that don't belong to the buffer */ @@ -70,13 +67,10 @@ uint8_t* buf_cpy(const uint8_t *from, uint8_t *to, int size) int buf_cmp(const uint8_t *buf1, const uint8_t *buf2, int size) { - int num_bytes = CEIL(size, 8); - int i; - if (!buf1 || !buf2) return 1; - for (i = 0; i < num_bytes; i++) + for (unsigned i = 0, num_bytes = CEIL(size, 8); i < num_bytes; i++) { /* last byte */ /* mask out bits that don't really belong to the buffer if size isn't a multiple of 8 bits */ @@ -97,10 +91,7 @@ int buf_cmp(const uint8_t *buf1, const uint8_t *buf2, int size) int buf_cmp_mask(const uint8_t *buf1, const uint8_t *buf2, const uint8_t *mask, int size) { - int num_bytes = CEIL(size, 8); - int i; - - for (i = 0; i < num_bytes; i++) + for (unsigned i = 0, num_bytes = CEIL(size, 8); i < num_bytes; i++) { /* last byte */ /* mask out bits that don't really belong to the buffer if size isn't a multiple of 8 bits */ @@ -122,10 +113,7 @@ int buf_cmp_mask(const uint8_t *buf1, const uint8_t *buf2, const uint8_t *mask, uint8_t* buf_set_ones(uint8_t *buf, int count) { - int num_bytes = CEIL(count, 8); - int i; - - for (i = 0; i < num_bytes; i++) + for (unsigned i = 0, num_bytes = CEIL(count, 8); i < num_bytes; i++) { if (count >= 8) buf[i] = 0xff; @@ -141,9 +129,8 @@ uint8_t* buf_set_ones(uint8_t *buf, int count) uint8_t* buf_set_buf(const uint8_t *src, int src_start, uint8_t *dst, int dst_start, int len) { int src_idx = src_start, dst_idx = dst_start; - int i; - for (i = 0; i < len; i++) + for (int i = 0; i < len; i++) { if (((src[src_idx / 8] >> (src_idx % 8)) & 1) == 1) dst[dst_idx / 8] |= 1 << (dst_idx % 8); @@ -158,9 +145,7 @@ uint8_t* buf_set_buf(const uint8_t *src, int src_start, uint8_t *dst, int dst_st uint32_t flip_u32(uint32_t value, unsigned int num) { - uint32_t c; - - c = (bit_reverse_table256[value & 0xff] << 24) | + uint32_t c = (bit_reverse_table256[value & 0xff] << 24) | (bit_reverse_table256[(value >> 8) & 0xff] << 16) | (bit_reverse_table256[(value >> 16) & 0xff] << 8) | (bit_reverse_table256[(value >> 24) & 0xff]); @@ -173,12 +158,10 @@ uint32_t flip_u32(uint32_t value, unsigned int num) int ceil_f_to_u32(float x) { - uint32_t y; - if (x < 0) /* return zero for negative numbers */ return 0; - y = x; /* cut off fraction */ + uint32_t y = x; /* cut off fraction */ if ((x - y) > 0.0) /* if there was a fractional part, increase by one */ y++; diff --git a/src/helper/binarybuffer.h b/src/helper/binarybuffer.h index 60077b3..400cbbe 100644 --- a/src/helper/binarybuffer.h +++ b/src/helper/binarybuffer.h @@ -39,8 +39,7 @@ static inline void buf_set_u32(uint8_t* buffer, buffer[1] = (value >> 8) & 0xff; buffer[0] = (value >> 0) & 0xff; } else { - unsigned int i; - for (i = first; i < first + num; i++) + for (unsigned i = first; i < first + num; i++) { if (((value >> (i - first)) & 1) == 1) buffer[i / 8] |= 1 << (i % 8); @@ -59,8 +58,7 @@ static inline uint32_t buf_get_u32(const uint8_t* buffer, (((uint32_t)buffer[0]) << 0); } else { uint32_t result = 0; - unsigned int i; - for (i = first; i < first + num; i++) + for (unsigned i = first; i < first + num; i++) { if (((buffer[i / 8] >> (i % 8)) & 1) == 1) result |= 1 << (i - first); commit 82fc2f9628e0f1dbd9010e0146ff63832e4a77a1 Author: Zachary T Welch <zw...@su...> Date: Sat Nov 14 08:10:22 2009 -0800 binarybuffer: fix whitespace related issues Add inter-operator whitespace. Improve existing documentation. diff --git a/src/helper/binarybuffer.c b/src/helper/binarybuffer.c index fdfcf71..d813ecf 100644 --- a/src/helper/binarybuffer.c +++ b/src/helper/binarybuffer.c @@ -145,10 +145,10 @@ uint8_t* buf_set_buf(const uint8_t *src, int src_start, uint8_t *dst, int dst_st for (i = 0; i < len; i++) { - if (((src[src_idx/8] >> (src_idx % 8)) & 1) == 1) - dst[dst_idx/8] |= 1 << (dst_idx%8); + if (((src[src_idx / 8] >> (src_idx % 8)) & 1) == 1) + dst[dst_idx / 8] |= 1 << (dst_idx % 8); else - dst[dst_idx/8] &= ~(1 << (dst_idx%8)); + dst[dst_idx / 8] &= ~(1 << (dst_idx % 8)); dst_idx++; src_idx++; } diff --git a/src/helper/binarybuffer.h b/src/helper/binarybuffer.h index 2399a6e..60077b3 100644 --- a/src/helper/binarybuffer.h +++ b/src/helper/binarybuffer.h @@ -25,38 +25,34 @@ #include "types.h" -/* support functions to access arbitrary bits in a byte array - * flip_u32 inverses the bit order inside a 32-bit word (31..0 -> 0..31) +/** @file + * Support functions to access arbitrary bits in a byte array */ /* inlining this will help show what fn that is taking time during profiling. */ static inline void buf_set_u32(uint8_t* buffer, unsigned int first, unsigned int num, uint32_t value) { - if ((num == 32) && (first == 0)) - { - buffer[3]=(value >> 24)&0xff; - buffer[2]=(value >> 16)&0xff; - buffer[1]=(value >> 8)&0xff; - buffer[0]=(value >> 0)&0xff; - } else - { + if ((num == 32) && (first == 0)) { + buffer[3] = (value >> 24) & 0xff; + buffer[2] = (value >> 16) & 0xff; + buffer[1] = (value >> 8) & 0xff; + buffer[0] = (value >> 0) & 0xff; + } else { unsigned int i; - for (i = first; i < first + num; i++) { - if (((value >> (i-first))&1) == 1) - buffer[i/8] |= 1 << (i%8); + if (((value >> (i - first)) & 1) == 1) + buffer[i / 8] |= 1 << (i % 8); else - buffer[i/8] &= ~(1 << (i%8)); + buffer[i / 8] &= ~(1 << (i % 8)); } } } static inline uint32_t buf_get_u32(const uint8_t* buffer, unsigned int first, unsigned int num) { - if ((num == 32) && (first == 0)) - { + if ((num == 32) && (first == 0)) { return (((uint32_t)buffer[3]) << 24) | (((uint32_t)buffer[2]) << 16) | (((uint32_t)buffer[1]) << 8) | @@ -64,17 +60,16 @@ static inline uint32_t buf_get_u32(const uint8_t* buffer, } else { uint32_t result = 0; unsigned int i; - for (i = first; i < first + num; i++) { - if (((buffer[i/8]>>(i%8))&1) == 1) - result |= 1 << (i-first); + if (((buffer[i / 8] >> (i % 8)) & 1) == 1) + result |= 1 << (i - first); } - return result; } } +/// flip_u32 inverts the bit order inside a 32-bit word (31..0 -> 0..31) uint32_t flip_u32(uint32_t value, unsigned int num); int buf_cmp(const uint8_t *buf1, const uint8_t *buf2, int size); ----------------------------------------------------------------------- Summary of changes: src/helper/binarybuffer.c | 262 ++++++++++++++++++++++----------------------- src/helper/binarybuffer.h | 116 +++++++++++++------- 2 files changed, 201 insertions(+), 177 deletions(-) hooks/post-receive -- Main OpenOCD repository |