116b2f2edSHarry Wentland /*
216b2f2edSHarry Wentland  * Copyright 2017 Advanced Micro Devices, Inc.
316b2f2edSHarry Wentland  *
416b2f2edSHarry Wentland  * Permission is hereby granted, free of charge, to any person obtaining a
516b2f2edSHarry Wentland  * copy of this software and associated documentation files (the "Software"),
616b2f2edSHarry Wentland  * to deal in the Software without restriction, including without limitation
716b2f2edSHarry Wentland  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
816b2f2edSHarry Wentland  * and/or sell copies of the Software, and to permit persons to whom the
916b2f2edSHarry Wentland  * Software is furnished to do so, subject to the following conditions:
1016b2f2edSHarry Wentland  *
1116b2f2edSHarry Wentland  * The above copyright notice and this permission notice shall be included in
1216b2f2edSHarry Wentland  * all copies or substantial portions of the Software.
1316b2f2edSHarry Wentland  *
1416b2f2edSHarry Wentland  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
1516b2f2edSHarry Wentland  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
1616b2f2edSHarry Wentland  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
1716b2f2edSHarry Wentland  * THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR
1816b2f2edSHarry Wentland  * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
1916b2f2edSHarry Wentland  * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
2016b2f2edSHarry Wentland  * OTHER DEALINGS IN THE SOFTWARE.
2116b2f2edSHarry Wentland  *
2216b2f2edSHarry Wentland  * Authors: AMD
2316b2f2edSHarry Wentland  *
2416b2f2edSHarry Wentland  */
2516b2f2edSHarry Wentland 
2616b2f2edSHarry Wentland #ifndef __DML_INLINE_DEFS_H__
2716b2f2edSHarry Wentland #define __DML_INLINE_DEFS_H__
2816b2f2edSHarry Wentland 
2934ad0230SJun Lei #include "dcn_calc_math.h"
30f0ba51e8SBhawanpreet Lakha #include "dml_logger.h"
3116b2f2edSHarry Wentland 
dml_min(double a,double b)3216b2f2edSHarry Wentland static inline double dml_min(double a, double b)
3316b2f2edSHarry Wentland {
3416b2f2edSHarry Wentland 	return (double) dcn_bw_min2(a, b);
3516b2f2edSHarry Wentland }
3616b2f2edSHarry Wentland 
dml_min3(double a,double b,double c)37cba5e870SDmytro Laktyushkin static inline double dml_min3(double a, double b, double c)
38cba5e870SDmytro Laktyushkin {
39cba5e870SDmytro Laktyushkin 	return dml_min(dml_min(a, b), c);
40cba5e870SDmytro Laktyushkin }
41cba5e870SDmytro Laktyushkin 
dml_min4(double a,double b,double c,double d)42cba5e870SDmytro Laktyushkin static inline double dml_min4(double a, double b, double c, double d)
43cba5e870SDmytro Laktyushkin {
44cba5e870SDmytro Laktyushkin 	return dml_min(dml_min(a, b), dml_min(c, d));
45cba5e870SDmytro Laktyushkin }
46cba5e870SDmytro Laktyushkin 
dml_max(double a,double b)4716b2f2edSHarry Wentland static inline double dml_max(double a, double b)
4816b2f2edSHarry Wentland {
4916b2f2edSHarry Wentland 	return (double) dcn_bw_max2(a, b);
5016b2f2edSHarry Wentland }
5116b2f2edSHarry Wentland 
dml_max3(double a,double b,double c)5216b2f2edSHarry Wentland static inline double dml_max3(double a, double b, double c)
5316b2f2edSHarry Wentland {
5416b2f2edSHarry Wentland 	return dml_max(dml_max(a, b), c);
5516b2f2edSHarry Wentland }
5616b2f2edSHarry Wentland 
dml_max4(double a,double b,double c,double d)5716b2f2edSHarry Wentland static inline double dml_max4(double a, double b, double c, double d)
5816b2f2edSHarry Wentland {
5916b2f2edSHarry Wentland 	return dml_max(dml_max(a, b), dml_max(c, d));
6016b2f2edSHarry Wentland }
6116b2f2edSHarry Wentland 
dml_max5(double a,double b,double c,double d,double e)6216b2f2edSHarry Wentland static inline double dml_max5(double a, double b, double c, double d, double e)
6316b2f2edSHarry Wentland {
6416b2f2edSHarry Wentland 	return dml_max(dml_max4(a, b, c, d), e);
6516b2f2edSHarry Wentland }
6616b2f2edSHarry Wentland 
dml_ceil(double a,double granularity)6716b2f2edSHarry Wentland static inline double dml_ceil(double a, double granularity)
6816b2f2edSHarry Wentland {
6916b2f2edSHarry Wentland 	return (double) dcn_bw_ceil2(a, granularity);
7016b2f2edSHarry Wentland }
7116b2f2edSHarry Wentland 
dml_floor(double a,double granularity)7216b2f2edSHarry Wentland static inline double dml_floor(double a, double granularity)
7316b2f2edSHarry Wentland {
7416b2f2edSHarry Wentland 	return (double) dcn_bw_floor2(a, granularity);
7516b2f2edSHarry Wentland }
7616b2f2edSHarry Wentland 
dml_round(double a)77c38606abSRodrigo Siqueira static inline double dml_round(double a)
78c38606abSRodrigo Siqueira {
79*17529ea2SAric Cyr 	const double round_pt = 0.5;
80c38606abSRodrigo Siqueira 
81*17529ea2SAric Cyr 	return dml_floor(a + round_pt, 1);
82c38606abSRodrigo Siqueira }
83c38606abSRodrigo Siqueira 
84b236e048SDmytro Laktyushkin /* float
85b236e048SDmytro Laktyushkin static inline int dml_log2(float x)
8616b2f2edSHarry Wentland {
87b236e048SDmytro Laktyushkin 	unsigned int ix = *((unsigned int *)&x);
88b236e048SDmytro Laktyushkin 
89b236e048SDmytro Laktyushkin 	return (int)((ix >> 23) & 0xff) - 127;
90b236e048SDmytro Laktyushkin }*/
91b236e048SDmytro Laktyushkin 
92b236e048SDmytro Laktyushkin /* double */
dml_log2(double x)93b236e048SDmytro Laktyushkin static inline int dml_log2(double x)
94b236e048SDmytro Laktyushkin {
95b236e048SDmytro Laktyushkin 	unsigned long long ix = *((unsigned long long *)&x);
96b236e048SDmytro Laktyushkin 
97b236e048SDmytro Laktyushkin 	return (int)((ix >> 52) & 0x7ff) - 1023;
9816b2f2edSHarry Wentland }
9916b2f2edSHarry Wentland 
dml_pow(double a,int exp)10016b2f2edSHarry Wentland static inline double dml_pow(double a, int exp)
10116b2f2edSHarry Wentland {
10216b2f2edSHarry Wentland 	return (double) dcn_bw_pow(a, exp);
10316b2f2edSHarry Wentland }
10416b2f2edSHarry Wentland 
dml_fmod(double f,int val)10516b2f2edSHarry Wentland static inline double dml_fmod(double f, int val)
10616b2f2edSHarry Wentland {
10716b2f2edSHarry Wentland 	return (double) dcn_bw_mod(f, val);
10816b2f2edSHarry Wentland }
10916b2f2edSHarry Wentland 
dml_ceil_2(double f)11016b2f2edSHarry Wentland static inline double dml_ceil_2(double f)
11116b2f2edSHarry Wentland {
11216b2f2edSHarry Wentland 	return (double) dcn_bw_ceil2(f, 2);
11316b2f2edSHarry Wentland }
11416b2f2edSHarry Wentland 
dml_ceil_ex(double x,double granularity)11516b2f2edSHarry Wentland static inline double dml_ceil_ex(double x, double granularity)
11616b2f2edSHarry Wentland {
11716b2f2edSHarry Wentland 	return (double) dcn_bw_ceil2(x, granularity);
11816b2f2edSHarry Wentland }
11916b2f2edSHarry Wentland 
dml_floor_ex(double x,double granularity)12016b2f2edSHarry Wentland static inline double dml_floor_ex(double x, double granularity)
12116b2f2edSHarry Wentland {
12216b2f2edSHarry Wentland 	return (double) dcn_bw_floor2(x, granularity);
12316b2f2edSHarry Wentland }
12416b2f2edSHarry Wentland 
dml_round_to_multiple(unsigned int num,unsigned int multiple,unsigned char up)125299f27fdSDave Airlie static inline unsigned int dml_round_to_multiple(unsigned int num,
126299f27fdSDave Airlie 						 unsigned int multiple,
127c38606abSRodrigo Siqueira 						 unsigned char up)
128299f27fdSDave Airlie {
129299f27fdSDave Airlie 	unsigned int remainder;
130299f27fdSDave Airlie 
131299f27fdSDave Airlie 	if (multiple == 0)
132299f27fdSDave Airlie 		return num;
133299f27fdSDave Airlie 
134299f27fdSDave Airlie 	remainder = num % multiple;
135299f27fdSDave Airlie 
136299f27fdSDave Airlie 	if (remainder == 0)
137299f27fdSDave Airlie 		return num;
138299f27fdSDave Airlie 
139299f27fdSDave Airlie 	if (up)
140299f27fdSDave Airlie 		return (num + multiple - remainder);
141299f27fdSDave Airlie 	else
142299f27fdSDave Airlie 		return (num - remainder);
143299f27fdSDave Airlie }
dml_abs(double a)144327e4f12SCharlene Liu static inline double dml_abs(double a)
145327e4f12SCharlene Liu {
146327e4f12SCharlene Liu 	if (a > 0)
147327e4f12SCharlene Liu 		return a;
148327e4f12SCharlene Liu 	else
149327e4f12SCharlene Liu 		return (a*(-1));
150327e4f12SCharlene Liu }
151327e4f12SCharlene Liu 
15216b2f2edSHarry Wentland #endif
153