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