Kestrel Interface
Loading...
Searching...
No Matches
kest_fixed_point.c
Go to the documentation of this file.
1#include "kest_int.h"
2
3#ifndef PRINTLINES_ALLOWED
4#define PRINTLINES_ALLOWED 0
5#endif
6
7static const char *FNAME = "kest_fixed_point.c";
8
9int32_t float_to_q_nminus1_18bit(float x, int shift)
10{
11 if (shift < 0 || shift > 17) return 0;
12
13 int n = (18 - 1) - shift;
14
15 float scale = ldexpf(1.0f, n);
16
17 float max = (float)((1 << (18 - 1)) - 1) / scale;
18 float min = -(float)(1 << (18 - 1)) / scale;
19
20 if (x > max) x = max;
21 if (x < min) x = min;
22
23 return lrintf(x * scale);
24}
25
27{
28 if (shift < 0 || KEST_FPGA_DATA_WIDTH - 1) return 0;
29
30 int n = (KEST_FPGA_DATA_WIDTH - 1) - shift;
31
32 float scale = (float)(1 << n);
33
34 float max = (float)((1 << (KEST_FPGA_DATA_WIDTH - 1)) - 1) / scale;
35 float min = -(float)(1 << (KEST_FPGA_DATA_WIDTH - 1)) / scale;
36
37 if (x > max) x = max;
38 if (x < min) x = min;
39
40 return (kest_fpga_sample_t)lrintf(x * scale);
41}
42
43
44int16_t float_to_q15(float x)
45{
46 if (x >= 0.999969482421875f) return 32767;
47 if (x <= -1.0f) return -32768;
48
49 return (int16_t)lrintf(x * 32768.0f);
50}
51
52int kest_expression_compute_format(kest_expression *expr, kest_expr_scope *scope, int fmax, int width)
53{
54 if (!expr) return 0;
55
57 float min = range.a;
58 float max = range.b;
59
60 kest_string string;
61 kest_string_init(&string);
62 char *str;
63
64 kest_string_appendf(&string, "Expression \"%s\" has range [%.06f, %.06f], so ", kest_expression_to_string(expr), min, max);
65
66 int format = 0;
67 float p2;
68 float p2_r;
69
70 while (format < fmax)
71 {
72 p2 = ldexpf(1.0, format);
73 p2_r = ldexp(1.0, -(width - 1 - format));
74 if (min >= -p2 && max < p2 - p2_r)
75 break;
76 format++;
77 }
78
79 kest_string_appendf(&string, "therefore, it requires format q%d.%d\n", 1 + format, width - 1 - format);
80
81 str = kest_string_to_native(&string);
82 KEST_PRINTF(str);
83 kest_free(str);
84 kest_string_destroy(&string);
85
86 return format;
87}
88
89
91{
92 KEST_PRINTF("Compute format for filter %p\n", filter);
93 if (!filter)
94 return ERR_NULL_PTR;
95
96 int format = 0;
97 int current_format;
98
99 for (int i = 0; i < filter->coefs.count; i++)
100 {
101 current_format = kest_expression_compute_format(filter->coefs.entries[i], scope, 8, KEST_FPGA_FILTER_WIDTH);
102 format = (current_format > format) ? current_format : format;
103 }
104
105 KEST_PRINTF("Filter format: q%d.%d\n", 1 + format, 18 - format);
106 filter->format = format;
107
108 return NO_ERROR;
109}
void kest_free(void *ptr)
Definition kest_alloc.c:32
#define NO_ERROR
#define ERR_NULL_PTR
kest_interval kest_expression_compute_range(kest_expression *expr, kest_expr_scope *scope)
const char * kest_expression_to_string(kest_expression *expr)
kest_fpga_sample_t float_to_q_nminus1(float x, int shift)
int16_t float_to_q15(float x)
int32_t float_to_q_nminus1_18bit(float x, int shift)
int kest_expression_compute_format(kest_expression *expr, kest_expr_scope *scope, int fmax, int width)
int kest_filter_compute_format(kest_filter *filter, kest_expr_scope *scope)
#define KEST_FPGA_DATA_WIDTH
#define KEST_FPGA_FILTER_WIDTH
int16_t kest_fpga_sample_t
#define KEST_PRINTF(...)
Definition kest_printf.h:10
char * kest_string_to_native(kest_string *string)
int kest_string_appendf(kest_string *string, const char *fmt,...)
int kest_string_init(kest_string *string)
Definition kest_string.c:12
int kest_string_destroy(kest_string *string)
Definition kest_string.c:97
char_list kest_string
Definition kest_string.h:6
struct kest_expression_ptr_list coefs