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