Back to home page

OSCL-LXR

 
 

    


0001 /*
0002  * Copyright 2017 Advanced Micro Devices, Inc.
0003  *
0004  * Permission is hereby granted, free of charge, to any person obtaining a
0005  * copy of this software and associated documentation files (the "Software"),
0006  * to deal in the Software without restriction, including without limitation
0007  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
0008  * and/or sell copies of the Software, and to permit persons to whom the
0009  * Software is furnished to do so, subject to the following conditions:
0010  *
0011  * The above copyright notice and this permission notice shall be included in
0012  * all copies or substantial portions of the Software.
0013  *
0014  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
0015  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
0016  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
0017  * THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR
0018  * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
0019  * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
0020  * OTHER DEALINGS IN THE SOFTWARE.
0021  *
0022  * Authors: AMD
0023  *
0024  */
0025 
0026 #include "dcn_calc_math.h"
0027 
0028 #define isNaN(number) ((number) != (number))
0029 
0030 /*
0031  * NOTE:
0032  *   This file is gcc-parseable HW gospel, coming straight from HW engineers.
0033  *
0034  * It doesn't adhere to Linux kernel style and sometimes will do things in odd
0035  * ways. Unless there is something clearly wrong with it the code should
0036  * remain as-is as it provides us with a guarantee from HW that it is correct.
0037  */
0038 
0039 float dcn_bw_mod(const float arg1, const float arg2)
0040 {
0041     if (isNaN(arg1))
0042         return arg2;
0043     if (isNaN(arg2))
0044         return arg1;
0045     return arg1 - arg1 * ((int) (arg1 / arg2));
0046 }
0047 
0048 float dcn_bw_min2(const float arg1, const float arg2)
0049 {
0050     if (isNaN(arg1))
0051         return arg2;
0052     if (isNaN(arg2))
0053         return arg1;
0054     return arg1 < arg2 ? arg1 : arg2;
0055 }
0056 
0057 unsigned int dcn_bw_max(const unsigned int arg1, const unsigned int arg2)
0058 {
0059     return arg1 > arg2 ? arg1 : arg2;
0060 }
0061 float dcn_bw_max2(const float arg1, const float arg2)
0062 {
0063     if (isNaN(arg1))
0064         return arg2;
0065     if (isNaN(arg2))
0066         return arg1;
0067     return arg1 > arg2 ? arg1 : arg2;
0068 }
0069 
0070 float dcn_bw_floor2(const float arg, const float significance)
0071 {
0072     if (significance == 0)
0073         return 0;
0074     return ((int) (arg / significance)) * significance;
0075 }
0076 float dcn_bw_floor(const float arg)
0077 {
0078     return ((int) (arg));
0079 }
0080 
0081 float dcn_bw_ceil(const float arg)
0082 {
0083     float flr = dcn_bw_floor2(arg, 1);
0084 
0085     return flr + 0.00001 >= arg ? arg : flr + 1;
0086 }
0087 
0088 float dcn_bw_ceil2(const float arg, const float significance)
0089 {
0090     float flr = dcn_bw_floor2(arg, significance);
0091     if (significance == 0)
0092         return 0;
0093     return flr + 0.00001 >= arg ? arg : flr + significance;
0094 }
0095 
0096 float dcn_bw_max3(float v1, float v2, float v3)
0097 {
0098     return v3 > dcn_bw_max2(v1, v2) ? v3 : dcn_bw_max2(v1, v2);
0099 }
0100 
0101 float dcn_bw_max5(float v1, float v2, float v3, float v4, float v5)
0102 {
0103     return dcn_bw_max3(v1, v2, v3) > dcn_bw_max2(v4, v5) ? dcn_bw_max3(v1, v2, v3) : dcn_bw_max2(v4, v5);
0104 }
0105 
0106 float dcn_bw_pow(float a, float exp)
0107 {
0108     float temp;
0109     /*ASSERT(exp == (int)exp);*/
0110     if ((int)exp == 0)
0111         return 1;
0112     temp = dcn_bw_pow(a, (int)(exp / 2));
0113     if (((int)exp % 2) == 0) {
0114         return temp * temp;
0115     } else {
0116         if ((int)exp > 0)
0117             return a * temp * temp;
0118         else
0119             return (temp * temp) / a;
0120     }
0121 }
0122 
0123 double dcn_bw_fabs(double a)
0124 {
0125     if (a > 0)
0126         return (a);
0127     else
0128         return (-a);
0129 }
0130 
0131 
0132 float dcn_bw_log(float a, float b)
0133 {
0134     int * const exp_ptr = (int *)(&a);
0135     int x = *exp_ptr;
0136     const int log_2 = ((x >> 23) & 255) - 128;
0137     x &= ~(255 << 23);
0138     x += 127 << 23;
0139     *exp_ptr = x;
0140 
0141     a = ((-1.0f / 3) * a + 2) * a - 2.0f / 3;
0142 
0143     if (b > 2.00001 || b < 1.99999)
0144         return (a + log_2) / dcn_bw_log(b, 2);
0145     else
0146         return (a + log_2);
0147 }