]> git.rkrishnan.org Git - dttsp.git/blob - jDttSP/banal.c
Minor fix to keyb, added hash function to banal.h, added aux input ports with settabl...
[dttsp.git] / jDttSP / banal.c
1 /* banal.c
2
3 This file is part of a program that implements a Software-Defined Radio.
4
5 Copyright (C) 2004 by Frank Brickle, AB2KT and Bob McGwier, N4HY
6
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2 of the License, or
10 (at your option) any later version.
11
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15 GNU General Public License for more details.
16
17 You should have received a copy of the GNU General Public License
18 along with this program; if not, write to the Free Software
19 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
20
21 The authors can be reached by email at
22
23 ab2kt@arrl.net
24 or
25 rwmcgwier@comcast.net
26
27 or by paper mail at
28
29 The DTTS Microwave Society
30 6 Kathleen Place
31 Bridgewater, NJ 08807
32 */
33
34 #include <fromsys.h>
35 #include <banal.h>
36
37 void
38 nilfunc(void) {}
39
40 double
41 sqr(double x) { return x * x; }
42
43 int
44 popcnt(int k) {
45   int c, i;
46   c = k & 01;
47   for (i = 1; i < 32; i++) c += (k >> i) & 01;
48   return c;
49 }
50
51 int
52 npoof2(int n) {
53   int i = 0;
54   --n;
55   while (n > 0) n >>= 1, i++;
56   return i;
57 }
58
59 int
60 nblock2(int n) { return 1 << npoof2(n); }
61
62 int
63 in_blocks(int count, int block_size) {
64   if (block_size < 1) {
65     fprintf(stderr, "block_size zero in in_blocks\n");
66     exit(1);
67   }
68   return (1 + ((count - 1) / block_size));
69 }
70
71 FILE *
72 efopen(char *path, char *mode) {
73   FILE *iop = fopen(path, mode);
74   if (!iop) {
75     fprintf(stderr, "can't open \"%s\" in mode \"%s\"\n", path, mode);
76     exit(1);
77   }
78   return iop;
79 }
80
81 FILE *
82 efreopen(char *path, char *mode, FILE *strm) {
83   FILE *iop = freopen(path, mode, strm);
84   if (!iop) {
85     fprintf(stderr, "can't reopen \"%s\" in mode \"%s\"\n", path, mode);
86     exit(1);
87   }
88   return iop;
89 }
90
91 size_t
92 filesize(char *path) {
93   struct stat sbuf;
94   if (stat(path, &sbuf) == -1) return -1;
95   return sbuf.st_size;
96 }
97
98 size_t
99 fdsize(int fd) {
100   struct stat sbuf;
101   if (fstat(fd, &sbuf) == -1) return -1;
102   return sbuf.st_size;
103 }
104
105 #define MILLION (1000000)
106
107 // return current tv
108 struct timeval
109 now_tv(void) {
110   struct timeval tv;
111   gettimeofday(&tv, 0);
112   return tv;
113 }
114
115 // return ta - tb
116 struct timeval
117 diff_tv(struct timeval *ta, struct timeval *tb) {
118   struct timeval tv;
119   if (tb->tv_usec > ta->tv_usec) {
120     ta->tv_sec--;
121     ta->tv_usec += MILLION;
122   }
123   tv.tv_sec = ta->tv_sec - tb->tv_sec;
124   if ((tv.tv_usec = ta->tv_usec - tb->tv_usec) >= MILLION) {
125     tv.tv_usec -= MILLION;
126     tv.tv_sec++;
127   }
128   return tv;
129 }
130
131 // return ta + tb
132 struct timeval
133 sum_tv(struct timeval *ta, struct timeval *tb) {
134   struct timeval tv; 
135   tv.tv_sec = ta->tv_sec + tb->tv_sec;
136   if ((tv.tv_usec = ta->tv_usec + tb->tv_usec) >= MILLION) {
137     tv.tv_usec -= MILLION;
138     tv.tv_sec++;
139   }
140   return tv;
141 }
142
143 char *
144 fmt_tv(struct timeval *tv) {
145   static char buff[256];
146   snprintf(buff, sizeof(buff), "%ds%du", tv->tv_sec, tv->tv_usec);
147   return buff;
148 }
149
150 char *
151 since(struct timeval *tv) {
152   struct timeval nt = now_tv(),
153                  dt = diff_tv(&nt, tv);
154   return fmt_tv(&dt);
155 }
156
157 // linear integer interpolation:
158 // real vector v, n long, -> real vector u, m long
159 // *** n must divide m
160 // returns actual number of valid points in u
161 // (== n - m/n since v[n] is undefined)
162
163 int
164 hinterp_vec(REAL *u, int m, REAL *v, int n) {
165   if (!u || !v || (n < 2) || (m < n) || (m % n)) return 0;
166   else {
167     int div = m / n, i, j = 0;
168     for (i = 1; i < n; i++) {
169       int k;
170       REAL vl = v[i - 1], del = (v[i] - vl) / div;
171       u[j++] = vl;
172       for (k = 1; k < div; k++) u[j++] = vl + k * del;
173     }
174     u[j++] = v[n - 1];
175     return j;
176   }
177 }
178
179 void
180 status_message(char *msg) { write(2, msg, strlen(msg)); }
181
182 FILE *
183 find_rcfile(char *base) {
184   char path[MAXPATHLEN];
185   FILE *fp;
186   sprintf(path, "./%s", base);
187   if ((fp = fopen(path, "r"))) return fp;
188   else {
189     char *home = getenv("HOME");
190     if (!home)
191       fprintf(stderr, "can't get HOME!\n"), exit(1);
192     sprintf(path, "%s/%s", home, base);
193     if ((fp = fopen(path, "r"))) return fp;
194   }
195   return 0;
196 }
197
198 //------------------------------------------------------------------------
199
200 unsigned long
201 hash(unsigned char *str) {
202   unsigned long hash = 5381;
203   int c;
204   while (c = *str++)
205     hash = ((hash << 5) + hash) + c; // (hash * 33 + c) better
206   return hash;
207 }