Enda Kilgarriff / platform_drivers
Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers util.h Source File

util.h

Go to the documentation of this file.
00001 /***************************************************************************//**
00002  *   @file   util.h
00003  *   @brief  Implementation of utility functions.
00004  *   @author DBogdan (dragos.bogdan@analog.com)
00005 ********************************************************************************
00006  * Copyright 2018, 2020(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 #ifndef UTIL_H_
00040 #define UTIL_H_
00041 
00042 /******************************************************************************/
00043 /***************************** Include Files **********************************/
00044 /******************************************************************************/
00045 #include <stdint.h>
00046 #include <stdbool.h>
00047 /******************************************************************************/
00048 /********************** Macros and Constants Definitions **********************/
00049 /******************************************************************************/
00050 #define BIT(x)  (1 << (x))
00051 
00052 #define ARRAY_SIZE(x) \
00053     (sizeof(x) / sizeof((x)[0]))
00054 
00055 #define DIV_ROUND_UP(x,y) \
00056     (((x) + (y) - 1) / (y))
00057 #define DIV_ROUND_CLOSEST(x, y) \
00058     (((x) + (y) / 2) / (y))
00059 #define DIV_ROUND_CLOSEST_ULL(x, y) \
00060     DIV_ROUND_CLOSEST(x, y)
00061 
00062 #define min(x, y) \
00063     (((x) < (y)) ? (x) : (y))
00064 #define min_t(type, x, y) \
00065     (type)min((type)(x), (type)(y))
00066 
00067 #define max(x, y) \
00068     (((x) > (y)) ? (x) : (y))
00069 #define max_t(type, x, y) \
00070     (type)max((type)(x), (type)(y))
00071 
00072 #define clamp(val, min_val, max_val) \
00073     (max(min((val), (max_val)), (min_val)))
00074 #define clamp_t(type, val, min_val, max_val) \
00075     (type)clamp((type)(val), (type)(min_val), (type)(max_val))
00076 
00077 #define abs(x) \
00078     ((x) < 0 ? (-(x)) : (x))
00079 
00080 #define swap(x, y) \
00081     {typeof(x) _tmp_ = (x); (x) = (y); (y) = _tmp_;}
00082 
00083 #define round_up(x,y) \
00084         (((x)+(y)-1)/(y))
00085 
00086 #define BITS_PER_LONG 32
00087 
00088 #define GENMASK(h, l) ({                                        \
00089         uint32_t t = (~0UL);                                    \
00090         t = t << (BITS_PER_LONG - (h - l + 1));                 \
00091         t = t >> (BITS_PER_LONG - (h + 1));                     \
00092         t;                                                      \
00093 })
00094 
00095 #define bswap_constant_32(x) \
00096     ((((x) & 0xff000000) >> 24) | (((x) & 0x00ff0000) >>  8) | \
00097      (((x) & 0x0000ff00) <<  8) | (((x) & 0x000000ff) << 24))
00098 
00099 #define U16_MAX     ((uint16_t)~0U)
00100 #define S16_MAX     ((int16_t)(U16_MAX>>1))
00101 
00102 #define DIV_U64(x, y) (x / y)
00103 
00104 #define UNUSED_PARAM(x) ((void)x)
00105 
00106 /******************************************************************************/
00107 /************************ Functions Declarations ******************************/
00108 /******************************************************************************/
00109 /* Find first set bit in word. */
00110 uint32_t find_first_set_bit(uint32_t word);
00111 /* Find last set bit in word. */
00112 uint32_t find_last_set_bit(uint32_t word);
00113 /* Locate the closest element in an array. */
00114 uint32_t find_closest(int32_t val,
00115               const int32_t *array,
00116               uint32_t size);
00117 /* Shift the value and apply the specified mask. */
00118 uint32_t field_prep(uint32_t mask, uint32_t val);
00119 /* Get a field specified by a mask from a word. */
00120 uint32_t field_get(uint32_t mask, uint32_t word);
00121 /* Log base 2 of the given number. */
00122 int32_t log_base_2(uint32_t x);
00123 /* Find greatest common divisor of the given two numbers. */
00124 uint32_t greatest_common_divisor(uint32_t a,
00125                  uint32_t b);
00126 /* Calculate best rational approximation for a given fraction. */
00127 void rational_best_approximation(uint32_t given_numerator,
00128                  uint32_t given_denominator,
00129                  uint32_t max_numerator,
00130                  uint32_t max_denominator,
00131                  uint32_t *best_numerator,
00132                  uint32_t *best_denominator);
00133 /* Calculate the number of set bits. */
00134 uint32_t hweight8(uint32_t word);
00135 /* Calculate the quotient and the remainder of an integer division. */
00136 uint64_t do_div(uint64_t* n,
00137         uint64_t base);
00138 /* Unsigned 64bit divide with 64bit divisor and remainder */
00139 uint64_t div64_u64_rem(uint64_t dividend, uint64_t divisor,
00140                uint64_t *remainder);
00141 /* Unsigned 64bit divide with 32bit divisor with remainder */
00142 uint64_t div_u64_rem(uint64_t dividend, uint32_t divisor, uint32_t *remainder);
00143 /* Unsigned 64bit divide with 32bit divisor */
00144 uint64_t div_u64(uint64_t dividend, uint32_t divisor);
00145 /* Converts from string to int32_t */
00146 int32_t str_to_int32(const char *str);
00147 /* Converts from string to uint32_t */
00148 uint32_t srt_to_uint32(const char *str);
00149 #endif // UTIL_H_
00150