SoapySDR 0.8.1-ARCH
Vendor and platform neutral SDR interface library
Loading...
Searching...
No Matches
ConverterPrimitives.hpp
Go to the documentation of this file.
1
10
11#pragma once
12#include <stdint.h>
13
14namespace SoapySDR
15{
16
17const uint32_t U32_ZERO_OFFSET = uint32_t(1<<31);
18const uint16_t U16_ZERO_OFFSET = uint16_t(1<<15);
19const uint8_t U8_ZERO_OFFSET = uint8_t(1<<7);
20
21const uint32_t S32_FULL_SCALE = uint32_t(1<<31);
22const uint16_t S16_FULL_SCALE = uint16_t(1<<15);
23const uint8_t S8_FULL_SCALE = uint8_t(1<<7);
24
31// type conversion: float <> signed integers
32
33inline int32_t F32toS32(float from){
34 return int32_t(from * S32_FULL_SCALE);
35}
36inline float S32toF32(int32_t from){
37 return float(from) / S32_FULL_SCALE;
38}
39
40inline int16_t F32toS16(float from){
41 return int16_t(from * S16_FULL_SCALE);
42}
43inline float S16toF32(int16_t from){
44 return float(from) / S16_FULL_SCALE;
45}
46
47inline int8_t F32toS8(float from){
48 return int8_t(from * S8_FULL_SCALE);
49}
50inline float S8toF32(int8_t from){
51 return float(from) / S8_FULL_SCALE;
52}
53
54
55// type conversion: offset binary <> two's complement (signed) integers
56
57inline int32_t U32toS32(uint32_t from){
58 return int32_t(from - U32_ZERO_OFFSET);
59}
60
61inline uint32_t S32toU32(int32_t from){
62 return uint32_t(from) + U32_ZERO_OFFSET;
63}
64
65inline int16_t U16toS16(uint16_t from){
66 return int16_t(from - U16_ZERO_OFFSET);
67}
68
69inline uint16_t S16toU16(int16_t from){
70 return uint16_t(from) + U16_ZERO_OFFSET;
71}
72
73inline int8_t U8toS8(uint8_t from){
74 return int8_t(from - U8_ZERO_OFFSET);
75}
76
77inline uint8_t S8toU8(int8_t from){
78 return uint8_t(from) + U8_ZERO_OFFSET;
79}
80
81// size conversion: signed <> signed
82
83inline int16_t S32toS16(int32_t from){
84 return int16_t(from >> 16);
85}
86inline int32_t S16toS32(int16_t from){
87 return int32_t(from << 16);
88}
89
90inline int8_t S16toS8(int16_t from){
91 return int8_t(from >> 8);
92}
93inline int16_t S8toS16(int8_t from){
94 return int16_t(from << 8);
95}
96
97// compound conversions
98
99// float <> unsigned (type and size)
100
101inline uint32_t F32toU32(float from){
102 return S32toU32(F32toS32(from));
103}
104inline float U32toF32(uint32_t from){
105 return S32toF32(U32toS32(from));
106}
107
108inline uint16_t F32toU16(float from){
109 return S16toU16(F32toS16(from));
110}
111inline float U16toF32(uint16_t from){
112 return S16toF32(U16toS16(from));
113}
114
115inline uint8_t F32toU8(float from){
116 return S8toU8(F32toS8(from));
117}
118inline float U8toF32(uint8_t from){
119 return S8toF32(U8toS8(from));
120}
121
122// signed <> unsigned (type and size)
123
124inline uint16_t S32toU16(int32_t from){
125 return S16toU16(S32toS16(from));
126}
127inline int32_t U16toS32(uint16_t from){
128 return S16toS32(U16toS16(from));
129}
130
131inline uint8_t S32toU8(int32_t from){
132 return S8toU8(S16toS8(S32toS16(from)));
133}
134inline int32_t U8toS32(uint8_t from){
135 return S16toS32(S8toS16(U8toS8(from)));
136}
137
138inline uint8_t S16toU8(int16_t from){
139 return S8toU8(S16toS8(from));
140}
141inline int16_t U8toS16(uint8_t from){
142 return S8toS16(U8toS8(from));
143}
144
145inline uint16_t S8toU16(int8_t from){
146 return S16toU16(S8toS16(from));
147}
148inline int8_t U16toS8(uint16_t from){
149 return S16toS8(U16toS16(from));
150}
151
152
153}
Definition ConverterPrimitives.hpp:15
uint8_t F32toU8(float from)
Definition ConverterPrimitives.hpp:115
uint8_t S16toU8(int16_t from)
Definition ConverterPrimitives.hpp:138
int8_t U16toS8(uint16_t from)
Definition ConverterPrimitives.hpp:148
int16_t S8toS16(int8_t from)
Definition ConverterPrimitives.hpp:93
int16_t S32toS16(int32_t from)
Definition ConverterPrimitives.hpp:83
int32_t F32toS32(float from)
Definition ConverterPrimitives.hpp:33
uint8_t S32toU8(int32_t from)
Definition ConverterPrimitives.hpp:131
int8_t S16toS8(int16_t from)
Definition ConverterPrimitives.hpp:90
const uint8_t U8_ZERO_OFFSET
Definition ConverterPrimitives.hpp:19
uint8_t S8toU8(int8_t from)
Definition ConverterPrimitives.hpp:77
uint16_t F32toU16(float from)
Definition ConverterPrimitives.hpp:108
float S8toF32(int8_t from)
Definition ConverterPrimitives.hpp:50
const uint16_t U16_ZERO_OFFSET
Definition ConverterPrimitives.hpp:18
uint16_t S32toU16(int32_t from)
Definition ConverterPrimitives.hpp:124
float S16toF32(int16_t from)
Definition ConverterPrimitives.hpp:43
const uint32_t U32_ZERO_OFFSET
Definition ConverterPrimitives.hpp:17
float U16toF32(uint16_t from)
Definition ConverterPrimitives.hpp:111
const uint8_t S8_FULL_SCALE
Definition ConverterPrimitives.hpp:23
int16_t U16toS16(uint16_t from)
Definition ConverterPrimitives.hpp:65
uint16_t S8toU16(int8_t from)
Definition ConverterPrimitives.hpp:145
int16_t U8toS16(uint8_t from)
Definition ConverterPrimitives.hpp:141
int16_t F32toS16(float from)
Definition ConverterPrimitives.hpp:40
int32_t U16toS32(uint16_t from)
Definition ConverterPrimitives.hpp:127
float U32toF32(uint32_t from)
Definition ConverterPrimitives.hpp:104
uint16_t S16toU16(int16_t from)
Definition ConverterPrimitives.hpp:69
float U8toF32(uint8_t from)
Definition ConverterPrimitives.hpp:118
int32_t U8toS32(uint8_t from)
Definition ConverterPrimitives.hpp:134
uint32_t F32toU32(float from)
Definition ConverterPrimitives.hpp:101
float S32toF32(int32_t from)
Definition ConverterPrimitives.hpp:36
const uint32_t S32_FULL_SCALE
Definition ConverterPrimitives.hpp:21
int32_t S16toS32(int16_t from)
Definition ConverterPrimitives.hpp:86
uint32_t S32toU32(int32_t from)
Definition ConverterPrimitives.hpp:61
int8_t F32toS8(float from)
Definition ConverterPrimitives.hpp:47
const uint16_t S16_FULL_SCALE
Definition ConverterPrimitives.hpp:22
int32_t U32toS32(uint32_t from)
Definition ConverterPrimitives.hpp:57
int8_t U8toS8(uint8_t from)
Definition ConverterPrimitives.hpp:73