Imported Upstream version 3.2.2
[debian/gnuradio] / usrp2 / firmware / lib / print_fxpt.c
1 /* -*- c++ -*- */
2 /*
3  * Copyright 2008 Free Software Foundation, Inc.
4  *
5  * This program is free software: you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation, either version 3 of the License, or
8  * (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
17  */
18 #include <nonstdio.h>
19
20 /*
21  * print uint64_t
22  */
23 void
24 print_uint64(uint64_t u)
25 {
26   const char *_hex = "0123456789ABCDEF";
27   if (u >= 10)
28     print_uint64(u/10);
29   putchar(_hex[u%10]);
30 }
31
32 static void
33 print_thousandths(int thousandths)
34 {
35   putchar('.');
36   if (thousandths < 100)
37     putchar('0');
38   if (thousandths < 10)
39     putchar('0');
40   printf("%d", thousandths);
41 }
42
43
44 void 
45 print_fxpt_freq(u2_fxpt_freq_t v)
46 {
47   if (v < 0){
48     v = -v;
49     putchar('-');
50   }
51
52   int64_t int_part = v >> 20;
53   int32_t frac_part = v & ((1 << 20) - 1);
54   
55 #if 0  
56   // would work, if we had it
57   printf("%lld.%03d", int_part, (frac_part * 1000) >> 20);
58 #else
59   print_uint64(int_part);
60   print_thousandths((frac_part * 1000) >> 20);
61 #endif
62 }
63
64 void
65 print_fxpt_gain(u2_fxpt_gain_t v)
66 {
67   if (v < 0){
68     v = -v;
69     putchar('-');
70   }
71
72   int32_t int_part = v >> 7;
73   int32_t frac_part = v & ((1 << 7) - 1);
74
75 #if 0  
76   // would work, if we had it
77   printf("%d.%03d", int_part, (frac_part * 1000) >> 7);
78 #else
79   printf("%d", int_part);
80   print_thousandths((frac_part * 1000) >> 7);
81 #endif
82 }
83