1b843c749SSergey Zigachev /*
2b843c749SSergey Zigachev * Copyright 2012-15 Advanced Micro Devices, Inc.
3b843c749SSergey Zigachev *
4b843c749SSergey Zigachev * Permission is hereby granted, free of charge, to any person obtaining a
5b843c749SSergey Zigachev * copy of this software and associated documentation files (the "Software"),
6b843c749SSergey Zigachev * to deal in the Software without restriction, including without limitation
7b843c749SSergey Zigachev * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8b843c749SSergey Zigachev * and/or sell copies of the Software, and to permit persons to whom the
9b843c749SSergey Zigachev * Software is furnished to do so, subject to the following conditions:
10b843c749SSergey Zigachev *
11b843c749SSergey Zigachev * The above copyright notice and this permission notice shall be included in
12b843c749SSergey Zigachev * all copies or substantial portions of the Software.
13b843c749SSergey Zigachev *
14b843c749SSergey Zigachev * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15b843c749SSergey Zigachev * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16b843c749SSergey Zigachev * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
17b843c749SSergey Zigachev * THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR
18b843c749SSergey Zigachev * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
19b843c749SSergey Zigachev * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
20b843c749SSergey Zigachev * OTHER DEALINGS IN THE SOFTWARE.
21b843c749SSergey Zigachev *
22b843c749SSergey Zigachev * Authors: AMD
23b843c749SSergey Zigachev *
24b843c749SSergey Zigachev */
25b843c749SSergey Zigachev
26b843c749SSergey Zigachev #include "dm_services.h"
27b843c749SSergey Zigachev
28*78973132SSergey Zigachev #include "conversion.h"
29*78973132SSergey Zigachev
30b843c749SSergey Zigachev #define DIVIDER 10000
31b843c749SSergey Zigachev
32b843c749SSergey Zigachev /* S2D13 value in [-3.00...0.9999] */
33b843c749SSergey Zigachev #define S2D13_MIN (-3 * DIVIDER)
34b843c749SSergey Zigachev #define S2D13_MAX (3 * DIVIDER)
35b843c749SSergey Zigachev
fixed_point_to_int_frac(struct fixed31_32 arg,uint8_t integer_bits,uint8_t fractional_bits)36b843c749SSergey Zigachev uint16_t fixed_point_to_int_frac(
37b843c749SSergey Zigachev struct fixed31_32 arg,
38b843c749SSergey Zigachev uint8_t integer_bits,
39b843c749SSergey Zigachev uint8_t fractional_bits)
40b843c749SSergey Zigachev {
41b843c749SSergey Zigachev int32_t numerator;
42b843c749SSergey Zigachev int32_t divisor = 1 << fractional_bits;
43b843c749SSergey Zigachev
44b843c749SSergey Zigachev uint16_t result;
45b843c749SSergey Zigachev
46b843c749SSergey Zigachev uint16_t d = (uint16_t)dc_fixpt_floor(
47b843c749SSergey Zigachev dc_fixpt_abs(
48b843c749SSergey Zigachev arg));
49b843c749SSergey Zigachev
50b843c749SSergey Zigachev if (d <= (uint16_t)(1 << integer_bits) - (1 / (uint16_t)divisor))
51b843c749SSergey Zigachev numerator = (uint16_t)dc_fixpt_round(
52b843c749SSergey Zigachev dc_fixpt_mul_int(
53b843c749SSergey Zigachev arg,
54b843c749SSergey Zigachev divisor));
55b843c749SSergey Zigachev else {
56b843c749SSergey Zigachev numerator = dc_fixpt_floor(
57b843c749SSergey Zigachev dc_fixpt_sub(
58b843c749SSergey Zigachev dc_fixpt_from_int(
59b843c749SSergey Zigachev 1LL << integer_bits),
60b843c749SSergey Zigachev dc_fixpt_recip(
61b843c749SSergey Zigachev dc_fixpt_from_int(
62b843c749SSergey Zigachev divisor))));
63b843c749SSergey Zigachev }
64b843c749SSergey Zigachev
65b843c749SSergey Zigachev if (numerator >= 0)
66b843c749SSergey Zigachev result = (uint16_t)numerator;
67b843c749SSergey Zigachev else
68b843c749SSergey Zigachev result = (uint16_t)(
69b843c749SSergey Zigachev (1 << (integer_bits + fractional_bits + 1)) + numerator);
70b843c749SSergey Zigachev
71b843c749SSergey Zigachev if ((result != 0) && dc_fixpt_lt(
72b843c749SSergey Zigachev arg, dc_fixpt_zero))
73b843c749SSergey Zigachev result |= 1 << (integer_bits + fractional_bits);
74b843c749SSergey Zigachev
75b843c749SSergey Zigachev return result;
76b843c749SSergey Zigachev }
77b843c749SSergey Zigachev /**
78b843c749SSergey Zigachev * convert_float_matrix
79b843c749SSergey Zigachev * This converts a double into HW register spec defined format S2D13.
80b843c749SSergey Zigachev * @param :
81b843c749SSergey Zigachev * @return None
82b843c749SSergey Zigachev */
convert_float_matrix(uint16_t * matrix,struct fixed31_32 * flt,uint32_t buffer_size)83b843c749SSergey Zigachev void convert_float_matrix(
84b843c749SSergey Zigachev uint16_t *matrix,
85b843c749SSergey Zigachev struct fixed31_32 *flt,
86b843c749SSergey Zigachev uint32_t buffer_size)
87b843c749SSergey Zigachev {
88b843c749SSergey Zigachev const struct fixed31_32 min_2_13 =
89b843c749SSergey Zigachev dc_fixpt_from_fraction(S2D13_MIN, DIVIDER);
90b843c749SSergey Zigachev const struct fixed31_32 max_2_13 =
91b843c749SSergey Zigachev dc_fixpt_from_fraction(S2D13_MAX, DIVIDER);
92b843c749SSergey Zigachev uint32_t i;
93b843c749SSergey Zigachev
94b843c749SSergey Zigachev for (i = 0; i < buffer_size; ++i) {
95b843c749SSergey Zigachev uint32_t reg_value =
96b843c749SSergey Zigachev fixed_point_to_int_frac(
97b843c749SSergey Zigachev dc_fixpt_clamp(
98b843c749SSergey Zigachev flt[i],
99b843c749SSergey Zigachev min_2_13,
100b843c749SSergey Zigachev max_2_13),
101b843c749SSergey Zigachev 2,
102b843c749SSergey Zigachev 13);
103b843c749SSergey Zigachev
104b843c749SSergey Zigachev matrix[i] = (uint16_t)reg_value;
105b843c749SSergey Zigachev }
106b843c749SSergey Zigachev }
107