Pratyush Mallick
/
nano_dac
this is testing
noos_mbed/include/util.h@0:e8a1ba50c46b, 2021-01-14 (annotated)
- Committer:
- pmallick
- Date:
- Thu Jan 14 19:12:57 2021 +0530
- Revision:
- 0:e8a1ba50c46b
this is testing
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
pmallick | 0:e8a1ba50c46b | 1 | /***************************************************************************//** |
pmallick | 0:e8a1ba50c46b | 2 | * @file util.h |
pmallick | 0:e8a1ba50c46b | 3 | * @brief Implementation of utility functions. |
pmallick | 0:e8a1ba50c46b | 4 | * @author DBogdan (dragos.bogdan@analog.com) |
pmallick | 0:e8a1ba50c46b | 5 | ******************************************************************************** |
pmallick | 0:e8a1ba50c46b | 6 | * Copyright 2018(c) Analog Devices, Inc. |
pmallick | 0:e8a1ba50c46b | 7 | * |
pmallick | 0:e8a1ba50c46b | 8 | * All rights reserved. |
pmallick | 0:e8a1ba50c46b | 9 | * |
pmallick | 0:e8a1ba50c46b | 10 | * Redistribution and use in source and binary forms, with or without |
pmallick | 0:e8a1ba50c46b | 11 | * modification, are permitted provided that the following conditions are met: |
pmallick | 0:e8a1ba50c46b | 12 | * - Redistributions of source code must retain the above copyright |
pmallick | 0:e8a1ba50c46b | 13 | * notice, this list of conditions and the following disclaimer. |
pmallick | 0:e8a1ba50c46b | 14 | * - Redistributions in binary form must reproduce the above copyright |
pmallick | 0:e8a1ba50c46b | 15 | * notice, this list of conditions and the following disclaimer in |
pmallick | 0:e8a1ba50c46b | 16 | * the documentation and/or other materials provided with the |
pmallick | 0:e8a1ba50c46b | 17 | * distribution. |
pmallick | 0:e8a1ba50c46b | 18 | * - Neither the name of Analog Devices, Inc. nor the names of its |
pmallick | 0:e8a1ba50c46b | 19 | * contributors may be used to endorse or promote products derived |
pmallick | 0:e8a1ba50c46b | 20 | * from this software without specific prior written permission. |
pmallick | 0:e8a1ba50c46b | 21 | * - The use of this software may or may not infringe the patent rights |
pmallick | 0:e8a1ba50c46b | 22 | * of one or more patent holders. This license does not release you |
pmallick | 0:e8a1ba50c46b | 23 | * from the requirement that you obtain separate licenses from these |
pmallick | 0:e8a1ba50c46b | 24 | * patent holders to use this software. |
pmallick | 0:e8a1ba50c46b | 25 | * - Use of the software either in source or binary form, must be run |
pmallick | 0:e8a1ba50c46b | 26 | * on or directly connected to an Analog Devices Inc. component. |
pmallick | 0:e8a1ba50c46b | 27 | * |
pmallick | 0:e8a1ba50c46b | 28 | * THIS SOFTWARE IS PROVIDED BY ANALOG DEVICES "AS IS" AND ANY EXPRESS OR |
pmallick | 0:e8a1ba50c46b | 29 | * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, NON-INFRINGEMENT, |
pmallick | 0:e8a1ba50c46b | 30 | * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. |
pmallick | 0:e8a1ba50c46b | 31 | * IN NO EVENT SHALL ANALOG DEVICES BE LIABLE FOR ANY DIRECT, INDIRECT, |
pmallick | 0:e8a1ba50c46b | 32 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
pmallick | 0:e8a1ba50c46b | 33 | * LIMITED TO, INTELLECTUAL PROPERTY RIGHTS, PROCUREMENT OF SUBSTITUTE GOODS OR |
pmallick | 0:e8a1ba50c46b | 34 | * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER |
pmallick | 0:e8a1ba50c46b | 35 | * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, |
pmallick | 0:e8a1ba50c46b | 36 | * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
pmallick | 0:e8a1ba50c46b | 37 | * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
pmallick | 0:e8a1ba50c46b | 38 | *******************************************************************************/ |
pmallick | 0:e8a1ba50c46b | 39 | #ifndef UTIL_H_ |
pmallick | 0:e8a1ba50c46b | 40 | #define UTIL_H_ |
pmallick | 0:e8a1ba50c46b | 41 | |
pmallick | 0:e8a1ba50c46b | 42 | /******************************************************************************/ |
pmallick | 0:e8a1ba50c46b | 43 | /***************************** Include Files **********************************/ |
pmallick | 0:e8a1ba50c46b | 44 | /******************************************************************************/ |
pmallick | 0:e8a1ba50c46b | 45 | #include <stdint.h> |
pmallick | 0:e8a1ba50c46b | 46 | #include <stdbool.h> |
pmallick | 0:e8a1ba50c46b | 47 | /******************************************************************************/ |
pmallick | 0:e8a1ba50c46b | 48 | /********************** Macros and Constants Definitions **********************/ |
pmallick | 0:e8a1ba50c46b | 49 | /******************************************************************************/ |
pmallick | 0:e8a1ba50c46b | 50 | #define BIT(x) (1 << (x)) |
pmallick | 0:e8a1ba50c46b | 51 | |
pmallick | 0:e8a1ba50c46b | 52 | #define ARRAY_SIZE(x) \ |
pmallick | 0:e8a1ba50c46b | 53 | (sizeof(x) / sizeof((x)[0])) |
pmallick | 0:e8a1ba50c46b | 54 | |
pmallick | 0:e8a1ba50c46b | 55 | #define DIV_ROUND_UP(x,y) \ |
pmallick | 0:e8a1ba50c46b | 56 | (((x) + (y) - 1) / (y)) |
pmallick | 0:e8a1ba50c46b | 57 | #define DIV_ROUND_CLOSEST(x, y) \ |
pmallick | 0:e8a1ba50c46b | 58 | (((x) + (y) / 2) / (y)) |
pmallick | 0:e8a1ba50c46b | 59 | #define DIV_ROUND_CLOSEST_ULL(x, y) \ |
pmallick | 0:e8a1ba50c46b | 60 | DIV_ROUND_CLOSEST(x, y) |
pmallick | 0:e8a1ba50c46b | 61 | |
pmallick | 0:e8a1ba50c46b | 62 | #define min(x, y) \ |
pmallick | 0:e8a1ba50c46b | 63 | (((x) < (y)) ? (x) : (y)) |
pmallick | 0:e8a1ba50c46b | 64 | #define min_t(type, x, y) \ |
pmallick | 0:e8a1ba50c46b | 65 | (type)min((type)(x), (type)(y)) |
pmallick | 0:e8a1ba50c46b | 66 | |
pmallick | 0:e8a1ba50c46b | 67 | #define max(x, y) \ |
pmallick | 0:e8a1ba50c46b | 68 | (((x) > (y)) ? (x) : (y)) |
pmallick | 0:e8a1ba50c46b | 69 | #define max_t(type, x, y) \ |
pmallick | 0:e8a1ba50c46b | 70 | (type)max((type)(x), (type)(y)) |
pmallick | 0:e8a1ba50c46b | 71 | |
pmallick | 0:e8a1ba50c46b | 72 | #define clamp(val, min_val, max_val) \ |
pmallick | 0:e8a1ba50c46b | 73 | (max(min((val), (max_val)), (min_val))) |
pmallick | 0:e8a1ba50c46b | 74 | #define clamp_t(type, val, min_val, max_val) \ |
pmallick | 0:e8a1ba50c46b | 75 | (type)clamp((type)(val), (type)(min_val), (type)(max_val)) |
pmallick | 0:e8a1ba50c46b | 76 | |
pmallick | 0:e8a1ba50c46b | 77 | #define abs(x) \ |
pmallick | 0:e8a1ba50c46b | 78 | ((x) < 0 ? (-(x)) : (x)) |
pmallick | 0:e8a1ba50c46b | 79 | |
pmallick | 0:e8a1ba50c46b | 80 | #define swap(x, y) \ |
pmallick | 0:e8a1ba50c46b | 81 | {typeof(x) _tmp_ = (x); (x) = (y); (y) = _tmp_;} |
pmallick | 0:e8a1ba50c46b | 82 | |
pmallick | 0:e8a1ba50c46b | 83 | #define round_up(x,y) \ |
pmallick | 0:e8a1ba50c46b | 84 | (((x)+(y)-1)/(y)) |
pmallick | 0:e8a1ba50c46b | 85 | |
pmallick | 0:e8a1ba50c46b | 86 | #define BITS_PER_LONG 32 |
pmallick | 0:e8a1ba50c46b | 87 | |
pmallick | 0:e8a1ba50c46b | 88 | #define GENMASK(h, l) ({ \ |
pmallick | 0:e8a1ba50c46b | 89 | uint32_t t = (uint32_t)(~0UL); \ |
pmallick | 0:e8a1ba50c46b | 90 | t = t << (BITS_PER_LONG - (h - l + 1)); \ |
pmallick | 0:e8a1ba50c46b | 91 | t = t >> (BITS_PER_LONG - (h + 1)); \ |
pmallick | 0:e8a1ba50c46b | 92 | t; \ |
pmallick | 0:e8a1ba50c46b | 93 | }) |
pmallick | 0:e8a1ba50c46b | 94 | |
pmallick | 0:e8a1ba50c46b | 95 | #define bswap_constant_32(x) \ |
pmallick | 0:e8a1ba50c46b | 96 | ((((x) & 0xff000000) >> 24) | (((x) & 0x00ff0000) >> 8) | \ |
pmallick | 0:e8a1ba50c46b | 97 | (((x) & 0x0000ff00) << 8) | (((x) & 0x000000ff) << 24)) |
pmallick | 0:e8a1ba50c46b | 98 | |
pmallick | 0:e8a1ba50c46b | 99 | #define U16_MAX ((uint16_t)~0U) |
pmallick | 0:e8a1ba50c46b | 100 | #define S16_MAX ((int16_t)(U16_MAX>>1)) |
pmallick | 0:e8a1ba50c46b | 101 | |
pmallick | 0:e8a1ba50c46b | 102 | #define DIV_U64(x, y) (x / y) |
pmallick | 0:e8a1ba50c46b | 103 | |
pmallick | 0:e8a1ba50c46b | 104 | #define UNUSED_PARAM(x) ((void)x) |
pmallick | 0:e8a1ba50c46b | 105 | |
pmallick | 0:e8a1ba50c46b | 106 | /******************************************************************************/ |
pmallick | 0:e8a1ba50c46b | 107 | /************************ Functions Declarations ******************************/ |
pmallick | 0:e8a1ba50c46b | 108 | /******************************************************************************/ |
pmallick | 0:e8a1ba50c46b | 109 | /* Find first set bit in word. */ |
pmallick | 0:e8a1ba50c46b | 110 | uint32_t find_first_set_bit(uint32_t word); |
pmallick | 0:e8a1ba50c46b | 111 | /* Find last set bit in word. */ |
pmallick | 0:e8a1ba50c46b | 112 | uint32_t find_last_set_bit(uint32_t word); |
pmallick | 0:e8a1ba50c46b | 113 | /* Locate the closest element in an array. */ |
pmallick | 0:e8a1ba50c46b | 114 | uint32_t find_closest(int32_t val, |
pmallick | 0:e8a1ba50c46b | 115 | const int32_t *array, |
pmallick | 0:e8a1ba50c46b | 116 | uint32_t size); |
pmallick | 0:e8a1ba50c46b | 117 | /* Shift the value and apply the specified mask. */ |
pmallick | 0:e8a1ba50c46b | 118 | uint32_t field_prep(uint32_t mask, uint32_t val); |
pmallick | 0:e8a1ba50c46b | 119 | /* Get a field specified by a mask from a word. */ |
pmallick | 0:e8a1ba50c46b | 120 | uint32_t field_get(uint32_t mask, uint32_t word); |
pmallick | 0:e8a1ba50c46b | 121 | /* Log base 2 of the given number. */ |
pmallick | 0:e8a1ba50c46b | 122 | int32_t log_base_2(uint32_t x); |
pmallick | 0:e8a1ba50c46b | 123 | /* Find greatest common divisor of the given two numbers. */ |
pmallick | 0:e8a1ba50c46b | 124 | uint32_t greatest_common_divisor(uint32_t a, |
pmallick | 0:e8a1ba50c46b | 125 | uint32_t b); |
pmallick | 0:e8a1ba50c46b | 126 | /* Calculate best rational approximation for a given fraction. */ |
pmallick | 0:e8a1ba50c46b | 127 | void rational_best_approximation(uint32_t given_numerator, |
pmallick | 0:e8a1ba50c46b | 128 | uint32_t given_denominator, |
pmallick | 0:e8a1ba50c46b | 129 | uint32_t max_numerator, |
pmallick | 0:e8a1ba50c46b | 130 | uint32_t max_denominator, |
pmallick | 0:e8a1ba50c46b | 131 | uint32_t *best_numerator, |
pmallick | 0:e8a1ba50c46b | 132 | uint32_t *best_denominator); |
pmallick | 0:e8a1ba50c46b | 133 | /* Calculate the number of set bits. */ |
pmallick | 0:e8a1ba50c46b | 134 | uint32_t hweight8(uint32_t word); |
pmallick | 0:e8a1ba50c46b | 135 | /* Calculate the quotient and the remainder of an integer division. */ |
pmallick | 0:e8a1ba50c46b | 136 | uint64_t do_div(uint64_t* n, |
pmallick | 0:e8a1ba50c46b | 137 | uint64_t base); |
pmallick | 0:e8a1ba50c46b | 138 | /* Unsigned 64bit divide with 64bit divisor and remainder */ |
pmallick | 0:e8a1ba50c46b | 139 | uint64_t div64_u64_rem(uint64_t dividend, uint64_t divisor, |
pmallick | 0:e8a1ba50c46b | 140 | uint64_t *remainder); |
pmallick | 0:e8a1ba50c46b | 141 | /* Unsigned 64bit divide with 32bit divisor with remainder */ |
pmallick | 0:e8a1ba50c46b | 142 | uint64_t div_u64_rem(uint64_t dividend, uint32_t divisor, uint32_t *remainder); |
pmallick | 0:e8a1ba50c46b | 143 | /* Unsigned 64bit divide with 32bit divisor */ |
pmallick | 0:e8a1ba50c46b | 144 | uint64_t div_u64(uint64_t dividend, uint32_t divisor); |
pmallick | 0:e8a1ba50c46b | 145 | /* Converts from string to int32_t */ |
pmallick | 0:e8a1ba50c46b | 146 | int32_t str_to_int32(const char *str); |
pmallick | 0:e8a1ba50c46b | 147 | /* Converts from string to uint32_t */ |
pmallick | 0:e8a1ba50c46b | 148 | uint32_t srt_to_uint32(const char *str); |
pmallick | 0:e8a1ba50c46b | 149 | #endif // UTIL_H_ |
pmallick | 0:e8a1ba50c46b | 150 |