1 /* 2 * Copyright 2012-15 Advanced Micro Devices, Inc. 3 * 4 * Permission is hereby granted, free of charge, to any person obtaining a 5 * copy of this software and associated documentation files (the "Software"), 6 * to deal in the Software without restriction, including without limitation 7 * the rights to use, copy, modify, merge, publish, distribute, sublicense, 8 * and/or sell copies of the Software, and to permit persons to whom the 9 * Software is furnished to do so, subject to the following conditions: 10 * 11 * The above copyright notice and this permission notice shall be included in 12 * all copies or substantial portions of the Software. 13 * 14 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 15 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 16 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL 17 * THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR 18 * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, 19 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR 20 * OTHER DEALINGS IN THE SOFTWARE. 21 * 22 * Authors: AMD 23 * 24 */ 25 26 #ifndef __DAL_FIXED31_32_H__ 27 #define __DAL_FIXED31_32_H__ 28 29 #include "os_types.h" 30 31 /* 32 * @brief 33 * Arithmetic operations on real numbers 34 * represented as fixed-point numbers. 35 * There are: 1 bit for sign, 36 * 31 bit for integer part, 37 * 32 bits for fractional part. 38 * 39 * @note 40 * Currently, overflows and underflows are asserted; 41 * no special result returned. 42 */ 43 44 struct fixed31_32 { 45 int64_t value; 46 }; 47 48 /* 49 * @brief 50 * Useful constants 51 */ 52 53 static const struct fixed31_32 dal_fixed31_32_zero = { 0 }; 54 static const struct fixed31_32 dal_fixed31_32_epsilon = { 1LL }; 55 static const struct fixed31_32 dal_fixed31_32_half = { 0x80000000LL }; 56 static const struct fixed31_32 dal_fixed31_32_one = { 0x100000000LL }; 57 58 static const struct fixed31_32 dal_fixed31_32_pi = { 13493037705LL }; 59 static const struct fixed31_32 dal_fixed31_32_two_pi = { 26986075409LL }; 60 static const struct fixed31_32 dal_fixed31_32_e = { 11674931555LL }; 61 static const struct fixed31_32 dal_fixed31_32_ln2 = { 2977044471LL }; 62 static const struct fixed31_32 dal_fixed31_32_ln2_div_2 = { 1488522236LL }; 63 64 /* 65 * @brief 66 * Initialization routines 67 */ 68 69 /* 70 * @brief 71 * result = numerator / denominator 72 */ 73 struct fixed31_32 dal_fixed31_32_from_fraction( 74 int64_t numerator, 75 int64_t denominator); 76 77 /* 78 * @brief 79 * result = arg 80 */ 81 struct fixed31_32 dal_fixed31_32_from_int( 82 int64_t arg); 83 84 /* 85 * @brief 86 * Unary operators 87 */ 88 89 /* 90 * @brief 91 * result = -arg 92 */ 93 struct fixed31_32 dal_fixed31_32_neg( 94 struct fixed31_32 arg); 95 96 /* 97 * @brief 98 * result = abs(arg) := (arg >= 0) ? arg : -arg 99 */ 100 struct fixed31_32 dal_fixed31_32_abs( 101 struct fixed31_32 arg); 102 103 /* 104 * @brief 105 * Binary relational operators 106 */ 107 108 /* 109 * @brief 110 * result = arg1 < arg2 111 */ 112 bool dal_fixed31_32_lt( 113 struct fixed31_32 arg1, 114 struct fixed31_32 arg2); 115 116 /* 117 * @brief 118 * result = arg1 <= arg2 119 */ 120 bool dal_fixed31_32_le( 121 struct fixed31_32 arg1, 122 struct fixed31_32 arg2); 123 124 /* 125 * @brief 126 * result = arg1 == arg2 127 */ 128 bool dal_fixed31_32_eq( 129 struct fixed31_32 arg1, 130 struct fixed31_32 arg2); 131 132 /* 133 * @brief 134 * result = min(arg1, arg2) := (arg1 <= arg2) ? arg1 : arg2 135 */ 136 struct fixed31_32 dal_fixed31_32_min( 137 struct fixed31_32 arg1, 138 struct fixed31_32 arg2); 139 140 /* 141 * @brief 142 * result = max(arg1, arg2) := (arg1 <= arg2) ? arg2 : arg1 143 */ 144 struct fixed31_32 dal_fixed31_32_max( 145 struct fixed31_32 arg1, 146 struct fixed31_32 arg2); 147 148 /* 149 * @brief 150 * | min_value, when arg <= min_value 151 * result = | arg, when min_value < arg < max_value 152 * | max_value, when arg >= max_value 153 */ 154 struct fixed31_32 dal_fixed31_32_clamp( 155 struct fixed31_32 arg, 156 struct fixed31_32 min_value, 157 struct fixed31_32 max_value); 158 159 /* 160 * @brief 161 * Binary shift operators 162 */ 163 164 /* 165 * @brief 166 * result = arg << shift 167 */ 168 struct fixed31_32 dal_fixed31_32_shl( 169 struct fixed31_32 arg, 170 uint8_t shift); 171 172 /* 173 * @brief 174 * result = arg >> shift 175 */ 176 struct fixed31_32 dal_fixed31_32_shr( 177 struct fixed31_32 arg, 178 uint8_t shift); 179 180 /* 181 * @brief 182 * Binary additive operators 183 */ 184 185 /* 186 * @brief 187 * result = arg1 + arg2 188 */ 189 struct fixed31_32 dal_fixed31_32_add( 190 struct fixed31_32 arg1, 191 struct fixed31_32 arg2); 192 193 /* 194 * @brief 195 * result = arg1 + arg2 196 */ 197 struct fixed31_32 dal_fixed31_32_add_int( 198 struct fixed31_32 arg1, 199 int32_t arg2); 200 201 /* 202 * @brief 203 * result = arg1 - arg2 204 */ 205 struct fixed31_32 dal_fixed31_32_sub_int( 206 struct fixed31_32 arg1, 207 int32_t arg2); 208 209 /* 210 * @brief 211 * result = arg1 - arg2 212 */ 213 struct fixed31_32 dal_fixed31_32_sub( 214 struct fixed31_32 arg1, 215 struct fixed31_32 arg2); 216 217 /* 218 * @brief 219 * Binary multiplicative operators 220 */ 221 222 /* 223 * @brief 224 * result = arg1 * arg2 225 */ 226 struct fixed31_32 dal_fixed31_32_mul_int( 227 struct fixed31_32 arg1, 228 int32_t arg2); 229 230 /* 231 * @brief 232 * result = arg1 * arg2 233 */ 234 struct fixed31_32 dal_fixed31_32_mul( 235 struct fixed31_32 arg1, 236 struct fixed31_32 arg2); 237 238 /* 239 * @brief 240 * result = square(arg) := arg * arg 241 */ 242 struct fixed31_32 dal_fixed31_32_sqr( 243 struct fixed31_32 arg); 244 245 /* 246 * @brief 247 * result = arg1 / arg2 248 */ 249 struct fixed31_32 dal_fixed31_32_div_int( 250 struct fixed31_32 arg1, 251 int64_t arg2); 252 253 /* 254 * @brief 255 * result = arg1 / arg2 256 */ 257 struct fixed31_32 dal_fixed31_32_div( 258 struct fixed31_32 arg1, 259 struct fixed31_32 arg2); 260 261 /* 262 * @brief 263 * Reciprocal function 264 */ 265 266 /* 267 * @brief 268 * result = reciprocal(arg) := 1 / arg 269 * 270 * @note 271 * No special actions taken in case argument is zero. 272 */ 273 struct fixed31_32 dal_fixed31_32_recip( 274 struct fixed31_32 arg); 275 276 /* 277 * @brief 278 * Trigonometric functions 279 */ 280 281 /* 282 * @brief 283 * result = sinc(arg) := sin(arg) / arg 284 * 285 * @note 286 * Argument specified in radians, 287 * internally it's normalized to [-2pi...2pi] range. 288 */ 289 struct fixed31_32 dal_fixed31_32_sinc( 290 struct fixed31_32 arg); 291 292 /* 293 * @brief 294 * result = sin(arg) 295 * 296 * @note 297 * Argument specified in radians, 298 * internally it's normalized to [-2pi...2pi] range. 299 */ 300 struct fixed31_32 dal_fixed31_32_sin( 301 struct fixed31_32 arg); 302 303 /* 304 * @brief 305 * result = cos(arg) 306 * 307 * @note 308 * Argument specified in radians 309 * and should be in [-2pi...2pi] range - 310 * passing arguments outside that range 311 * will cause incorrect result! 312 */ 313 struct fixed31_32 dal_fixed31_32_cos( 314 struct fixed31_32 arg); 315 316 /* 317 * @brief 318 * Transcendent functions 319 */ 320 321 /* 322 * @brief 323 * result = exp(arg) 324 * 325 * @note 326 * Currently, function is verified for abs(arg) <= 1. 327 */ 328 struct fixed31_32 dal_fixed31_32_exp( 329 struct fixed31_32 arg); 330 331 /* 332 * @brief 333 * result = log(arg) 334 * 335 * @note 336 * Currently, abs(arg) should be less than 1. 337 * No normalization is done. 338 * Currently, no special actions taken 339 * in case of invalid argument(s). Take care! 340 */ 341 struct fixed31_32 dal_fixed31_32_log( 342 struct fixed31_32 arg); 343 344 /* 345 * @brief 346 * Power function 347 */ 348 349 /* 350 * @brief 351 * result = pow(arg1, arg2) 352 * 353 * @note 354 * Currently, abs(arg1) should be less than 1. Take care! 355 */ 356 struct fixed31_32 dal_fixed31_32_pow( 357 struct fixed31_32 arg1, 358 struct fixed31_32 arg2); 359 360 /* 361 * @brief 362 * Rounding functions 363 */ 364 365 /* 366 * @brief 367 * result = floor(arg) := greatest integer lower than or equal to arg 368 */ 369 int32_t dal_fixed31_32_floor( 370 struct fixed31_32 arg); 371 372 /* 373 * @brief 374 * result = round(arg) := integer nearest to arg 375 */ 376 int32_t dal_fixed31_32_round( 377 struct fixed31_32 arg); 378 379 /* 380 * @brief 381 * result = ceil(arg) := lowest integer greater than or equal to arg 382 */ 383 int32_t dal_fixed31_32_ceil( 384 struct fixed31_32 arg); 385 386 /* the following two function are used in scaler hw programming to convert fixed 387 * point value to format 2 bits from integer part and 19 bits from fractional 388 * part. The same applies for u0d19, 0 bits from integer part and 19 bits from 389 * fractional 390 */ 391 392 uint32_t dal_fixed31_32_u2d19( 393 struct fixed31_32 arg); 394 395 uint32_t dal_fixed31_32_u0d19( 396 struct fixed31_32 arg); 397 398 #endif 399