this is testing

Committer:
pmallick
Date:
Thu Jan 14 18:54:16 2021 +0530
Revision:
0:3afcd581558d
this is testing

Who changed what in which revision?

UserRevisionLine numberNew contents of line
pmallick 0:3afcd581558d 1 /***************************************************************************//**
pmallick 0:3afcd581558d 2 * @file util.c
pmallick 0:3afcd581558d 3 * @brief Implementation of utility functions.
pmallick 0:3afcd581558d 4 * @author DBogdan (dragos.bogdan@analog.com)
pmallick 0:3afcd581558d 5 ********************************************************************************
pmallick 0:3afcd581558d 6 * Copyright 2018(c) Analog Devices, Inc.
pmallick 0:3afcd581558d 7 *
pmallick 0:3afcd581558d 8 * All rights reserved.
pmallick 0:3afcd581558d 9 *
pmallick 0:3afcd581558d 10 * Redistribution and use in source and binary forms, with or without
pmallick 0:3afcd581558d 11 * modification, are permitted provided that the following conditions are met:
pmallick 0:3afcd581558d 12 * - Redistributions of source code must retain the above copyright
pmallick 0:3afcd581558d 13 * notice, this list of conditions and the following disclaimer.
pmallick 0:3afcd581558d 14 * - Redistributions in binary form must reproduce the above copyright
pmallick 0:3afcd581558d 15 * notice, this list of conditions and the following disclaimer in
pmallick 0:3afcd581558d 16 * the documentation and/or other materials provided with the
pmallick 0:3afcd581558d 17 * distribution.
pmallick 0:3afcd581558d 18 * - Neither the name of Analog Devices, Inc. nor the names of its
pmallick 0:3afcd581558d 19 * contributors may be used to endorse or promote products derived
pmallick 0:3afcd581558d 20 * from this software without specific prior written permission.
pmallick 0:3afcd581558d 21 * - The use of this software may or may not infringe the patent rights
pmallick 0:3afcd581558d 22 * of one or more patent holders. This license does not release you
pmallick 0:3afcd581558d 23 * from the requirement that you obtain separate licenses from these
pmallick 0:3afcd581558d 24 * patent holders to use this software.
pmallick 0:3afcd581558d 25 * - Use of the software either in source or binary form, must be run
pmallick 0:3afcd581558d 26 * on or directly connected to an Analog Devices Inc. component.
pmallick 0:3afcd581558d 27 *
pmallick 0:3afcd581558d 28 * THIS SOFTWARE IS PROVIDED BY ANALOG DEVICES "AS IS" AND ANY EXPRESS OR
pmallick 0:3afcd581558d 29 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, NON-INFRINGEMENT,
pmallick 0:3afcd581558d 30 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
pmallick 0:3afcd581558d 31 * IN NO EVENT SHALL ANALOG DEVICES BE LIABLE FOR ANY DIRECT, INDIRECT,
pmallick 0:3afcd581558d 32 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
pmallick 0:3afcd581558d 33 * LIMITED TO, INTELLECTUAL PROPERTY RIGHTS, PROCUREMENT OF SUBSTITUTE GOODS OR
pmallick 0:3afcd581558d 34 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
pmallick 0:3afcd581558d 35 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
pmallick 0:3afcd581558d 36 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
pmallick 0:3afcd581558d 37 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
pmallick 0:3afcd581558d 38 *******************************************************************************/
pmallick 0:3afcd581558d 39
pmallick 0:3afcd581558d 40 /******************************************************************************/
pmallick 0:3afcd581558d 41 /***************************** Include Files **********************************/
pmallick 0:3afcd581558d 42 /******************************************************************************/
pmallick 0:3afcd581558d 43 #include <string.h>
pmallick 0:3afcd581558d 44 #include <stdlib.h>
pmallick 0:3afcd581558d 45 #include "util.h"
pmallick 0:3afcd581558d 46 #include "errno.h"
pmallick 0:3afcd581558d 47 /******************************************************************************/
pmallick 0:3afcd581558d 48 /************************** Functions Implementation **************************/
pmallick 0:3afcd581558d 49 /******************************************************************************/
pmallick 0:3afcd581558d 50
pmallick 0:3afcd581558d 51 /**
pmallick 0:3afcd581558d 52 * Find first set bit in word.
pmallick 0:3afcd581558d 53 */
pmallick 0:3afcd581558d 54 uint32_t find_first_set_bit(uint32_t word)
pmallick 0:3afcd581558d 55 {
pmallick 0:3afcd581558d 56 uint32_t first_set_bit = 0;
pmallick 0:3afcd581558d 57
pmallick 0:3afcd581558d 58 while (word) {
pmallick 0:3afcd581558d 59 if (word & 0x1)
pmallick 0:3afcd581558d 60 return first_set_bit;
pmallick 0:3afcd581558d 61 word >>= 1;
pmallick 0:3afcd581558d 62 first_set_bit ++;
pmallick 0:3afcd581558d 63 }
pmallick 0:3afcd581558d 64
pmallick 0:3afcd581558d 65 return 32;
pmallick 0:3afcd581558d 66 }
pmallick 0:3afcd581558d 67
pmallick 0:3afcd581558d 68 /**
pmallick 0:3afcd581558d 69 * Find last set bit in word.
pmallick 0:3afcd581558d 70 */
pmallick 0:3afcd581558d 71 uint32_t find_last_set_bit(uint32_t word)
pmallick 0:3afcd581558d 72 {
pmallick 0:3afcd581558d 73 uint32_t bit = 0;
pmallick 0:3afcd581558d 74 uint32_t last_set_bit = 32;
pmallick 0:3afcd581558d 75
pmallick 0:3afcd581558d 76 while (word) {
pmallick 0:3afcd581558d 77 if (word & 0x1)
pmallick 0:3afcd581558d 78 last_set_bit = bit;
pmallick 0:3afcd581558d 79 word >>= 1;
pmallick 0:3afcd581558d 80 bit ++;
pmallick 0:3afcd581558d 81 }
pmallick 0:3afcd581558d 82
pmallick 0:3afcd581558d 83 return last_set_bit;
pmallick 0:3afcd581558d 84 }
pmallick 0:3afcd581558d 85
pmallick 0:3afcd581558d 86 /**
pmallick 0:3afcd581558d 87 * Locate the closest element in an array.
pmallick 0:3afcd581558d 88 */
pmallick 0:3afcd581558d 89 uint32_t find_closest(int32_t val,
pmallick 0:3afcd581558d 90 const int32_t *array,
pmallick 0:3afcd581558d 91 uint32_t size)
pmallick 0:3afcd581558d 92 {
pmallick 0:3afcd581558d 93 int32_t diff = abs(array[0] - val);
pmallick 0:3afcd581558d 94 uint32_t ret = 0;
pmallick 0:3afcd581558d 95 uint32_t i;
pmallick 0:3afcd581558d 96
pmallick 0:3afcd581558d 97 for (i = 1; i < size; i++) {
pmallick 0:3afcd581558d 98 if (abs(array[i] - val) < diff) {
pmallick 0:3afcd581558d 99 diff = abs(array[i] - val);
pmallick 0:3afcd581558d 100 ret = i;
pmallick 0:3afcd581558d 101 }
pmallick 0:3afcd581558d 102 }
pmallick 0:3afcd581558d 103
pmallick 0:3afcd581558d 104 return ret;
pmallick 0:3afcd581558d 105 }
pmallick 0:3afcd581558d 106
pmallick 0:3afcd581558d 107 /**
pmallick 0:3afcd581558d 108 * Shift the value and apply the specified mask.
pmallick 0:3afcd581558d 109 */
pmallick 0:3afcd581558d 110 uint32_t field_prep(uint32_t mask, uint32_t val)
pmallick 0:3afcd581558d 111 {
pmallick 0:3afcd581558d 112 return (val << find_first_set_bit(mask)) & mask;
pmallick 0:3afcd581558d 113 }
pmallick 0:3afcd581558d 114
pmallick 0:3afcd581558d 115 /**
pmallick 0:3afcd581558d 116 * Get a field specified by a mask from a word.
pmallick 0:3afcd581558d 117 */
pmallick 0:3afcd581558d 118 uint32_t field_get(uint32_t mask, uint32_t word)
pmallick 0:3afcd581558d 119 {
pmallick 0:3afcd581558d 120 return (word & mask) >> find_first_set_bit(mask);
pmallick 0:3afcd581558d 121 }
pmallick 0:3afcd581558d 122
pmallick 0:3afcd581558d 123 /**
pmallick 0:3afcd581558d 124 * Log base 2 of the given number.
pmallick 0:3afcd581558d 125 */
pmallick 0:3afcd581558d 126 int32_t log_base_2(uint32_t x)
pmallick 0:3afcd581558d 127 {
pmallick 0:3afcd581558d 128 return find_last_set_bit(x);
pmallick 0:3afcd581558d 129 }
pmallick 0:3afcd581558d 130
pmallick 0:3afcd581558d 131 /**
pmallick 0:3afcd581558d 132 * Find greatest common divisor of the given two numbers.
pmallick 0:3afcd581558d 133 */
pmallick 0:3afcd581558d 134 uint32_t greatest_common_divisor(uint32_t a,
pmallick 0:3afcd581558d 135 uint32_t b)
pmallick 0:3afcd581558d 136 {
pmallick 0:3afcd581558d 137 uint32_t div;
pmallick 0:3afcd581558d 138 uint32_t common_div = 1;
pmallick 0:3afcd581558d 139
pmallick 0:3afcd581558d 140 if ((a == 0) || (b == 0))
pmallick 0:3afcd581558d 141 return max(a, b);
pmallick 0:3afcd581558d 142
pmallick 0:3afcd581558d 143 for (div = 1; (div <= a) && (div <= b); div++)
pmallick 0:3afcd581558d 144 if (!(a % div) && !(b % div))
pmallick 0:3afcd581558d 145 common_div = div;
pmallick 0:3afcd581558d 146
pmallick 0:3afcd581558d 147 return common_div;
pmallick 0:3afcd581558d 148 }
pmallick 0:3afcd581558d 149
pmallick 0:3afcd581558d 150 /**
pmallick 0:3afcd581558d 151 * Calculate best rational approximation for a given fraction.
pmallick 0:3afcd581558d 152 */
pmallick 0:3afcd581558d 153 void rational_best_approximation(uint32_t given_numerator,
pmallick 0:3afcd581558d 154 uint32_t given_denominator,
pmallick 0:3afcd581558d 155 uint32_t max_numerator,
pmallick 0:3afcd581558d 156 uint32_t max_denominator,
pmallick 0:3afcd581558d 157 uint32_t *best_numerator,
pmallick 0:3afcd581558d 158 uint32_t *best_denominator)
pmallick 0:3afcd581558d 159 {
pmallick 0:3afcd581558d 160 uint32_t gcd;
pmallick 0:3afcd581558d 161
pmallick 0:3afcd581558d 162 gcd = greatest_common_divisor(given_numerator, given_denominator);
pmallick 0:3afcd581558d 163
pmallick 0:3afcd581558d 164 *best_numerator = given_numerator / gcd;
pmallick 0:3afcd581558d 165 *best_denominator = given_denominator / gcd;
pmallick 0:3afcd581558d 166
pmallick 0:3afcd581558d 167 if ((*best_numerator > max_numerator) ||
pmallick 0:3afcd581558d 168 (*best_denominator > max_denominator)) {
pmallick 0:3afcd581558d 169 *best_numerator = 0;
pmallick 0:3afcd581558d 170 *best_denominator = 0;
pmallick 0:3afcd581558d 171 }
pmallick 0:3afcd581558d 172 }
pmallick 0:3afcd581558d 173
pmallick 0:3afcd581558d 174 /**
pmallick 0:3afcd581558d 175 * Calculate the number of set bits.
pmallick 0:3afcd581558d 176 */
pmallick 0:3afcd581558d 177 uint32_t hweight8(uint32_t word)
pmallick 0:3afcd581558d 178 {
pmallick 0:3afcd581558d 179 uint32_t count = 0;
pmallick 0:3afcd581558d 180
pmallick 0:3afcd581558d 181 while (word) {
pmallick 0:3afcd581558d 182 if (word & 0x1)
pmallick 0:3afcd581558d 183 count++;
pmallick 0:3afcd581558d 184 word >>= 1;
pmallick 0:3afcd581558d 185 }
pmallick 0:3afcd581558d 186
pmallick 0:3afcd581558d 187 return count;
pmallick 0:3afcd581558d 188 }
pmallick 0:3afcd581558d 189
pmallick 0:3afcd581558d 190 /**
pmallick 0:3afcd581558d 191 * Calculate the quotient and the remainder of an integer division.
pmallick 0:3afcd581558d 192 */
pmallick 0:3afcd581558d 193 uint64_t do_div(uint64_t* n,
pmallick 0:3afcd581558d 194 uint64_t base)
pmallick 0:3afcd581558d 195 {
pmallick 0:3afcd581558d 196 uint64_t mod = 0;
pmallick 0:3afcd581558d 197
pmallick 0:3afcd581558d 198 mod = *n % base;
pmallick 0:3afcd581558d 199 *n = *n / base;
pmallick 0:3afcd581558d 200
pmallick 0:3afcd581558d 201 return mod;
pmallick 0:3afcd581558d 202 }
pmallick 0:3afcd581558d 203
pmallick 0:3afcd581558d 204 /**
pmallick 0:3afcd581558d 205 * Unsigned 64bit divide with 64bit divisor and remainder
pmallick 0:3afcd581558d 206 */
pmallick 0:3afcd581558d 207 uint64_t div64_u64_rem(uint64_t dividend, uint64_t divisor, uint64_t *remainder)
pmallick 0:3afcd581558d 208 {
pmallick 0:3afcd581558d 209 *remainder = dividend % divisor;
pmallick 0:3afcd581558d 210
pmallick 0:3afcd581558d 211 return dividend / divisor;
pmallick 0:3afcd581558d 212 }
pmallick 0:3afcd581558d 213
pmallick 0:3afcd581558d 214 /**
pmallick 0:3afcd581558d 215 * Unsigned 64bit divide with 32bit divisor with remainder
pmallick 0:3afcd581558d 216 */
pmallick 0:3afcd581558d 217 uint64_t div_u64_rem(uint64_t dividend, uint32_t divisor, uint32_t *remainder)
pmallick 0:3afcd581558d 218 {
pmallick 0:3afcd581558d 219 *remainder = do_div(&dividend, divisor);
pmallick 0:3afcd581558d 220
pmallick 0:3afcd581558d 221 return dividend;
pmallick 0:3afcd581558d 222 }
pmallick 0:3afcd581558d 223
pmallick 0:3afcd581558d 224 /**
pmallick 0:3afcd581558d 225 * Unsigned 64bit divide with 32bit divisor
pmallick 0:3afcd581558d 226 */
pmallick 0:3afcd581558d 227 uint64_t div_u64(uint64_t dividend, uint32_t divisor)
pmallick 0:3afcd581558d 228 {
pmallick 0:3afcd581558d 229 uint32_t remainder;
pmallick 0:3afcd581558d 230
pmallick 0:3afcd581558d 231 return div_u64_rem(dividend, divisor, &remainder);
pmallick 0:3afcd581558d 232 }
pmallick 0:3afcd581558d 233
pmallick 0:3afcd581558d 234 /**
pmallick 0:3afcd581558d 235 * Converts from string to int32_t
pmallick 0:3afcd581558d 236 * @param *str
pmallick 0:3afcd581558d 237 * @return int32_t
pmallick 0:3afcd581558d 238 */
pmallick 0:3afcd581558d 239 int32_t str_to_int32(const char *str)
pmallick 0:3afcd581558d 240 {
pmallick 0:3afcd581558d 241 char *end;
pmallick 0:3afcd581558d 242 int32_t value = strtol(str, &end, 0);
pmallick 0:3afcd581558d 243
pmallick 0:3afcd581558d 244 if (end == str)
pmallick 0:3afcd581558d 245 return -EINVAL;
pmallick 0:3afcd581558d 246 else
pmallick 0:3afcd581558d 247 return value;
pmallick 0:3afcd581558d 248 }
pmallick 0:3afcd581558d 249
pmallick 0:3afcd581558d 250 /**
pmallick 0:3afcd581558d 251 * Converts from string to uint32_t
pmallick 0:3afcd581558d 252 * @param *str
pmallick 0:3afcd581558d 253 * @return uint32_t
pmallick 0:3afcd581558d 254 */
pmallick 0:3afcd581558d 255 uint32_t srt_to_uint32(const char *str)
pmallick 0:3afcd581558d 256 {
pmallick 0:3afcd581558d 257 char *end;
pmallick 0:3afcd581558d 258 uint32_t value = strtoul(str, &end, 0);
pmallick 0:3afcd581558d 259
pmallick 0:3afcd581558d 260 if (end == str)
pmallick 0:3afcd581558d 261 return -EINVAL;
pmallick 0:3afcd581558d 262 else
pmallick 0:3afcd581558d 263 return value;
pmallick 0:3afcd581558d 264 }