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