]> sigrok.org Git - libsigrok.git/blame - src/analog.c
analog: Implement multiplication for sr_rational
[libsigrok.git] / src / analog.c
CommitLineData
fb019a0e
BV
1/*
2 * This file is part of the libsigrok project.
3 *
4 * Copyright (C) 2014 Bert Vermeulen <bert@biot.com>
5 *
6 * This program is free software: you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation, either version 3 of the License, or
9 * (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program. If not, see <http://www.gnu.org/licenses/>.
18 */
19
6ec6c43b 20#include <config.h>
c2a25ebb
BV
21#include <stdio.h>
22#include <stdint.h>
fb019a0e 23#include <string.h>
c2a25ebb 24#include <ctype.h>
c1aae900 25#include <libsigrok/libsigrok.h>
fb019a0e
BV
26#include "libsigrok-internal.h"
27
e00b3f58 28/** @cond PRIVATE */
fb019a0e 29#define LOG_PREFIX "analog"
e00b3f58
UH
30/** @endcond */
31
32/**
33 * @file
34 *
35 * Handling and converting analog data.
36 */
37
38/**
39 * @defgroup grp_analog Analog data handling
40 *
41 * Handling and converting analog data.
42 *
43 * @{
44 */
fb019a0e 45
a5892391
BV
46struct unit_mq_string {
47 uint64_t value;
2c240774 48 const char *str;
a5892391
BV
49};
50
ca7dbb56 51/* Please use the same order as in enum sr_unit (libsigrok.h). */
a5892391
BV
52static struct unit_mq_string unit_strings[] = {
53 { SR_UNIT_VOLT, "V" },
54 { SR_UNIT_AMPERE, "A" },
55 { SR_UNIT_OHM, "\xe2\x84\xa6" },
56 { SR_UNIT_FARAD, "F" },
a5892391
BV
57 { SR_UNIT_KELVIN, "K" },
58 { SR_UNIT_CELSIUS, "\xc2\xb0""C" },
59 { SR_UNIT_FAHRENHEIT, "\xc2\xb0""F" },
60 { SR_UNIT_HERTZ, "Hz" },
61 { SR_UNIT_PERCENTAGE, "%" },
f7bcc686 62 { SR_UNIT_BOOLEAN, "" },
a5892391
BV
63 { SR_UNIT_SECOND, "s" },
64 { SR_UNIT_SIEMENS, "S" },
65 { SR_UNIT_DECIBEL_MW, "dBu" },
66 { SR_UNIT_DECIBEL_VOLT, "dBv" },
f7bcc686 67 { SR_UNIT_UNITLESS, "" },
a5892391
BV
68 { SR_UNIT_DECIBEL_SPL, "dB" },
69 { SR_UNIT_CONCENTRATION, "ppm" },
70 { SR_UNIT_REVOLUTIONS_PER_MINUTE, "RPM" },
71 { SR_UNIT_VOLT_AMPERE, "VA" },
72 { SR_UNIT_WATT, "W" },
73 { SR_UNIT_WATT_HOUR, "Wh" },
74 { SR_UNIT_METER_SECOND, "m/s" },
75 { SR_UNIT_HECTOPASCAL, "hPa" },
76 { SR_UNIT_HUMIDITY_293K, "%rF" },
77 { SR_UNIT_DEGREE, "\xc2\xb0" },
f7bcc686
UH
78 { SR_UNIT_HENRY, "H" },
79 { SR_UNIT_GRAM, "g" },
80 { SR_UNIT_CARAT, "ct" },
81 { SR_UNIT_OUNCE, "oz" },
82 { SR_UNIT_TROY_OUNCE, "oz t" },
83 { SR_UNIT_POUND, "lb" },
84 { SR_UNIT_PENNYWEIGHT, "dwt" },
85 { SR_UNIT_GRAIN, "gr" },
86 { SR_UNIT_TAEL, "tael" },
87 { SR_UNIT_MOMME, "momme" },
88 { SR_UNIT_TOLA, "tola" },
89 { SR_UNIT_PIECE, "pcs" },
a5892391
BV
90 ALL_ZERO
91};
92
ca7dbb56 93/* Please use the same order as in enum sr_mqflag (libsigrok.h). */
a5892391 94static struct unit_mq_string mq_strings[] = {
a5892391
BV
95 { SR_MQFLAG_AC, " AC" },
96 { SR_MQFLAG_DC, " DC" },
97 { SR_MQFLAG_RMS, " RMS" },
98 { SR_MQFLAG_DIODE, " DIODE" },
99 { SR_MQFLAG_HOLD, " HOLD" },
100 { SR_MQFLAG_MAX, " MAX" },
101 { SR_MQFLAG_MIN, " MIN" },
102 { SR_MQFLAG_AUTORANGE, " AUTO" },
103 { SR_MQFLAG_RELATIVE, " REL" },
f7bcc686
UH
104 { SR_MQFLAG_SPL_FREQ_WEIGHT_A, "(A)" },
105 { SR_MQFLAG_SPL_FREQ_WEIGHT_C, "(C)" },
106 { SR_MQFLAG_SPL_FREQ_WEIGHT_Z, "(Z)" },
107 { SR_MQFLAG_SPL_FREQ_WEIGHT_FLAT, "(SPL)" },
108 { SR_MQFLAG_SPL_TIME_WEIGHT_S, " S" },
109 { SR_MQFLAG_SPL_TIME_WEIGHT_F, " F" },
110 { SR_MQFLAG_SPL_LAT, " LAT" },
111 /* Not a standard function for SLMs, so this is a made-up notation. */
112 { SR_MQFLAG_SPL_PCT_OVER_ALARM, "%oA" },
113 { SR_MQFLAG_DURATION, " DURATION" },
a5892391
BV
114 { SR_MQFLAG_AVG, " AVG" },
115 { SR_MQFLAG_REFERENCE, " REF" },
f7bcc686 116 { SR_MQFLAG_UNSTABLE, " UNSTABLE" },
6d5cd3bd 117 { SR_MQFLAG_FOUR_WIRE, " 4-WIRE" },
a5892391
BV
118 ALL_ZERO
119};
120
edb691fc 121SR_PRIV int sr_analog_init(struct sr_datafeed_analog *analog,
41caa319
AJ
122 struct sr_analog_encoding *encoding,
123 struct sr_analog_meaning *meaning,
124 struct sr_analog_spec *spec,
125 int digits)
126{
127 memset(analog, 0, sizeof(*analog));
128 memset(encoding, 0, sizeof(*encoding));
129 memset(meaning, 0, sizeof(*meaning));
130 memset(spec, 0, sizeof(*spec));
131
132 analog->encoding = encoding;
133 analog->meaning = meaning;
134 analog->spec = spec;
135
136 encoding->unitsize = sizeof(float);
137 encoding->is_float = TRUE;
138#ifdef WORDS_BIGENDIAN
139 encoding->is_bigendian = TRUE;
140#else
141 encoding->is_bigendian = FALSE;
142#endif
143 encoding->digits = digits;
144 encoding->is_digits_decimal = TRUE;
145 encoding->scale.p = 1;
146 encoding->scale.q = 1;
147 encoding->offset.p = 0;
148 encoding->offset.q = 1;
149
150 spec->spec_digits = digits;
151
152 return SR_OK;
153}
154
22fb1bff
UH
155/**
156 * Convert an analog datafeed payload to an array of floats.
157 *
158 * @param[in] analog The analog payload to convert. Must not be NULL.
159 * analog->data, analog->meaning, and analog->encoding
160 * must not be NULL.
161 * @param[out] outbuf Memory where to store the result. Must not be NULL.
162 *
163 * Sufficient memory for outbuf must have been pre-allocated by the caller,
164 * who is also responsible for freeing it when no longer needed.
165 *
166 * @retval SR_OK Success.
167 * @retval SR_ERR Unsupported encoding.
168 * @retval SR_ERR_ARG Invalid argument.
169 *
170 * @since 0.4.0
171 */
edb691fc 172SR_API int sr_analog_to_float(const struct sr_datafeed_analog *analog,
4b4fdeea 173 float *outbuf)
fb019a0e
BV
174{
175 float offset;
5cee3d08 176 unsigned int b, i, count;
fb019a0e 177 gboolean bigendian;
5cee3d08
UH
178
179 if (!analog || !(analog->data) || !(analog->meaning)
180 || !(analog->encoding) || !outbuf)
181 return SR_ERR_ARG;
182
183 count = analog->num_samples * g_slist_length(analog->meaning->channels);
fb019a0e
BV
184
185#ifdef WORDS_BIGENDIAN
186 bigendian = TRUE;
187#else
188 bigendian = FALSE;
189#endif
190 if (!analog->encoding->is_float) {
4d376e08
SB
191 float offset = analog->encoding->offset.p / (float)analog->encoding->offset.q;
192 float scale = analog->encoding->scale.p / (float)analog->encoding->scale.q;
193 gboolean is_signed = analog->encoding->is_signed;
194 gboolean is_bigendian = analog->encoding->is_bigendian;
195 int8_t *data8 = (int8_t *)(analog->data);
196 int16_t *data16 = (int16_t *)(analog->data);
197 int32_t *data32 = (int32_t *)(analog->data);
198
199 switch (analog->encoding->unitsize) {
200 case 1:
201 if (is_signed) {
202 for (unsigned int i = 0; i < count; i++) {
203 outbuf[i] = scale * data8[i];
204 outbuf[i] += offset;
205 }
206 } else {
207 for (unsigned int i = 0; i < count; i++) {
208 outbuf[i] = scale * R8(data8 + i);
209 outbuf[i] += offset;
210 }
211 }
212 break;
213 case 2:
214 if (is_signed && is_bigendian) {
215 for (unsigned int i = 0; i < count; i++) {
216 outbuf[i] = scale * RB16S(&data16[i]);
217 outbuf[i] += offset;
218 }
219 } else if (is_bigendian) {
220 for (unsigned int i = 0; i < count; i++) {
221 outbuf[i] = scale * RB16(&data16[i]);
222 outbuf[i] += offset;
223 }
224 } else if (is_signed) {
225 for (unsigned int i = 0; i < count; i++) {
226 outbuf[i] = scale * RL16S(&data16[i]);
227 outbuf[i] += offset;
228 }
229 } else {
230 for (unsigned int i = 0; i < count; i++) {
231 outbuf[i] = scale * RL16(&data16[i]);
232 outbuf[i] += offset;
233 }
234 }
235 break;
236 case 4:
237 if (is_signed && is_bigendian) {
238 for (unsigned int i = 0; i < count; i++) {
239 outbuf[i] = scale * RB32S(&data32[i]);
240 outbuf[i] += offset;
241 }
242 } else if (is_bigendian) {
243 for (unsigned int i = 0; i < count; i++) {
244 outbuf[i] = scale * RB32(&data32[i]);
245 outbuf[i] += offset;
246 }
247 } else if (is_signed) {
248 for (unsigned int i = 0; i < count; i++) {
249 outbuf[i] = scale * RL32S(&data32[i]);
250 outbuf[i] += offset;
251 }
252 } else {
253 for (unsigned int i = 0; i < count; i++) {
254 outbuf[i] = scale * RL32(&data32[i]);
255 outbuf[i] += offset;
256 }
257 }
258 break;
259 default:
260 sr_err("Unsupported unit size '%d' for analog-to-float conversion.",
261 analog->encoding->unitsize);
262 return SR_ERR;
263 }
264 return SR_OK;
fb019a0e
BV
265 }
266
267 if (analog->encoding->unitsize == sizeof(float)
268 && analog->encoding->is_bigendian == bigendian
b07a1b04
ML
269 && analog->encoding->scale.p == 1
270 && analog->encoding->scale.q == 1
4b4fdeea 271 && analog->encoding->offset.p / (float)analog->encoding->offset.q == 0) {
fb019a0e 272 /* The data is already in the right format. */
7d65dd3a 273 memcpy(outbuf, analog->data, count * sizeof(float));
fb019a0e 274 } else {
7d65dd3a 275 for (i = 0; i < count; i += analog->encoding->unitsize) {
fb019a0e
BV
276 for (b = 0; b < analog->encoding->unitsize; b++) {
277 if (analog->encoding->is_bigendian == bigendian)
3e277549
ML
278 ((uint8_t *)outbuf)[i + b] =
279 ((uint8_t *)analog->data)[i * analog->encoding->unitsize + b];
fb019a0e 280 else
3e277549
ML
281 ((uint8_t *)outbuf)[i + (analog->encoding->unitsize - b)] =
282 ((uint8_t *)analog->data)[i * analog->encoding->unitsize + b];
fb019a0e 283 }
b07a1b04
ML
284 if (analog->encoding->scale.p != 1
285 || analog->encoding->scale.q != 1)
4b4fdeea
BV
286 outbuf[i] = (outbuf[i] * analog->encoding->scale.p) / analog->encoding->scale.q;
287 offset = ((float)analog->encoding->offset.p / (float)analog->encoding->offset.q);
288 outbuf[i] += offset;
fb019a0e
BV
289 }
290 }
291
292 return SR_OK;
293}
c2a25ebb 294
22fb1bff 295/**
a5892391
BV
296 * Convert the unit/MQ/MQ flags in the analog struct to a string.
297 *
22fb1bff
UH
298 * @param[in] analog Struct containing the unit, MQ and MQ flags.
299 * Must not be NULL. analog->meaning must not be NULL.
300 * @param[out] result Pointer to store result. Must not be NULL.
a24da9a8
ML
301 *
302 * The string is allocated by the function and must be freed by the caller
303 * after use by calling g_free().
a5892391 304 *
22fb1bff
UH
305 * @retval SR_OK Success.
306 * @retval SR_ERR_ARG Invalid argument.
a5892391
BV
307 *
308 * @since 0.4.0
309 */
edb691fc 310SR_API int sr_analog_unit_to_string(const struct sr_datafeed_analog *analog,
a24da9a8 311 char **result)
a5892391 312{
a24da9a8 313 int i;
5cee3d08
UH
314 GString *buf;
315
316 if (!analog || !(analog->meaning) || !result)
317 return SR_ERR_ARG;
318
319 buf = g_string_new(NULL);
a5892391 320
a5892391
BV
321 for (i = 0; unit_strings[i].value; i++) {
322 if (analog->meaning->unit == unit_strings[i].value) {
a24da9a8 323 g_string_assign(buf, unit_strings[i].str);
a5892391
BV
324 break;
325 }
326 }
327
328 /* More than one MQ flag may apply. */
a24da9a8
ML
329 for (i = 0; mq_strings[i].value; i++)
330 if (analog->meaning->mqflags & mq_strings[i].value)
331 g_string_append(buf, mq_strings[i].str);
332
333 *result = buf->str;
334 g_string_free(buf, FALSE);
a5892391
BV
335
336 return SR_OK;
337}
338
22fb1bff 339/**
90cefe0c
BV
340 * Set sr_rational r to the given value.
341 *
22fb1bff
UH
342 * @param[out] r Rational number struct to set. Must not be NULL.
343 * @param[in] p Numerator.
344 * @param[in] q Denominator.
345 *
346 * @since 0.4.0
90cefe0c 347 */
53e5d3d1 348SR_API void sr_rational_set(struct sr_rational *r, int64_t p, uint64_t q)
90cefe0c 349{
5cee3d08
UH
350 if (!r)
351 return;
352
90cefe0c
BV
353 r->p = p;
354 r->q = q;
355}
356
bdba3626
SB
357#ifndef HAVE___INT128_T
358struct sr_int128_t {
359 int64_t high;
360 uint64_t low;
361};
362
363struct sr_uint128_t {
364 uint64_t high;
365 uint64_t low;
366};
367
368static void mult_int64(struct sr_int128_t *res, const int64_t a,
369 const int64_t b)
370{
371 uint64_t t1, t2, t3, t4;
372
373 t1 = (UINT32_MAX & a) * (UINT32_MAX & b);
374 t2 = (UINT32_MAX & a) * (b >> 32);
375 t3 = (a >> 32) * (UINT32_MAX & b);
376 t4 = (a >> 32) * (b >> 32);
377
378 res->low = t1 + (t2 << 32) + (t3 << 32);
379 res->high = (t1 >> 32) + (uint64_t)((uint32_t)(t2)) + (uint64_t)((uint32_t)(t3));
380 res->high >>= 32;
381 res->high += ((int64_t)t2 >> 32) + ((int64_t)t3 >> 32) + t4;
382}
383
384static void mult_uint64(struct sr_uint128_t *res, const uint64_t a,
385 const uint64_t b)
386{
387 uint64_t t1, t2, t3, t4;
388
389 // (x1 + x2) * (y1 + y2) = x1*y1 + x1*y2 + x2*y1 + x2*y2
390 t1 = (UINT32_MAX & a) * (UINT32_MAX & b);
391 t2 = (UINT32_MAX & a) * (b >> 32);
392 t3 = (a >> 32) * (UINT32_MAX & b);
393 t4 = (a >> 32) * (b >> 32);
394
395 res->low = t1 + (t2 << 32) + (t3 << 32);
396 res->high = (t1 >> 32) + (uint64_t)((uint32_t)(t2)) + (uint64_t)((uint32_t)(t3));
397 res->high >>= 32;
398 res->high += ((int64_t)t2 >> 32) + ((int64_t)t3 >> 32) + t4;
399}
400#endif
401
402/**
403 * Compare two sr_rational for equality
404 *
405 * @param[in] a First value
406 * @param[in] b Second value
407 *
408 * The values are compared for numerical equality, i.e. 2/10 == 1/5
409 *
410 * @retval 1 if both values are equal
411 * @retval 0 otherwise
412 *
413 * @since 0.5.0
414 */
415SR_API int sr_rational_eq(const struct sr_rational *a, const struct sr_rational *b)
416{
417#ifdef HAVE___INT128_T
418 __int128_t m1, m2;
419
420 /* p1/q1 = p2/q2 <=> p1*q2 = p2*q1 */
421 m1 = ((__int128_t)(b->p)) * ((__uint128_t)a->q);
422 m2 = ((__int128_t)(a->p)) * ((__uint128_t)b->q);
423
424 return (m1 == m2);
425
426#else
427 struct sr_int128_t m1, m2;
428
429 mult_int64(&m1, a->q, b->p);
430 mult_int64(&m2, a->p, b->q);
431
432 return (m1.high == m2.high) && (m1.low == m2.low);
433#endif
434}
435
ee1b6054
SB
436/**
437 * Multiply two sr_rational
438 *
439 * @param[in] a First value
440 * @param[in] b Second value
441 * @param[out] res Result
442 *
443 * The resulting nominator/denominator are reduced if the result would not fit
444 * otherwise. If the resulting nominator/denominator are relatively prime,
445 * this may not be possible.
446 *
447 * @retval SR_OK Success.
448 * @retval SR_ERR_ARG Resulting value to large
449 *
450 * @since 0.5.0
451 */
452SR_API int sr_rational_mult(struct sr_rational *res, const struct sr_rational *a,
453 const struct sr_rational *b)
454{
455#ifdef HAVE___INT128_T
456 __int128_t p;
457 __uint128_t q;
458
459 p = (__int128_t)(a->p) * (__int128_t)(b->p);
460 q = (__uint128_t)(a->q) * (__uint128_t)(b->q);
461
462 if ((p > INT64_MAX) || (p < INT64_MIN) || (q > UINT64_MAX)) {
463 while (!((p & 1) || (q & 1))) {
464 p /= 2;
465 q /= 2;
466 }
467 }
468
469 if ((p > INT64_MAX) || (p < INT64_MIN) || (q > UINT64_MAX)) {
470 // TODO: determine gcd to do further reduction
471 return SR_ERR_ARG;
472 }
473
474 res->p = (int64_t)(p);
475 res->q = (uint64_t)(q);
476
477 return SR_OK;
478
479#else
480 struct sr_int128_t p;
481 struct sr_uint128_t q;
482
483 mult_int64(&p, a->p, b->p);
484 mult_uint64(&q, a->q, b->q);
485
486 while (!(p.low & 1) && !(q.low & 1)) {
487 p.low /= 2;
488 if (p.high & 1) p.low |= (1ll << 63);
489 p.high >>= 1;
490 q.low /= 2;
491 if (q.high & 1) q.low |= (1ll << 63);
492 q.high >>= 1;
493 }
494
495 if (q.high)
496 return SR_ERR_ARG;
497 if ((p.high >= 0) && (p.low > INT64_MAX))
498 return SR_ERR_ARG;
499 if (p.high < -1)
500 return SR_ERR_ARG;
501
502 res->p = (int64_t)p.low;
503 res->q = q.low;
504
505 return SR_OK;
506#endif
507}
508
e00b3f58 509/** @} */