root / host / lib / convert / convert_fc32_with_sse2.cpp @ 52e20f9e
History | View | Annotate | Download (9.1 KB)
| 1 |
//
|
|---|---|
| 2 |
// Copyright 2011 Ettus Research LLC
|
| 3 |
//
|
| 4 |
// This program is free software: you can redistribute it and/or modify
|
| 5 |
// it under the terms of the GNU General Public License as published by
|
| 6 |
// the Free Software Foundation, either version 3 of the License, or
|
| 7 |
// (at your option) any later version.
|
| 8 |
//
|
| 9 |
// This program is distributed in the hope that it will be useful,
|
| 10 |
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
| 11 |
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
| 12 |
// GNU General Public License for more details.
|
| 13 |
//
|
| 14 |
// You should have received a copy of the GNU General Public License
|
| 15 |
// along with this program. If not, see <http://www.gnu.org/licenses/>.
|
| 16 |
//
|
| 17 |
|
| 18 |
#include "convert_common.hpp" |
| 19 |
#include <uhd/utils/byteswap.hpp> |
| 20 |
#include <emmintrin.h> |
| 21 |
|
| 22 |
using namespace uhd::convert; |
| 23 |
|
| 24 |
DECLARE_CONVERTER(fc32, 1, sc16_item32_le, 1, PRIORITY_CUSTOM){ |
| 25 |
const fc32_t *input = reinterpret_cast<const fc32_t *>(inputs[0]); |
| 26 |
item32_t *output = reinterpret_cast<item32_t *>(outputs[0]); |
| 27 |
|
| 28 |
const __m128 scalar = _mm_set_ps1(float(scale_factor)); |
| 29 |
|
| 30 |
#define convert_fc32_1_to_item32_1_nswap_guts(_al_) \
|
| 31 |
for (; i+4 < nsamps; i+=4){ \ |
| 32 |
/* load from input */ \
|
| 33 |
__m128 tmplo = _mm_load ## _al_ ## ps(reinterpret_cast<const float *>(input+i+0)); \ |
| 34 |
__m128 tmphi = _mm_load ## _al_ ## ps(reinterpret_cast<const float *>(input+i+2)); \ |
| 35 |
\ |
| 36 |
/* convert and scale */ \
|
| 37 |
__m128i tmpilo = _mm_cvtps_epi32(_mm_mul_ps(tmplo, scalar)); \ |
| 38 |
__m128i tmpihi = _mm_cvtps_epi32(_mm_mul_ps(tmphi, scalar)); \ |
| 39 |
\ |
| 40 |
/* pack + swap 16-bit pairs */ \
|
| 41 |
__m128i tmpi = _mm_packs_epi32(tmpilo, tmpihi); \ |
| 42 |
tmpi = _mm_shufflelo_epi16(tmpi, _MM_SHUFFLE(2, 3, 0, 1)); \ |
| 43 |
tmpi = _mm_shufflehi_epi16(tmpi, _MM_SHUFFLE(2, 3, 0, 1)); \ |
| 44 |
\ |
| 45 |
/* store to output */ \
|
| 46 |
_mm_storeu_si128(reinterpret_cast<__m128i *>(output+i), tmpi); \
|
| 47 |
} \ |
| 48 |
|
| 49 |
size_t i = 0;
|
| 50 |
|
| 51 |
//dispatch according to alignment
|
| 52 |
switch (size_t(input) & 0xf){ |
| 53 |
case 0x8: |
| 54 |
output[i] = fc32_to_item32(input[i], float(scale_factor)); i++;
|
| 55 |
case 0x0: |
| 56 |
convert_fc32_1_to_item32_1_nswap_guts(_) |
| 57 |
break;
|
| 58 |
default: convert_fc32_1_to_item32_1_nswap_guts(u_)
|
| 59 |
} |
| 60 |
|
| 61 |
//convert remainder
|
| 62 |
for (; i < nsamps; i++){
|
| 63 |
output[i] = fc32_to_item32(input[i], float(scale_factor));
|
| 64 |
} |
| 65 |
} |
| 66 |
|
| 67 |
DECLARE_CONVERTER(fc32, 1, sc16_item32_be, 1, PRIORITY_CUSTOM){ |
| 68 |
const fc32_t *input = reinterpret_cast<const fc32_t *>(inputs[0]); |
| 69 |
item32_t *output = reinterpret_cast<item32_t *>(outputs[0]); |
| 70 |
|
| 71 |
const __m128 scalar = _mm_set_ps1(float(scale_factor)); |
| 72 |
|
| 73 |
#define convert_fc32_1_to_item32_1_bswap_guts(_al_) \
|
| 74 |
for (; i+4 < nsamps; i+=4){ \ |
| 75 |
/* load from input */ \
|
| 76 |
__m128 tmplo = _mm_load ## _al_ ## ps(reinterpret_cast<const float *>(input+i+0)); \ |
| 77 |
__m128 tmphi = _mm_load ## _al_ ## ps(reinterpret_cast<const float *>(input+i+2)); \ |
| 78 |
\ |
| 79 |
/* convert and scale */ \
|
| 80 |
__m128i tmpilo = _mm_cvtps_epi32(_mm_mul_ps(tmplo, scalar)); \ |
| 81 |
__m128i tmpihi = _mm_cvtps_epi32(_mm_mul_ps(tmphi, scalar)); \ |
| 82 |
\ |
| 83 |
/* pack + byteswap -> byteswap 16 bit words */ \
|
| 84 |
__m128i tmpi = _mm_packs_epi32(tmpilo, tmpihi); \ |
| 85 |
tmpi = _mm_or_si128(_mm_srli_epi16(tmpi, 8), _mm_slli_epi16(tmpi, 8)); \ |
| 86 |
\ |
| 87 |
/* store to output */ \
|
| 88 |
_mm_storeu_si128(reinterpret_cast<__m128i *>(output+i), tmpi); \
|
| 89 |
} \ |
| 90 |
|
| 91 |
size_t i = 0;
|
| 92 |
|
| 93 |
//dispatch according to alignment
|
| 94 |
switch (size_t(input) & 0xf){ |
| 95 |
case 0x8: |
| 96 |
output[i] = uhd::byteswap(fc32_to_item32(input[i], float(scale_factor))); i++;
|
| 97 |
case 0x0: |
| 98 |
convert_fc32_1_to_item32_1_bswap_guts(_) |
| 99 |
break;
|
| 100 |
default: convert_fc32_1_to_item32_1_bswap_guts(u_)
|
| 101 |
} |
| 102 |
|
| 103 |
//convert remainder
|
| 104 |
for (; i < nsamps; i++){
|
| 105 |
output[i] = uhd::byteswap(fc32_to_item32(input[i], float(scale_factor)));
|
| 106 |
} |
| 107 |
} |
| 108 |
|
| 109 |
DECLARE_CONVERTER(sc16_item32_le, 1, fc32, 1, PRIORITY_CUSTOM){ |
| 110 |
const item32_t *input = reinterpret_cast<const item32_t *>(inputs[0]); |
| 111 |
fc32_t *output = reinterpret_cast<fc32_t *>(outputs[0]); |
| 112 |
|
| 113 |
const __m128 scalar = _mm_set_ps1(float(scale_factor)/(1 << 16)); |
| 114 |
const __m128i zeroi = _mm_setzero_si128();
|
| 115 |
|
| 116 |
#define convert_item32_1_to_fc32_1_nswap_guts(_al_) \
|
| 117 |
for (; i+4 < nsamps; i+=4){ \ |
| 118 |
/* load from input */ \
|
| 119 |
__m128i tmpi = _mm_loadu_si128(reinterpret_cast<const __m128i *>(input+i)); \ |
| 120 |
\ |
| 121 |
/* unpack + swap 16-bit pairs */ \
|
| 122 |
tmpi = _mm_shufflelo_epi16(tmpi, _MM_SHUFFLE(2, 3, 0, 1)); \ |
| 123 |
tmpi = _mm_shufflehi_epi16(tmpi, _MM_SHUFFLE(2, 3, 0, 1)); \ |
| 124 |
__m128i tmpilo = _mm_unpacklo_epi16(zeroi, tmpi); /* value in upper 16 bits */ \
|
| 125 |
__m128i tmpihi = _mm_unpackhi_epi16(zeroi, tmpi); \ |
| 126 |
\ |
| 127 |
/* convert and scale */ \
|
| 128 |
__m128 tmplo = _mm_mul_ps(_mm_cvtepi32_ps(tmpilo), scalar); \ |
| 129 |
__m128 tmphi = _mm_mul_ps(_mm_cvtepi32_ps(tmpihi), scalar); \ |
| 130 |
\ |
| 131 |
/* store to output */ \
|
| 132 |
_mm_store ## _al_ ## ps(reinterpret_cast<float *>(output+i+0), tmplo); \ |
| 133 |
_mm_store ## _al_ ## ps(reinterpret_cast<float *>(output+i+2), tmphi); \ |
| 134 |
} \ |
| 135 |
|
| 136 |
size_t i = 0;
|
| 137 |
|
| 138 |
//dispatch according to alignment
|
| 139 |
switch (size_t(output) & 0xf){ |
| 140 |
case 0x8: |
| 141 |
output[i] = item32_to_fc32(input[i], float(scale_factor)); i++;
|
| 142 |
case 0x0: |
| 143 |
convert_item32_1_to_fc32_1_nswap_guts(_) |
| 144 |
break;
|
| 145 |
default: convert_item32_1_to_fc32_1_nswap_guts(u_)
|
| 146 |
} |
| 147 |
|
| 148 |
//convert remainder
|
| 149 |
for (; i < nsamps; i++){
|
| 150 |
output[i] = item32_to_fc32(input[i], float(scale_factor));
|
| 151 |
} |
| 152 |
} |
| 153 |
|
| 154 |
DECLARE_CONVERTER(sc16_item32_be, 1, fc32, 1, PRIORITY_CUSTOM){ |
| 155 |
const item32_t *input = reinterpret_cast<const item32_t *>(inputs[0]); |
| 156 |
fc32_t *output = reinterpret_cast<fc32_t *>(outputs[0]); |
| 157 |
|
| 158 |
const __m128 scalar = _mm_set_ps1(float(scale_factor)/(1 << 16)); |
| 159 |
const __m128i zeroi = _mm_setzero_si128();
|
| 160 |
|
| 161 |
#define convert_item32_1_to_fc32_1_bswap_guts(_al_) \
|
| 162 |
for (; i+4 < nsamps; i+=4){ \ |
| 163 |
/* load from input */ \
|
| 164 |
__m128i tmpi = _mm_loadu_si128(reinterpret_cast<const __m128i *>(input+i)); \ |
| 165 |
\ |
| 166 |
/* byteswap + unpack -> byteswap 16 bit words */ \
|
| 167 |
tmpi = _mm_or_si128(_mm_srli_epi16(tmpi, 8), _mm_slli_epi16(tmpi, 8)); \ |
| 168 |
__m128i tmpilo = _mm_unpacklo_epi16(zeroi, tmpi); /* value in upper 16 bits */ \
|
| 169 |
__m128i tmpihi = _mm_unpackhi_epi16(zeroi, tmpi); \ |
| 170 |
\ |
| 171 |
/* convert and scale */ \
|
| 172 |
__m128 tmplo = _mm_mul_ps(_mm_cvtepi32_ps(tmpilo), scalar); \ |
| 173 |
__m128 tmphi = _mm_mul_ps(_mm_cvtepi32_ps(tmpihi), scalar); \ |
| 174 |
\ |
| 175 |
/* store to output */ \
|
| 176 |
_mm_store ## _al_ ## ps(reinterpret_cast<float *>(output+i+0), tmplo); \ |
| 177 |
_mm_store ## _al_ ## ps(reinterpret_cast<float *>(output+i+2), tmphi); \ |
| 178 |
} \ |
| 179 |
|
| 180 |
size_t i = 0;
|
| 181 |
|
| 182 |
//dispatch according to alignment
|
| 183 |
switch (size_t(output) & 0xf){ |
| 184 |
case 0x8: |
| 185 |
output[i] = item32_to_fc32(uhd::byteswap(input[i]), float(scale_factor)); i++;
|
| 186 |
case 0x0: |
| 187 |
convert_item32_1_to_fc32_1_bswap_guts(_) |
| 188 |
break;
|
| 189 |
default: convert_item32_1_to_fc32_1_bswap_guts(u_)
|
| 190 |
} |
| 191 |
|
| 192 |
//convert remainder
|
| 193 |
for (; i < nsamps; i++){
|
| 194 |
output[i] = item32_to_fc32(uhd::byteswap(input[i]), float(scale_factor));
|
| 195 |
} |
| 196 |
} |