14562236bSHarry Wentland /*
24562236bSHarry Wentland * Copyright 2012-15 Advanced Micro Devices, Inc.
34562236bSHarry Wentland *
44562236bSHarry Wentland * Permission is hereby granted, free of charge, to any person obtaining a
54562236bSHarry Wentland * copy of this software and associated documentation files (the "Software"),
64562236bSHarry Wentland * to deal in the Software without restriction, including without limitation
74562236bSHarry Wentland * the rights to use, copy, modify, merge, publish, distribute, sublicense,
84562236bSHarry Wentland * and/or sell copies of the Software, and to permit persons to whom the
94562236bSHarry Wentland * Software is furnished to do so, subject to the following conditions:
104562236bSHarry Wentland *
114562236bSHarry Wentland * The above copyright notice and this permission notice shall be included in
124562236bSHarry Wentland * all copies or substantial portions of the Software.
134562236bSHarry Wentland *
144562236bSHarry Wentland * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
154562236bSHarry Wentland * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
164562236bSHarry Wentland * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
174562236bSHarry Wentland * THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR
184562236bSHarry Wentland * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
194562236bSHarry Wentland * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
204562236bSHarry Wentland * OTHER DEALINGS IN THE SOFTWARE.
214562236bSHarry Wentland *
224562236bSHarry Wentland * Authors: AMD
234562236bSHarry Wentland *
244562236bSHarry Wentland */
254562236bSHarry Wentland
264562236bSHarry Wentland #include "dm_services.h"
27*c2de8bc9SRodrigo Siqueira #include "basics/conversion.h"
284562236bSHarry Wentland
294562236bSHarry Wentland #define DIVIDER 10000
304562236bSHarry Wentland
314562236bSHarry Wentland /* S2D13 value in [-3.00...0.9999] */
324562236bSHarry Wentland #define S2D13_MIN (-3 * DIVIDER)
334562236bSHarry Wentland #define S2D13_MAX (3 * DIVIDER)
344562236bSHarry Wentland
fixed_point_to_int_frac(struct fixed31_32 arg,uint8_t integer_bits,uint8_t fractional_bits)354562236bSHarry Wentland uint16_t fixed_point_to_int_frac(
364562236bSHarry Wentland struct fixed31_32 arg,
374562236bSHarry Wentland uint8_t integer_bits,
384562236bSHarry Wentland uint8_t fractional_bits)
394562236bSHarry Wentland {
404562236bSHarry Wentland int32_t numerator;
414562236bSHarry Wentland int32_t divisor = 1 << fractional_bits;
424562236bSHarry Wentland
434562236bSHarry Wentland uint16_t result;
444562236bSHarry Wentland
45eb0e5154SDmytro Laktyushkin uint16_t d = (uint16_t)dc_fixpt_floor(
46eb0e5154SDmytro Laktyushkin dc_fixpt_abs(
474562236bSHarry Wentland arg));
484562236bSHarry Wentland
494562236bSHarry Wentland if (d <= (uint16_t)(1 << integer_bits) - (1 / (uint16_t)divisor))
50eb0e5154SDmytro Laktyushkin numerator = (uint16_t)dc_fixpt_round(
51eb0e5154SDmytro Laktyushkin dc_fixpt_mul_int(
524562236bSHarry Wentland arg,
534562236bSHarry Wentland divisor));
544562236bSHarry Wentland else {
55eb0e5154SDmytro Laktyushkin numerator = dc_fixpt_floor(
56eb0e5154SDmytro Laktyushkin dc_fixpt_sub(
57eb0e5154SDmytro Laktyushkin dc_fixpt_from_int(
584562236bSHarry Wentland 1LL << integer_bits),
59eb0e5154SDmytro Laktyushkin dc_fixpt_recip(
60eb0e5154SDmytro Laktyushkin dc_fixpt_from_int(
614562236bSHarry Wentland divisor))));
624562236bSHarry Wentland }
634562236bSHarry Wentland
644562236bSHarry Wentland if (numerator >= 0)
654562236bSHarry Wentland result = (uint16_t)numerator;
664562236bSHarry Wentland else
674562236bSHarry Wentland result = (uint16_t)(
684562236bSHarry Wentland (1 << (integer_bits + fractional_bits + 1)) + numerator);
694562236bSHarry Wentland
70eb0e5154SDmytro Laktyushkin if ((result != 0) && dc_fixpt_lt(
71eb0e5154SDmytro Laktyushkin arg, dc_fixpt_zero))
724562236bSHarry Wentland result |= 1 << (integer_bits + fractional_bits);
734562236bSHarry Wentland
744562236bSHarry Wentland return result;
754562236bSHarry Wentland }
764a531137SLee Jones /*
774a531137SLee Jones * convert_float_matrix - This converts a double into HW register spec defined format S2D13.
784562236bSHarry Wentland */
convert_float_matrix(uint16_t * matrix,struct fixed31_32 * flt,uint32_t buffer_size)794562236bSHarry Wentland void convert_float_matrix(
804562236bSHarry Wentland uint16_t *matrix,
814562236bSHarry Wentland struct fixed31_32 *flt,
824562236bSHarry Wentland uint32_t buffer_size)
834562236bSHarry Wentland {
844562236bSHarry Wentland const struct fixed31_32 min_2_13 =
85eb0e5154SDmytro Laktyushkin dc_fixpt_from_fraction(S2D13_MIN, DIVIDER);
864562236bSHarry Wentland const struct fixed31_32 max_2_13 =
87eb0e5154SDmytro Laktyushkin dc_fixpt_from_fraction(S2D13_MAX, DIVIDER);
884562236bSHarry Wentland uint32_t i;
894562236bSHarry Wentland
904562236bSHarry Wentland for (i = 0; i < buffer_size; ++i) {
914562236bSHarry Wentland uint32_t reg_value =
924562236bSHarry Wentland fixed_point_to_int_frac(
93eb0e5154SDmytro Laktyushkin dc_fixpt_clamp(
944562236bSHarry Wentland flt[i],
954562236bSHarry Wentland min_2_13,
964562236bSHarry Wentland max_2_13),
974562236bSHarry Wentland 2,
984562236bSHarry Wentland 13);
994562236bSHarry Wentland
1004562236bSHarry Wentland matrix[i] = (uint16_t)reg_value;
1014562236bSHarry Wentland }
1024562236bSHarry Wentland }
103fbe43dcdSAlvin Lee
find_gcd(uint32_t a,uint32_t b)104fbe43dcdSAlvin Lee static uint32_t find_gcd(uint32_t a, uint32_t b)
105fbe43dcdSAlvin Lee {
106fbe43dcdSAlvin Lee uint32_t remainder = 0;
107fbe43dcdSAlvin Lee while (b != 0) {
108fbe43dcdSAlvin Lee remainder = a % b;
109fbe43dcdSAlvin Lee a = b;
110fbe43dcdSAlvin Lee b = remainder;
111fbe43dcdSAlvin Lee }
112fbe43dcdSAlvin Lee return a;
113fbe43dcdSAlvin Lee }
114fbe43dcdSAlvin Lee
reduce_fraction(uint32_t num,uint32_t den,uint32_t * out_num,uint32_t * out_den)115fbe43dcdSAlvin Lee void reduce_fraction(uint32_t num, uint32_t den,
116fbe43dcdSAlvin Lee uint32_t *out_num, uint32_t *out_den)
117fbe43dcdSAlvin Lee {
118fbe43dcdSAlvin Lee uint32_t gcd = 0;
119fbe43dcdSAlvin Lee
120fbe43dcdSAlvin Lee gcd = find_gcd(num, den);
121fbe43dcdSAlvin Lee *out_num = num / gcd;
122fbe43dcdSAlvin Lee *out_den = den / gcd;
123fbe43dcdSAlvin Lee }
124