Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
util.c
00001 /***************************************************************************//** 00002 * @file util.c 00003 * @brief Implementation of utility functions. 00004 * @author DBogdan (dragos.bogdan@analog.com) 00005 ******************************************************************************** 00006 * Copyright 2018(c) Analog Devices, Inc. 00007 * 00008 * All rights reserved. 00009 * 00010 * Redistribution and use in source and binary forms, with or without 00011 * modification, are permitted provided that the following conditions are met: 00012 * - Redistributions of source code must retain the above copyright 00013 * notice, this list of conditions and the following disclaimer. 00014 * - Redistributions in binary form must reproduce the above copyright 00015 * notice, this list of conditions and the following disclaimer in 00016 * the documentation and/or other materials provided with the 00017 * distribution. 00018 * - Neither the name of Analog Devices, Inc. nor the names of its 00019 * contributors may be used to endorse or promote products derived 00020 * from this software without specific prior written permission. 00021 * - The use of this software may or may not infringe the patent rights 00022 * of one or more patent holders. This license does not release you 00023 * from the requirement that you obtain separate licenses from these 00024 * patent holders to use this software. 00025 * - Use of the software either in source or binary form, must be run 00026 * on or directly connected to an Analog Devices Inc. component. 00027 * 00028 * THIS SOFTWARE IS PROVIDED BY ANALOG DEVICES "AS IS" AND ANY EXPRESS OR 00029 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, NON-INFRINGEMENT, 00030 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 00031 * IN NO EVENT SHALL ANALOG DEVICES BE LIABLE FOR ANY DIRECT, INDIRECT, 00032 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 00033 * LIMITED TO, INTELLECTUAL PROPERTY RIGHTS, PROCUREMENT OF SUBSTITUTE GOODS OR 00034 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER 00035 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, 00036 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 00037 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 00038 *******************************************************************************/ 00039 00040 /******************************************************************************/ 00041 /***************************** Include Files **********************************/ 00042 /******************************************************************************/ 00043 #include <string.h> 00044 #include <stdlib.h> 00045 #include "platform_drivers.h" 00046 #include "errno.h" 00047 /******************************************************************************/ 00048 /************************** Functions Implementation **************************/ 00049 /******************************************************************************/ 00050 00051 /** 00052 * Find first set bit in word. 00053 */ 00054 uint32_t find_first_set_bit(uint32_t word) 00055 { 00056 uint32_t first_set_bit = 0; 00057 00058 while (word) { 00059 if (word & 0x1) 00060 return first_set_bit; 00061 word >>= 1; 00062 first_set_bit ++; 00063 } 00064 00065 return 32; 00066 } 00067 00068 /** 00069 * Find last set bit in word. 00070 */ 00071 uint32_t find_last_set_bit(uint32_t word) 00072 { 00073 uint32_t bit = 0; 00074 uint32_t last_set_bit = 32; 00075 00076 while (word) { 00077 if (word & 0x1) 00078 last_set_bit = bit; 00079 word >>= 1; 00080 bit ++; 00081 } 00082 00083 return last_set_bit; 00084 } 00085 00086 /** 00087 * Locate the closest element in an array. 00088 */ 00089 uint32_t find_closest(int32_t val, 00090 const int32_t *array, 00091 uint32_t size) 00092 { 00093 int32_t diff = abs(array[0] - val); 00094 uint32_t ret = 0; 00095 uint32_t i; 00096 00097 for (i = 1; i < size; i++) { 00098 if (abs(array[i] - val) < diff) { 00099 diff = abs(array[i] - val); 00100 ret = i; 00101 } 00102 } 00103 00104 return ret; 00105 } 00106 00107 /** 00108 * Shift the value and apply the specified mask. 00109 */ 00110 uint32_t field_prep(uint32_t mask, uint32_t val) 00111 { 00112 return (val << find_first_set_bit(mask)) & mask; 00113 } 00114 00115 /** 00116 * Get a field specified by a mask from a word. 00117 */ 00118 uint32_t field_get(uint32_t mask, uint32_t word) 00119 { 00120 return (word & mask) >> find_first_set_bit(mask); 00121 } 00122 00123 /** 00124 * Log base 2 of the given number. 00125 */ 00126 int32_t log_base_2(uint32_t x) 00127 { 00128 return find_last_set_bit(x); 00129 } 00130 00131 /** 00132 * Find greatest common divisor of the given two numbers. 00133 */ 00134 uint32_t greatest_common_divisor(uint32_t a, 00135 uint32_t b) 00136 { 00137 uint32_t div; 00138 uint32_t common_div = 1; 00139 00140 if ((a == 0) || (b == 0)) 00141 return max(a, b); 00142 00143 for (div = 1; (div <= a) && (div <= b); div++) 00144 if (!(a % div) && !(b % div)) 00145 common_div = div; 00146 00147 return common_div; 00148 } 00149 00150 /** 00151 * Calculate best rational approximation for a given fraction. 00152 */ 00153 void rational_best_approximation(uint32_t given_numerator, 00154 uint32_t given_denominator, 00155 uint32_t max_numerator, 00156 uint32_t max_denominator, 00157 uint32_t *best_numerator, 00158 uint32_t *best_denominator) 00159 { 00160 uint32_t gcd; 00161 00162 gcd = greatest_common_divisor(given_numerator, given_denominator); 00163 00164 *best_numerator = given_numerator / gcd; 00165 *best_denominator = given_denominator / gcd; 00166 00167 if ((*best_numerator > max_numerator) || 00168 (*best_denominator > max_denominator)) { 00169 *best_numerator = 0; 00170 *best_denominator = 0; 00171 } 00172 } 00173 00174 /** 00175 * Calculate the number of set bits. 00176 */ 00177 uint32_t hweight8(uint32_t word) 00178 { 00179 uint32_t count = 0; 00180 00181 while (word) { 00182 if (word & 0x1) 00183 count++; 00184 word >>= 1; 00185 } 00186 00187 return count; 00188 } 00189 00190 /** 00191 * Calculate the quotient and the remainder of an integer division. 00192 */ 00193 uint64_t do_div(uint64_t* n, 00194 uint64_t base) 00195 { 00196 uint64_t mod = 0; 00197 00198 mod = *n % base; 00199 *n = *n / base; 00200 00201 return mod; 00202 } 00203 00204 /** 00205 * Unsigned 64bit divide with 64bit divisor and remainder 00206 */ 00207 uint64_t div64_u64_rem(uint64_t dividend, uint64_t divisor, uint64_t *remainder) 00208 { 00209 *remainder = dividend % divisor; 00210 00211 return dividend / divisor; 00212 } 00213 00214 /** 00215 * Unsigned 64bit divide with 32bit divisor with remainder 00216 */ 00217 uint64_t div_u64_rem(uint64_t dividend, uint32_t divisor, uint32_t *remainder) 00218 { 00219 *remainder = do_div(÷nd, divisor); 00220 00221 return dividend; 00222 } 00223 00224 /** 00225 * Unsigned 64bit divide with 32bit divisor 00226 */ 00227 uint64_t div_u64(uint64_t dividend, uint32_t divisor) 00228 { 00229 uint32_t remainder; 00230 00231 return div_u64_rem(dividend, divisor, &remainder); 00232 } 00233 00234 /** 00235 * Converts from string to int32_t 00236 * @param *str 00237 * @return int32_t 00238 */ 00239 int32_t str_to_int32(const char *str) 00240 { 00241 char *end; 00242 int32_t value = strtol(str, &end, 0); 00243 00244 if (end == str) 00245 return -EINVAL; 00246 else 00247 return value; 00248 } 00249 00250 /** 00251 * Converts from string to uint32_t 00252 * @param *str 00253 * @return uint32_t 00254 */ 00255 uint32_t srt_to_uint32(const char *str) 00256 { 00257 char *end; 00258 uint32_t value = strtoul(str, &end, 0); 00259 00260 if (end == str) 00261 return -EINVAL; 00262 else 00263 return value; 00264 }
Generated on Tue Jul 12 2022 17:15:46 by
