AFBR-S50 API Reference Manual  v1.6.5
AFBR-S50 Time-of-Flight Sensor SDK for Embedded Software
fp_rnd.h
Go to the documentation of this file.
1 /*************************************************************************/
37 #ifndef FP_RND_H
38 #define FP_RND_H
39 #ifdef __cplusplus
40 extern "C" {
41 #endif
42 
43 /*!***************************************************************************
44  * @addtogroup argus_fp
45  * @{
46  *****************************************************************************/
47 
48 #include "fp_def.h"
49 #include <assert.h>
50 
51 /*!***************************************************************************
52  * @brief Converting with rounding from UQx.n1 to UQx.n2.
53  * @details Equivalent to dividing by 2^n with correct rounding to unsigned
54  * integer values.
55  * @param Q The number in (U)Qx.n1 fixed point format to be rounded.
56  * @param n The number of bits to be rounded,
57  * e.g. UQ8.8 -> UQ12.4 => n = 8 - 4 = 4.
58  * @return The rounded value in (U)Qx.n2 format.
59  *****************************************************************************/
60 inline uint32_t fp_rndu(uint32_t Q, uint_fast8_t n)
61 {
62  if (n == 0) return Q;
63  else if (n > 32U) return 0;
64 
65  // Shift by n>=32 yields undefined behavior! Thus, this extra first
66  // step is essential to prevent issues.
67  Q >>= n - 1;
68  return (Q >> 1) + (Q & 1U);
69 }
70 
71 /*!***************************************************************************
72  * @brief Converting with rounding from Qx.n1 to Qx.n2.
73  * @details Equivalent to dividing by 2^n with correct rounding to integer
74  * values.
75  * @param Q The number in (U)Qx.n1 fixed point format to be rounded.
76  * @param n The number of bits to be rounded,
77  * e.g. Q7.8 -> Q11.4 => n = 8 - 4 = 4.
78  * @return The rounded value in (U)Qx.n2 format.
79  *****************************************************************************/
80 inline int32_t fp_rnds(int32_t Q, uint_fast8_t n)
81 {
82  return (Q < 0) ? -(int32_t)fp_rndu((uint32_t)(-Q), n) : (int32_t)fp_rndu((uint32_t)Q, n);
83 }
84 
85 /*!***************************************************************************
86  * @brief Converting with truncation from UQx.n1 to UQx.n2.
87  * @details Equivalent to dividing by 2^n with truncating (throw away) the
88  * fractional part, resulting in an unsigned integer/fixed-point value.
89  * @param Q The number in (U)Qx.n1 fixed point format to be truncated.
90  * @param n The number of bits to be truncated,
91  * e.g. UQ8.8 -> UQ12.4 => n = 8 - 4 = 4.
92  * @return The truncated value in (U)Qx.n2 format.
93  *****************************************************************************/
94 inline uint32_t fp_truncu(uint32_t Q, uint_fast8_t n)
95 {
96  return (n < 32U) ? (Q >> n) : 0;
97 }
98 
99 /*!***************************************************************************
100  * @brief Converting with truncation from Qx.n1 to Qx.n2.
101  * @details Equivalent to dividing by 2^n with truncating (throw away) the
102  * fractional part, resulting in a signed integer/fixed-point value.
103  * @param Q The number in (U)Qx.n1 fixed point format to be truncated.
104  * @param n The number of bits to be truncated,
105  * e.g. Q7.8 -> Q11.4 => n = 8 - 4 = 4.
106  * @return The truncated value in (U)Qx.n2 format.
107  *****************************************************************************/
108 inline int32_t fp_truncs(int32_t Q, uint_fast8_t n)
109 {
110  return (Q < 0) ? -(int32_t)fp_truncu((uint32_t)(-Q), n) : (int32_t)fp_truncu((uint32_t)Q, n);
111 }
112 
114 #ifdef __cplusplus
115 } // extern "C"
116 #endif
117 #endif /* FP_RND_H */
fp_def.h
This file is part of the AFBR-S50 API.
fp_rndu
uint32_t fp_rndu(uint32_t Q, uint_fast8_t n)
Converting with rounding from UQx.n1 to UQx.n2.
Definition: fp_rnd.h:60
fp_rnds
int32_t fp_rnds(int32_t Q, uint_fast8_t n)
Converting with rounding from Qx.n1 to Qx.n2.
Definition: fp_rnd.h:80
fp_truncu
uint32_t fp_truncu(uint32_t Q, uint_fast8_t n)
Converting with truncation from UQx.n1 to UQx.n2.
Definition: fp_rnd.h:94
fp_truncs
int32_t fp_truncs(int32_t Q, uint_fast8_t n)
Converting with truncation from Qx.n1 to Qx.n2.
Definition: fp_rnd.h:108