1fb4d8502Sjsg /*
2fb4d8502Sjsg * Copyright 2012-15 Advanced Micro Devices, Inc.
3fb4d8502Sjsg *
4fb4d8502Sjsg * Permission is hereby granted, free of charge, to any person obtaining a
5fb4d8502Sjsg * copy of this software and associated documentation files (the "Software"),
6fb4d8502Sjsg * to deal in the Software without restriction, including without limitation
7fb4d8502Sjsg * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8fb4d8502Sjsg * and/or sell copies of the Software, and to permit persons to whom the
9fb4d8502Sjsg * Software is furnished to do so, subject to the following conditions:
10fb4d8502Sjsg *
11fb4d8502Sjsg * The above copyright notice and this permission notice shall be included in
12fb4d8502Sjsg * all copies or substantial portions of the Software.
13fb4d8502Sjsg *
14fb4d8502Sjsg * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15fb4d8502Sjsg * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16fb4d8502Sjsg * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
17fb4d8502Sjsg * THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR
18fb4d8502Sjsg * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
19fb4d8502Sjsg * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
20fb4d8502Sjsg * OTHER DEALINGS IN THE SOFTWARE.
21fb4d8502Sjsg *
22fb4d8502Sjsg * Authors: AMD
23fb4d8502Sjsg *
24fb4d8502Sjsg */
25fb4d8502Sjsg
26fb4d8502Sjsg #ifndef __DAL_FIXED31_32_H__
27fb4d8502Sjsg #define __DAL_FIXED31_32_H__
28fb4d8502Sjsg
29fb4d8502Sjsg #ifndef LLONG_MAX
30fb4d8502Sjsg #define LLONG_MAX 9223372036854775807ll
31fb4d8502Sjsg #endif
32fb4d8502Sjsg #ifndef LLONG_MIN
33fb4d8502Sjsg #define LLONG_MIN (-LLONG_MAX - 1ll)
34fb4d8502Sjsg #endif
35fb4d8502Sjsg
36fb4d8502Sjsg #define FIXED31_32_BITS_PER_FRACTIONAL_PART 32
37fb4d8502Sjsg #ifndef LLONG_MIN
38fb4d8502Sjsg #define LLONG_MIN (1LL<<63)
39fb4d8502Sjsg #endif
40fb4d8502Sjsg #ifndef LLONG_MAX
41fb4d8502Sjsg #define LLONG_MAX (-1LL>>1)
42fb4d8502Sjsg #endif
43fb4d8502Sjsg
44fb4d8502Sjsg /*
45fb4d8502Sjsg * @brief
46fb4d8502Sjsg * Arithmetic operations on real numbers
47fb4d8502Sjsg * represented as fixed-point numbers.
48fb4d8502Sjsg * There are: 1 bit for sign,
49fb4d8502Sjsg * 31 bit for integer part,
50fb4d8502Sjsg * 32 bits for fractional part.
51fb4d8502Sjsg *
52fb4d8502Sjsg * @note
53fb4d8502Sjsg * Currently, overflows and underflows are asserted;
54fb4d8502Sjsg * no special result returned.
55fb4d8502Sjsg */
56fb4d8502Sjsg
57fb4d8502Sjsg struct fixed31_32 {
58fb4d8502Sjsg long long value;
59fb4d8502Sjsg };
60fb4d8502Sjsg
61fb4d8502Sjsg
62fb4d8502Sjsg /*
63fb4d8502Sjsg * @brief
64fb4d8502Sjsg * Useful constants
65fb4d8502Sjsg */
66fb4d8502Sjsg
67fb4d8502Sjsg static const struct fixed31_32 dc_fixpt_zero = { 0 };
68fb4d8502Sjsg static const struct fixed31_32 dc_fixpt_epsilon = { 1LL };
69fb4d8502Sjsg static const struct fixed31_32 dc_fixpt_half = { 0x80000000LL };
70fb4d8502Sjsg static const struct fixed31_32 dc_fixpt_one = { 0x100000000LL };
71fb4d8502Sjsg
72fb4d8502Sjsg /*
73fb4d8502Sjsg * @brief
74fb4d8502Sjsg * Initialization routines
75fb4d8502Sjsg */
76fb4d8502Sjsg
77fb4d8502Sjsg /*
78fb4d8502Sjsg * @brief
79fb4d8502Sjsg * result = numerator / denominator
80fb4d8502Sjsg */
81fb4d8502Sjsg struct fixed31_32 dc_fixpt_from_fraction(long long numerator, long long denominator);
82fb4d8502Sjsg
83fb4d8502Sjsg /*
84fb4d8502Sjsg * @brief
85fb4d8502Sjsg * result = arg
86fb4d8502Sjsg */
dc_fixpt_from_int(int arg)87fb4d8502Sjsg static inline struct fixed31_32 dc_fixpt_from_int(int arg)
88fb4d8502Sjsg {
89fb4d8502Sjsg struct fixed31_32 res;
90fb4d8502Sjsg
91fb4d8502Sjsg res.value = (long long) arg << FIXED31_32_BITS_PER_FRACTIONAL_PART;
92fb4d8502Sjsg
93fb4d8502Sjsg return res;
94fb4d8502Sjsg }
95fb4d8502Sjsg
96fb4d8502Sjsg /*
97fb4d8502Sjsg * @brief
98fb4d8502Sjsg * Unary operators
99fb4d8502Sjsg */
100fb4d8502Sjsg
101fb4d8502Sjsg /*
102fb4d8502Sjsg * @brief
103fb4d8502Sjsg * result = -arg
104fb4d8502Sjsg */
dc_fixpt_neg(struct fixed31_32 arg)105fb4d8502Sjsg static inline struct fixed31_32 dc_fixpt_neg(struct fixed31_32 arg)
106fb4d8502Sjsg {
107fb4d8502Sjsg struct fixed31_32 res;
108fb4d8502Sjsg
109fb4d8502Sjsg res.value = -arg.value;
110fb4d8502Sjsg
111fb4d8502Sjsg return res;
112fb4d8502Sjsg }
113fb4d8502Sjsg
114fb4d8502Sjsg /*
115fb4d8502Sjsg * @brief
116fb4d8502Sjsg * result = abs(arg) := (arg >= 0) ? arg : -arg
117fb4d8502Sjsg */
dc_fixpt_abs(struct fixed31_32 arg)118fb4d8502Sjsg static inline struct fixed31_32 dc_fixpt_abs(struct fixed31_32 arg)
119fb4d8502Sjsg {
120fb4d8502Sjsg if (arg.value < 0)
121fb4d8502Sjsg return dc_fixpt_neg(arg);
122fb4d8502Sjsg else
123fb4d8502Sjsg return arg;
124fb4d8502Sjsg }
125fb4d8502Sjsg
126fb4d8502Sjsg /*
127fb4d8502Sjsg * @brief
128fb4d8502Sjsg * Binary relational operators
129fb4d8502Sjsg */
130fb4d8502Sjsg
131fb4d8502Sjsg /*
132fb4d8502Sjsg * @brief
133fb4d8502Sjsg * result = arg1 < arg2
134fb4d8502Sjsg */
dc_fixpt_lt(struct fixed31_32 arg1,struct fixed31_32 arg2)135fb4d8502Sjsg static inline bool dc_fixpt_lt(struct fixed31_32 arg1, struct fixed31_32 arg2)
136fb4d8502Sjsg {
137fb4d8502Sjsg return arg1.value < arg2.value;
138fb4d8502Sjsg }
139fb4d8502Sjsg
140fb4d8502Sjsg /*
141fb4d8502Sjsg * @brief
142fb4d8502Sjsg * result = arg1 <= arg2
143fb4d8502Sjsg */
dc_fixpt_le(struct fixed31_32 arg1,struct fixed31_32 arg2)144fb4d8502Sjsg static inline bool dc_fixpt_le(struct fixed31_32 arg1, struct fixed31_32 arg2)
145fb4d8502Sjsg {
146fb4d8502Sjsg return arg1.value <= arg2.value;
147fb4d8502Sjsg }
148fb4d8502Sjsg
149fb4d8502Sjsg /*
150fb4d8502Sjsg * @brief
151fb4d8502Sjsg * result = arg1 == arg2
152fb4d8502Sjsg */
dc_fixpt_eq(struct fixed31_32 arg1,struct fixed31_32 arg2)153fb4d8502Sjsg static inline bool dc_fixpt_eq(struct fixed31_32 arg1, struct fixed31_32 arg2)
154fb4d8502Sjsg {
155fb4d8502Sjsg return arg1.value == arg2.value;
156fb4d8502Sjsg }
157fb4d8502Sjsg
158fb4d8502Sjsg /*
159fb4d8502Sjsg * @brief
160fb4d8502Sjsg * result = min(arg1, arg2) := (arg1 <= arg2) ? arg1 : arg2
161fb4d8502Sjsg */
dc_fixpt_min(struct fixed31_32 arg1,struct fixed31_32 arg2)162fb4d8502Sjsg static inline struct fixed31_32 dc_fixpt_min(struct fixed31_32 arg1, struct fixed31_32 arg2)
163fb4d8502Sjsg {
164fb4d8502Sjsg if (arg1.value <= arg2.value)
165fb4d8502Sjsg return arg1;
166fb4d8502Sjsg else
167fb4d8502Sjsg return arg2;
168fb4d8502Sjsg }
169fb4d8502Sjsg
170fb4d8502Sjsg /*
171fb4d8502Sjsg * @brief
172fb4d8502Sjsg * result = max(arg1, arg2) := (arg1 <= arg2) ? arg2 : arg1
173fb4d8502Sjsg */
dc_fixpt_max(struct fixed31_32 arg1,struct fixed31_32 arg2)174fb4d8502Sjsg static inline struct fixed31_32 dc_fixpt_max(struct fixed31_32 arg1, struct fixed31_32 arg2)
175fb4d8502Sjsg {
176fb4d8502Sjsg if (arg1.value <= arg2.value)
177fb4d8502Sjsg return arg2;
178fb4d8502Sjsg else
179fb4d8502Sjsg return arg1;
180fb4d8502Sjsg }
181fb4d8502Sjsg
182fb4d8502Sjsg /*
183fb4d8502Sjsg * @brief
184fb4d8502Sjsg * | min_value, when arg <= min_value
185fb4d8502Sjsg * result = | arg, when min_value < arg < max_value
186fb4d8502Sjsg * | max_value, when arg >= max_value
187fb4d8502Sjsg */
dc_fixpt_clamp(struct fixed31_32 arg,struct fixed31_32 min_value,struct fixed31_32 max_value)188fb4d8502Sjsg static inline struct fixed31_32 dc_fixpt_clamp(
189fb4d8502Sjsg struct fixed31_32 arg,
190fb4d8502Sjsg struct fixed31_32 min_value,
191fb4d8502Sjsg struct fixed31_32 max_value)
192fb4d8502Sjsg {
193fb4d8502Sjsg if (dc_fixpt_le(arg, min_value))
194fb4d8502Sjsg return min_value;
195fb4d8502Sjsg else if (dc_fixpt_le(max_value, arg))
196fb4d8502Sjsg return max_value;
197fb4d8502Sjsg else
198fb4d8502Sjsg return arg;
199fb4d8502Sjsg }
200fb4d8502Sjsg
201fb4d8502Sjsg /*
202fb4d8502Sjsg * @brief
203fb4d8502Sjsg * Binary shift operators
204fb4d8502Sjsg */
205fb4d8502Sjsg
206fb4d8502Sjsg /*
207fb4d8502Sjsg * @brief
208fb4d8502Sjsg * result = arg << shift
209fb4d8502Sjsg */
dc_fixpt_shl(struct fixed31_32 arg,unsigned char shift)210fb4d8502Sjsg static inline struct fixed31_32 dc_fixpt_shl(struct fixed31_32 arg, unsigned char shift)
211fb4d8502Sjsg {
212fb4d8502Sjsg ASSERT(((arg.value >= 0) && (arg.value <= LLONG_MAX >> shift)) ||
213fb4d8502Sjsg ((arg.value < 0) && (arg.value >= ~(LLONG_MAX >> shift))));
214fb4d8502Sjsg
215fb4d8502Sjsg arg.value = arg.value << shift;
216fb4d8502Sjsg
217fb4d8502Sjsg return arg;
218fb4d8502Sjsg }
219fb4d8502Sjsg
220fb4d8502Sjsg /*
221fb4d8502Sjsg * @brief
222fb4d8502Sjsg * result = arg >> shift
223fb4d8502Sjsg */
dc_fixpt_shr(struct fixed31_32 arg,unsigned char shift)224fb4d8502Sjsg static inline struct fixed31_32 dc_fixpt_shr(struct fixed31_32 arg, unsigned char shift)
225fb4d8502Sjsg {
226fb4d8502Sjsg bool negative = arg.value < 0;
227fb4d8502Sjsg
228fb4d8502Sjsg if (negative)
229fb4d8502Sjsg arg.value = -arg.value;
230fb4d8502Sjsg arg.value = arg.value >> shift;
231fb4d8502Sjsg if (negative)
232fb4d8502Sjsg arg.value = -arg.value;
233fb4d8502Sjsg return arg;
234fb4d8502Sjsg }
235fb4d8502Sjsg
236fb4d8502Sjsg /*
237fb4d8502Sjsg * @brief
238fb4d8502Sjsg * Binary additive operators
239fb4d8502Sjsg */
240fb4d8502Sjsg
241fb4d8502Sjsg /*
242fb4d8502Sjsg * @brief
243fb4d8502Sjsg * result = arg1 + arg2
244fb4d8502Sjsg */
dc_fixpt_add(struct fixed31_32 arg1,struct fixed31_32 arg2)245fb4d8502Sjsg static inline struct fixed31_32 dc_fixpt_add(struct fixed31_32 arg1, struct fixed31_32 arg2)
246fb4d8502Sjsg {
247fb4d8502Sjsg struct fixed31_32 res;
248fb4d8502Sjsg
249fb4d8502Sjsg ASSERT(((arg1.value >= 0) && (LLONG_MAX - arg1.value >= arg2.value)) ||
250fb4d8502Sjsg ((arg1.value < 0) && (LLONG_MIN - arg1.value <= arg2.value)));
251fb4d8502Sjsg
252fb4d8502Sjsg res.value = arg1.value + arg2.value;
253fb4d8502Sjsg
254fb4d8502Sjsg return res;
255fb4d8502Sjsg }
256fb4d8502Sjsg
257fb4d8502Sjsg /*
258fb4d8502Sjsg * @brief
259fb4d8502Sjsg * result = arg1 + arg2
260fb4d8502Sjsg */
dc_fixpt_add_int(struct fixed31_32 arg1,int arg2)261fb4d8502Sjsg static inline struct fixed31_32 dc_fixpt_add_int(struct fixed31_32 arg1, int arg2)
262fb4d8502Sjsg {
263fb4d8502Sjsg return dc_fixpt_add(arg1, dc_fixpt_from_int(arg2));
264fb4d8502Sjsg }
265fb4d8502Sjsg
266fb4d8502Sjsg /*
267fb4d8502Sjsg * @brief
268fb4d8502Sjsg * result = arg1 - arg2
269fb4d8502Sjsg */
dc_fixpt_sub(struct fixed31_32 arg1,struct fixed31_32 arg2)270fb4d8502Sjsg static inline struct fixed31_32 dc_fixpt_sub(struct fixed31_32 arg1, struct fixed31_32 arg2)
271fb4d8502Sjsg {
272fb4d8502Sjsg struct fixed31_32 res;
273fb4d8502Sjsg
274fb4d8502Sjsg ASSERT(((arg2.value >= 0) && (LLONG_MIN + arg2.value <= arg1.value)) ||
275fb4d8502Sjsg ((arg2.value < 0) && (LLONG_MAX + arg2.value >= arg1.value)));
276fb4d8502Sjsg
277fb4d8502Sjsg res.value = arg1.value - arg2.value;
278fb4d8502Sjsg
279fb4d8502Sjsg return res;
280fb4d8502Sjsg }
281fb4d8502Sjsg
282fb4d8502Sjsg /*
283fb4d8502Sjsg * @brief
284fb4d8502Sjsg * result = arg1 - arg2
285fb4d8502Sjsg */
dc_fixpt_sub_int(struct fixed31_32 arg1,int arg2)286fb4d8502Sjsg static inline struct fixed31_32 dc_fixpt_sub_int(struct fixed31_32 arg1, int arg2)
287fb4d8502Sjsg {
288fb4d8502Sjsg return dc_fixpt_sub(arg1, dc_fixpt_from_int(arg2));
289fb4d8502Sjsg }
290fb4d8502Sjsg
291fb4d8502Sjsg
292fb4d8502Sjsg /*
293fb4d8502Sjsg * @brief
294fb4d8502Sjsg * Binary multiplicative operators
295fb4d8502Sjsg */
296fb4d8502Sjsg
297fb4d8502Sjsg /*
298fb4d8502Sjsg * @brief
299fb4d8502Sjsg * result = arg1 * arg2
300fb4d8502Sjsg */
301fb4d8502Sjsg struct fixed31_32 dc_fixpt_mul(struct fixed31_32 arg1, struct fixed31_32 arg2);
302fb4d8502Sjsg
303fb4d8502Sjsg
304fb4d8502Sjsg /*
305fb4d8502Sjsg * @brief
306fb4d8502Sjsg * result = arg1 * arg2
307fb4d8502Sjsg */
dc_fixpt_mul_int(struct fixed31_32 arg1,int arg2)308fb4d8502Sjsg static inline struct fixed31_32 dc_fixpt_mul_int(struct fixed31_32 arg1, int arg2)
309fb4d8502Sjsg {
310fb4d8502Sjsg return dc_fixpt_mul(arg1, dc_fixpt_from_int(arg2));
311fb4d8502Sjsg }
312fb4d8502Sjsg
313fb4d8502Sjsg /*
314fb4d8502Sjsg * @brief
315fb4d8502Sjsg * result = square(arg) := arg * arg
316fb4d8502Sjsg */
317fb4d8502Sjsg struct fixed31_32 dc_fixpt_sqr(struct fixed31_32 arg);
318fb4d8502Sjsg
319fb4d8502Sjsg /*
320fb4d8502Sjsg * @brief
321fb4d8502Sjsg * result = arg1 / arg2
322fb4d8502Sjsg */
dc_fixpt_div_int(struct fixed31_32 arg1,long long arg2)323fb4d8502Sjsg static inline struct fixed31_32 dc_fixpt_div_int(struct fixed31_32 arg1, long long arg2)
324fb4d8502Sjsg {
3251bb76ff1Sjsg return dc_fixpt_from_fraction(arg1.value, dc_fixpt_from_int((int)arg2).value);
326fb4d8502Sjsg }
327fb4d8502Sjsg
328fb4d8502Sjsg /*
329fb4d8502Sjsg * @brief
330fb4d8502Sjsg * result = arg1 / arg2
331fb4d8502Sjsg */
dc_fixpt_div(struct fixed31_32 arg1,struct fixed31_32 arg2)332fb4d8502Sjsg static inline struct fixed31_32 dc_fixpt_div(struct fixed31_32 arg1, struct fixed31_32 arg2)
333fb4d8502Sjsg {
334fb4d8502Sjsg return dc_fixpt_from_fraction(arg1.value, arg2.value);
335fb4d8502Sjsg }
336fb4d8502Sjsg
337fb4d8502Sjsg /*
338fb4d8502Sjsg * @brief
339fb4d8502Sjsg * Reciprocal function
340fb4d8502Sjsg */
341fb4d8502Sjsg
342fb4d8502Sjsg /*
343fb4d8502Sjsg * @brief
344fb4d8502Sjsg * result = reciprocal(arg) := 1 / arg
345fb4d8502Sjsg *
346fb4d8502Sjsg * @note
347fb4d8502Sjsg * No special actions taken in case argument is zero.
348fb4d8502Sjsg */
349fb4d8502Sjsg struct fixed31_32 dc_fixpt_recip(struct fixed31_32 arg);
350fb4d8502Sjsg
351fb4d8502Sjsg /*
352fb4d8502Sjsg * @brief
353fb4d8502Sjsg * Trigonometric functions
354fb4d8502Sjsg */
355fb4d8502Sjsg
356fb4d8502Sjsg /*
357fb4d8502Sjsg * @brief
358fb4d8502Sjsg * result = sinc(arg) := sin(arg) / arg
359fb4d8502Sjsg *
360fb4d8502Sjsg * @note
361fb4d8502Sjsg * Argument specified in radians,
362fb4d8502Sjsg * internally it's normalized to [-2pi...2pi] range.
363fb4d8502Sjsg */
364fb4d8502Sjsg struct fixed31_32 dc_fixpt_sinc(struct fixed31_32 arg);
365fb4d8502Sjsg
366fb4d8502Sjsg /*
367fb4d8502Sjsg * @brief
368fb4d8502Sjsg * result = sin(arg)
369fb4d8502Sjsg *
370fb4d8502Sjsg * @note
371fb4d8502Sjsg * Argument specified in radians,
372fb4d8502Sjsg * internally it's normalized to [-2pi...2pi] range.
373fb4d8502Sjsg */
374fb4d8502Sjsg struct fixed31_32 dc_fixpt_sin(struct fixed31_32 arg);
375fb4d8502Sjsg
376fb4d8502Sjsg /*
377fb4d8502Sjsg * @brief
378fb4d8502Sjsg * result = cos(arg)
379fb4d8502Sjsg *
380fb4d8502Sjsg * @note
381fb4d8502Sjsg * Argument specified in radians
382fb4d8502Sjsg * and should be in [-2pi...2pi] range -
383fb4d8502Sjsg * passing arguments outside that range
384fb4d8502Sjsg * will cause incorrect result!
385fb4d8502Sjsg */
386fb4d8502Sjsg struct fixed31_32 dc_fixpt_cos(struct fixed31_32 arg);
387fb4d8502Sjsg
388fb4d8502Sjsg /*
389fb4d8502Sjsg * @brief
390fb4d8502Sjsg * Transcendent functions
391fb4d8502Sjsg */
392fb4d8502Sjsg
393fb4d8502Sjsg /*
394fb4d8502Sjsg * @brief
395fb4d8502Sjsg * result = exp(arg)
396fb4d8502Sjsg *
397fb4d8502Sjsg * @note
398fb4d8502Sjsg * Currently, function is verified for abs(arg) <= 1.
399fb4d8502Sjsg */
400fb4d8502Sjsg struct fixed31_32 dc_fixpt_exp(struct fixed31_32 arg);
401fb4d8502Sjsg
402fb4d8502Sjsg /*
403fb4d8502Sjsg * @brief
404fb4d8502Sjsg * result = log(arg)
405fb4d8502Sjsg *
406fb4d8502Sjsg * @note
407fb4d8502Sjsg * Currently, abs(arg) should be less than 1.
408fb4d8502Sjsg * No normalization is done.
409fb4d8502Sjsg * Currently, no special actions taken
410fb4d8502Sjsg * in case of invalid argument(s). Take care!
411fb4d8502Sjsg */
412fb4d8502Sjsg struct fixed31_32 dc_fixpt_log(struct fixed31_32 arg);
413fb4d8502Sjsg
414fb4d8502Sjsg /*
415fb4d8502Sjsg * @brief
416fb4d8502Sjsg * Power function
417fb4d8502Sjsg */
418fb4d8502Sjsg
419fb4d8502Sjsg /*
420fb4d8502Sjsg * @brief
421fb4d8502Sjsg * result = pow(arg1, arg2)
422fb4d8502Sjsg *
423fb4d8502Sjsg * @note
424fb4d8502Sjsg * Currently, abs(arg1) should be less than 1. Take care!
425fb4d8502Sjsg */
dc_fixpt_pow(struct fixed31_32 arg1,struct fixed31_32 arg2)426fb4d8502Sjsg static inline struct fixed31_32 dc_fixpt_pow(struct fixed31_32 arg1, struct fixed31_32 arg2)
427fb4d8502Sjsg {
428c0a4051fSjsg if (arg1.value == 0)
429c0a4051fSjsg return arg2.value == 0 ? dc_fixpt_one : dc_fixpt_zero;
430c0a4051fSjsg
431fb4d8502Sjsg return dc_fixpt_exp(
432fb4d8502Sjsg dc_fixpt_mul(
433fb4d8502Sjsg dc_fixpt_log(arg1),
434fb4d8502Sjsg arg2));
435fb4d8502Sjsg }
436fb4d8502Sjsg
437fb4d8502Sjsg /*
438fb4d8502Sjsg * @brief
439fb4d8502Sjsg * Rounding functions
440fb4d8502Sjsg */
441fb4d8502Sjsg
442fb4d8502Sjsg /*
443fb4d8502Sjsg * @brief
444fb4d8502Sjsg * result = floor(arg) := greatest integer lower than or equal to arg
445fb4d8502Sjsg */
dc_fixpt_floor(struct fixed31_32 arg)446fb4d8502Sjsg static inline int dc_fixpt_floor(struct fixed31_32 arg)
447fb4d8502Sjsg {
448fb4d8502Sjsg unsigned long long arg_value = arg.value > 0 ? arg.value : -arg.value;
449fb4d8502Sjsg
450fb4d8502Sjsg if (arg.value >= 0)
451fb4d8502Sjsg return (int)(arg_value >> FIXED31_32_BITS_PER_FRACTIONAL_PART);
452fb4d8502Sjsg else
453fb4d8502Sjsg return -(int)(arg_value >> FIXED31_32_BITS_PER_FRACTIONAL_PART);
454fb4d8502Sjsg }
455fb4d8502Sjsg
456fb4d8502Sjsg /*
457fb4d8502Sjsg * @brief
458fb4d8502Sjsg * result = round(arg) := integer nearest to arg
459fb4d8502Sjsg */
dc_fixpt_round(struct fixed31_32 arg)460fb4d8502Sjsg static inline int dc_fixpt_round(struct fixed31_32 arg)
461fb4d8502Sjsg {
462fb4d8502Sjsg unsigned long long arg_value = arg.value > 0 ? arg.value : -arg.value;
463fb4d8502Sjsg
464fb4d8502Sjsg const long long summand = dc_fixpt_half.value;
465fb4d8502Sjsg
466fb4d8502Sjsg ASSERT(LLONG_MAX - (long long)arg_value >= summand);
467fb4d8502Sjsg
468fb4d8502Sjsg arg_value += summand;
469fb4d8502Sjsg
470fb4d8502Sjsg if (arg.value >= 0)
471fb4d8502Sjsg return (int)(arg_value >> FIXED31_32_BITS_PER_FRACTIONAL_PART);
472fb4d8502Sjsg else
473fb4d8502Sjsg return -(int)(arg_value >> FIXED31_32_BITS_PER_FRACTIONAL_PART);
474fb4d8502Sjsg }
475fb4d8502Sjsg
476fb4d8502Sjsg /*
477fb4d8502Sjsg * @brief
478fb4d8502Sjsg * result = ceil(arg) := lowest integer greater than or equal to arg
479fb4d8502Sjsg */
dc_fixpt_ceil(struct fixed31_32 arg)480fb4d8502Sjsg static inline int dc_fixpt_ceil(struct fixed31_32 arg)
481fb4d8502Sjsg {
482fb4d8502Sjsg unsigned long long arg_value = arg.value > 0 ? arg.value : -arg.value;
483fb4d8502Sjsg
484fb4d8502Sjsg const long long summand = dc_fixpt_one.value -
485fb4d8502Sjsg dc_fixpt_epsilon.value;
486fb4d8502Sjsg
487fb4d8502Sjsg ASSERT(LLONG_MAX - (long long)arg_value >= summand);
488fb4d8502Sjsg
489fb4d8502Sjsg arg_value += summand;
490fb4d8502Sjsg
491fb4d8502Sjsg if (arg.value >= 0)
492fb4d8502Sjsg return (int)(arg_value >> FIXED31_32_BITS_PER_FRACTIONAL_PART);
493fb4d8502Sjsg else
494fb4d8502Sjsg return -(int)(arg_value >> FIXED31_32_BITS_PER_FRACTIONAL_PART);
495fb4d8502Sjsg }
496fb4d8502Sjsg
497fb4d8502Sjsg /* the following two function are used in scaler hw programming to convert fixed
498fb4d8502Sjsg * point value to format 2 bits from integer part and 19 bits from fractional
499fb4d8502Sjsg * part. The same applies for u0d19, 0 bits from integer part and 19 bits from
500fb4d8502Sjsg * fractional
501fb4d8502Sjsg */
502fb4d8502Sjsg
503c349dbc7Sjsg unsigned int dc_fixpt_u4d19(struct fixed31_32 arg);
504c349dbc7Sjsg
505fb4d8502Sjsg unsigned int dc_fixpt_u3d19(struct fixed31_32 arg);
506fb4d8502Sjsg
507fb4d8502Sjsg unsigned int dc_fixpt_u2d19(struct fixed31_32 arg);
508fb4d8502Sjsg
509fb4d8502Sjsg unsigned int dc_fixpt_u0d19(struct fixed31_32 arg);
510fb4d8502Sjsg
511fb4d8502Sjsg unsigned int dc_fixpt_clamp_u0d14(struct fixed31_32 arg);
512fb4d8502Sjsg
513fb4d8502Sjsg unsigned int dc_fixpt_clamp_u0d10(struct fixed31_32 arg);
514fb4d8502Sjsg
515fb4d8502Sjsg int dc_fixpt_s4d19(struct fixed31_32 arg);
516fb4d8502Sjsg
dc_fixpt_truncate(struct fixed31_32 arg,unsigned int frac_bits)517fb4d8502Sjsg static inline struct fixed31_32 dc_fixpt_truncate(struct fixed31_32 arg, unsigned int frac_bits)
518fb4d8502Sjsg {
519fb4d8502Sjsg bool negative = arg.value < 0;
520fb4d8502Sjsg
521fb4d8502Sjsg if (frac_bits >= FIXED31_32_BITS_PER_FRACTIONAL_PART) {
522fb4d8502Sjsg ASSERT(frac_bits == FIXED31_32_BITS_PER_FRACTIONAL_PART);
523fb4d8502Sjsg return arg;
524fb4d8502Sjsg }
525fb4d8502Sjsg
526fb4d8502Sjsg if (negative)
527fb4d8502Sjsg arg.value = -arg.value;
528*f005ef32Sjsg arg.value &= (~0ULL) << (FIXED31_32_BITS_PER_FRACTIONAL_PART - frac_bits);
529fb4d8502Sjsg if (negative)
530fb4d8502Sjsg arg.value = -arg.value;
531fb4d8502Sjsg return arg;
532fb4d8502Sjsg }
533fb4d8502Sjsg
534fb4d8502Sjsg #endif
535