2 * Copyright 2012-15 Advanced Micro Devices, Inc.
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:
11 * The above copyright notice and this permission notice shall be included in
12 * all copies or substantial portions of the Software.
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.
26 #include "dm_services.h"
27 #include "conversion.h"
31 /* S2D13 value in [-3.00...0.9999] */
32 #define S2D13_MIN (-3 * DIVIDER)
33 #define S2D13_MAX (3 * DIVIDER)
35 uint16_t fixed_point_to_int_frac(
36 struct fixed31_32 arg,
38 uint8_t fractional_bits)
41 int32_t divisor = 1 << fractional_bits;
45 uint16_t d = (uint16_t)dc_fixpt_floor(
49 if (d <= (uint16_t)(1 << integer_bits) - (1 / (uint16_t)divisor))
50 numerator = (uint16_t)dc_fixpt_round(
55 numerator = dc_fixpt_floor(
65 result = (uint16_t)numerator;
68 (1 << (integer_bits + fractional_bits + 1)) + numerator);
70 if ((result != 0) && dc_fixpt_lt(
72 result |= 1 << (integer_bits + fractional_bits);
77 * convert_float_matrix - This converts a double into HW register spec defined format S2D13.
79 void convert_float_matrix(
81 struct fixed31_32 *flt,
84 const struct fixed31_32 min_2_13 =
85 dc_fixpt_from_fraction(S2D13_MIN, DIVIDER);
86 const struct fixed31_32 max_2_13 =
87 dc_fixpt_from_fraction(S2D13_MAX, DIVIDER);
90 for (i = 0; i < buffer_size; ++i) {
92 fixed_point_to_int_frac(
100 matrix[i] = (uint16_t)reg_value;
104 static uint32_t find_gcd(uint32_t a, uint32_t b)
106 uint32_t remainder = 0;
115 void reduce_fraction(uint32_t num, uint32_t den,
116 uint32_t *out_num, uint32_t *out_den)
120 gcd = find_gcd(num, den);
121 *out_num = num / gcd;
122 *out_den = den / gcd;