brintos

brintos / linux-shallow public Read only

0
0
Text · 10.8 KiB · 8a045e2 Raw
526 lines · c
1/* SPDX-License-Identifier: MIT */2 3/* Copyright 2024 Advanced Micro Devices, Inc. */4 5#ifndef __SPL_FIXED31_32_H__6#define __SPL_FIXED31_32_H__7 8#include "os_types.h"9#include "spl_os_types.h"   // swap10#ifndef ASSERT11#define ASSERT(_bool) ((void *)0)12#endif13 14#ifndef LLONG_MAX15#define LLONG_MAX 9223372036854775807ll16#endif17#ifndef LLONG_MIN18#define LLONG_MIN (-LLONG_MAX - 1ll)19#endif20 21#define FIXED31_32_BITS_PER_FRACTIONAL_PART 3222#ifndef LLONG_MIN23#define LLONG_MIN (1LL<<63)24#endif25#ifndef LLONG_MAX26#define LLONG_MAX (-1LL>>1)27#endif28 29/*30 * @brief31 * Arithmetic operations on real numbers32 * represented as fixed-point numbers.33 * There are: 1 bit for sign,34 * 31 bit for integer part,35 * 32 bits for fractional part.36 *37 * @note38 * Currently, overflows and underflows are asserted;39 * no special result returned.40 */41 42struct spl_fixed31_32 {43	long long value;44};45 46 47/*48 * @brief49 * Useful constants50 */51 52static const struct spl_fixed31_32 spl_fixpt_zero = { 0 };53static const struct spl_fixed31_32 spl_fixpt_epsilon = { 1LL };54static const struct spl_fixed31_32 spl_fixpt_half = { 0x80000000LL };55static const struct spl_fixed31_32 spl_fixpt_one = { 0x100000000LL };56 57/*58 * @brief59 * Initialization routines60 */61 62/*63 * @brief64 * result = numerator / denominator65 */66struct spl_fixed31_32 spl_fixpt_from_fraction(long long numerator, long long denominator);67 68/*69 * @brief70 * result = arg71 */72static inline struct spl_fixed31_32 spl_fixpt_from_int(int arg)73{74	struct spl_fixed31_32 res;75 76	res.value = (long long) arg << FIXED31_32_BITS_PER_FRACTIONAL_PART;77 78	return res;79}80 81/*82 * @brief83 * Unary operators84 */85 86/*87 * @brief88 * result = -arg89 */90static inline struct spl_fixed31_32 spl_fixpt_neg(struct spl_fixed31_32 arg)91{92	struct spl_fixed31_32 res;93 94	res.value = -arg.value;95 96	return res;97}98 99/*100 * @brief101 * result = abs(arg) := (arg >= 0) ? arg : -arg102 */103static inline struct spl_fixed31_32 spl_fixpt_abs(struct spl_fixed31_32 arg)104{105	if (arg.value < 0)106		return spl_fixpt_neg(arg);107	else108		return arg;109}110 111/*112 * @brief113 * Binary relational operators114 */115 116/*117 * @brief118 * result = arg1 < arg2119 */120static inline bool spl_fixpt_lt(struct spl_fixed31_32 arg1, struct spl_fixed31_32 arg2)121{122	return arg1.value < arg2.value;123}124 125/*126 * @brief127 * result = arg1 <= arg2128 */129static inline bool spl_fixpt_le(struct spl_fixed31_32 arg1, struct spl_fixed31_32 arg2)130{131	return arg1.value <= arg2.value;132}133 134/*135 * @brief136 * result = arg1 == arg2137 */138static inline bool spl_fixpt_eq(struct spl_fixed31_32 arg1, struct spl_fixed31_32 arg2)139{140	return arg1.value == arg2.value;141}142 143/*144 * @brief145 * result = min(arg1, arg2) := (arg1 <= arg2) ? arg1 : arg2146 */147static inline struct spl_fixed31_32 spl_fixpt_min(struct spl_fixed31_32 arg1, struct spl_fixed31_32 arg2)148{149	if (arg1.value <= arg2.value)150		return arg1;151	else152		return arg2;153}154 155/*156 * @brief157 * result = max(arg1, arg2) := (arg1 <= arg2) ? arg2 : arg1158 */159static inline struct spl_fixed31_32 spl_fixpt_max(struct spl_fixed31_32 arg1, struct spl_fixed31_32 arg2)160{161	if (arg1.value <= arg2.value)162		return arg2;163	else164		return arg1;165}166 167/*168 * @brief169 *          | min_value, when arg <= min_value170 * result = | arg, when min_value < arg < max_value171 *          | max_value, when arg >= max_value172 */173static inline struct spl_fixed31_32 spl_fixpt_clamp(174	struct spl_fixed31_32 arg,175	struct spl_fixed31_32 min_value,176	struct spl_fixed31_32 max_value)177{178	if (spl_fixpt_le(arg, min_value))179		return min_value;180	else if (spl_fixpt_le(max_value, arg))181		return max_value;182	else183		return arg;184}185 186/*187 * @brief188 * Binary shift operators189 */190 191/*192 * @brief193 * result = arg << shift194 */195static inline struct spl_fixed31_32 spl_fixpt_shl(struct spl_fixed31_32 arg, unsigned char shift)196{197	ASSERT(((arg.value >= 0) && (arg.value <= LLONG_MAX >> shift)) ||198		((arg.value < 0) && (arg.value >= ~(LLONG_MAX >> shift))));199 200	arg.value = arg.value << shift;201 202	return arg;203}204 205/*206 * @brief207 * result = arg >> shift208 */209static inline struct spl_fixed31_32 spl_fixpt_shr(struct spl_fixed31_32 arg, unsigned char shift)210{211	bool negative = arg.value < 0;212 213	if (negative)214		arg.value = -arg.value;215	arg.value = arg.value >> shift;216	if (negative)217		arg.value = -arg.value;218	return arg;219}220 221/*222 * @brief223 * Binary additive operators224 */225 226/*227 * @brief228 * result = arg1 + arg2229 */230static inline struct spl_fixed31_32 spl_fixpt_add(struct spl_fixed31_32 arg1, struct spl_fixed31_32 arg2)231{232	struct spl_fixed31_32 res;233 234	ASSERT(((arg1.value >= 0) && (LLONG_MAX - arg1.value >= arg2.value)) ||235		((arg1.value < 0) && (LLONG_MIN - arg1.value <= arg2.value)));236 237	res.value = arg1.value + arg2.value;238 239	return res;240}241 242/*243 * @brief244 * result = arg1 + arg2245 */246static inline struct spl_fixed31_32 spl_fixpt_add_int(struct spl_fixed31_32 arg1, int arg2)247{248	return spl_fixpt_add(arg1, spl_fixpt_from_int(arg2));249}250 251/*252 * @brief253 * result = arg1 - arg2254 */255static inline struct spl_fixed31_32 spl_fixpt_sub(struct spl_fixed31_32 arg1, struct spl_fixed31_32 arg2)256{257	struct spl_fixed31_32 res;258 259	ASSERT(((arg2.value >= 0) && (LLONG_MIN + arg2.value <= arg1.value)) ||260		((arg2.value < 0) && (LLONG_MAX + arg2.value >= arg1.value)));261 262	res.value = arg1.value - arg2.value;263 264	return res;265}266 267/*268 * @brief269 * result = arg1 - arg2270 */271static inline struct spl_fixed31_32 spl_fixpt_sub_int(struct spl_fixed31_32 arg1, int arg2)272{273	return spl_fixpt_sub(arg1, spl_fixpt_from_int(arg2));274}275 276 277/*278 * @brief279 * Binary multiplicative operators280 */281 282/*283 * @brief284 * result = arg1 * arg2285 */286struct spl_fixed31_32 spl_fixpt_mul(struct spl_fixed31_32 arg1, struct spl_fixed31_32 arg2);287 288 289/*290 * @brief291 * result = arg1 * arg2292 */293static inline struct spl_fixed31_32 spl_fixpt_mul_int(struct spl_fixed31_32 arg1, int arg2)294{295	return spl_fixpt_mul(arg1, spl_fixpt_from_int(arg2));296}297 298/*299 * @brief300 * result = square(arg) := arg * arg301 */302struct spl_fixed31_32 spl_fixpt_sqr(struct spl_fixed31_32 arg);303 304/*305 * @brief306 * result = arg1 / arg2307 */308static inline struct spl_fixed31_32 spl_fixpt_div_int(struct spl_fixed31_32 arg1, long long arg2)309{310	return spl_fixpt_from_fraction(arg1.value, spl_fixpt_from_int((int)arg2).value);311}312 313/*314 * @brief315 * result = arg1 / arg2316 */317static inline struct spl_fixed31_32 spl_fixpt_div(struct spl_fixed31_32 arg1, struct spl_fixed31_32 arg2)318{319	return spl_fixpt_from_fraction(arg1.value, arg2.value);320}321 322/*323 * @brief324 * Reciprocal function325 */326 327/*328 * @brief329 * result = reciprocal(arg) := 1 / arg330 *331 * @note332 * No special actions taken in case argument is zero.333 */334struct spl_fixed31_32 spl_fixpt_recip(struct spl_fixed31_32 arg);335 336/*337 * @brief338 * Trigonometric functions339 */340 341/*342 * @brief343 * result = sinc(arg) := sin(arg) / arg344 *345 * @note346 * Argument specified in radians,347 * internally it's normalized to [-2pi...2pi] range.348 */349struct spl_fixed31_32 spl_fixpt_sinc(struct spl_fixed31_32 arg);350 351/*352 * @brief353 * result = sin(arg)354 *355 * @note356 * Argument specified in radians,357 * internally it's normalized to [-2pi...2pi] range.358 */359struct spl_fixed31_32 spl_fixpt_sin(struct spl_fixed31_32 arg);360 361/*362 * @brief363 * result = cos(arg)364 *365 * @note366 * Argument specified in radians367 * and should be in [-2pi...2pi] range -368 * passing arguments outside that range369 * will cause incorrect result!370 */371struct spl_fixed31_32 spl_fixpt_cos(struct spl_fixed31_32 arg);372 373/*374 * @brief375 * Transcendent functions376 */377 378/*379 * @brief380 * result = exp(arg)381 *382 * @note383 * Currently, function is verified for abs(arg) <= 1.384 */385struct spl_fixed31_32 spl_fixpt_exp(struct spl_fixed31_32 arg);386 387/*388 * @brief389 * result = log(arg)390 *391 * @note392 * Currently, abs(arg) should be less than 1.393 * No normalization is done.394 * Currently, no special actions taken395 * in case of invalid argument(s). Take care!396 */397struct spl_fixed31_32 spl_fixpt_log(struct spl_fixed31_32 arg);398 399/*400 * @brief401 * Power function402 */403 404/*405 * @brief406 * result = pow(arg1, arg2)407 *408 * @note409 * Currently, abs(arg1) should be less than 1. Take care!410 */411static inline struct spl_fixed31_32 spl_fixpt_pow(struct spl_fixed31_32 arg1, struct spl_fixed31_32 arg2)412{413	if (arg1.value == 0)414		return arg2.value == 0 ? spl_fixpt_one : spl_fixpt_zero;415 416	return spl_fixpt_exp(417		spl_fixpt_mul(418			spl_fixpt_log(arg1),419			arg2));420}421 422/*423 * @brief424 * Rounding functions425 */426 427/*428 * @brief429 * result = floor(arg) := greatest integer lower than or equal to arg430 */431static inline int spl_fixpt_floor(struct spl_fixed31_32 arg)432{433	unsigned long long arg_value = arg.value > 0 ? arg.value : -arg.value;434 435	if (arg.value >= 0)436		return (int)(arg_value >> FIXED31_32_BITS_PER_FRACTIONAL_PART);437	else438		return -(int)(arg_value >> FIXED31_32_BITS_PER_FRACTIONAL_PART);439}440 441/*442 * @brief443 * result = round(arg) := integer nearest to arg444 */445static inline int spl_fixpt_round(struct spl_fixed31_32 arg)446{447	unsigned long long arg_value = arg.value > 0 ? arg.value : -arg.value;448 449	const long long summand = spl_fixpt_half.value;450 451	ASSERT(LLONG_MAX - (long long)arg_value >= summand);452 453	arg_value += summand;454 455	if (arg.value >= 0)456		return (int)(arg_value >> FIXED31_32_BITS_PER_FRACTIONAL_PART);457	else458		return -(int)(arg_value >> FIXED31_32_BITS_PER_FRACTIONAL_PART);459}460 461/*462 * @brief463 * result = ceil(arg) := lowest integer greater than or equal to arg464 */465static inline int spl_fixpt_ceil(struct spl_fixed31_32 arg)466{467	unsigned long long arg_value = arg.value > 0 ? arg.value : -arg.value;468 469	const long long summand = spl_fixpt_one.value -470		spl_fixpt_epsilon.value;471 472	ASSERT(LLONG_MAX - (long long)arg_value >= summand);473 474	arg_value += summand;475 476	if (arg.value >= 0)477		return (int)(arg_value >> FIXED31_32_BITS_PER_FRACTIONAL_PART);478	else479		return -(int)(arg_value >> FIXED31_32_BITS_PER_FRACTIONAL_PART);480}481 482/* the following two function are used in scaler hw programming to convert fixed483 * point value to format 2 bits from integer part and 19 bits from fractional484 * part. The same applies for u0d19, 0 bits from integer part and 19 bits from485 * fractional486 */487 488unsigned int spl_fixpt_u4d19(struct spl_fixed31_32 arg);489 490unsigned int spl_fixpt_u3d19(struct spl_fixed31_32 arg);491 492unsigned int spl_fixpt_u2d19(struct spl_fixed31_32 arg);493 494unsigned int spl_fixpt_u0d19(struct spl_fixed31_32 arg);495 496unsigned int spl_fixpt_clamp_u0d14(struct spl_fixed31_32 arg);497 498unsigned int spl_fixpt_clamp_u0d10(struct spl_fixed31_32 arg);499 500int spl_fixpt_s4d19(struct spl_fixed31_32 arg);501 502static inline struct spl_fixed31_32 spl_fixpt_truncate(struct spl_fixed31_32 arg, unsigned int frac_bits)503{504	bool negative = arg.value < 0;505 506	if (frac_bits >= FIXED31_32_BITS_PER_FRACTIONAL_PART) {507		ASSERT(frac_bits == FIXED31_32_BITS_PER_FRACTIONAL_PART);508		return arg;509	}510 511	if (negative)512		arg.value = -arg.value;513	arg.value &= (~0ULL) << (FIXED31_32_BITS_PER_FRACTIONAL_PART - frac_bits);514	if (negative)515		arg.value = -arg.value;516	return arg;517}518 519struct spl_fixed31_32 spl_fixpt_from_ux_dy(unsigned int value, unsigned int integer_bits, unsigned int fractional_bits);520struct spl_fixed31_32 spl_fixpt_from_int_dy(unsigned int int_value,521		unsigned int frac_value,522		unsigned int integer_bits,523		unsigned int fractional_bits);524 525#endif526