aboutsummaryrefslogtreecommitdiffstats
path: root/host/lib/convert/sse2_sc16_to_sc16.cpp
blob: 5c81f357bd3ced0c2b3506e9eb7d0b9f5353e6b9 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
//
// Copyright 2015 Ettus Research LLC
// Copyright 2018 Ettus Research, a National Instruments Company
//
// SPDX-License-Identifier: GPL-3.0-or-later
//

#include "convert_common.hpp"
#include <uhd/utils/byteswap.hpp>
#include <emmintrin.h>

using namespace uhd::convert;

//
// SSE 16-bit pair swap
//
// Valid alignment macro arguments are 'u_' and '_' for unaligned and aligned
// access respectively. Macro operates on 4 complex 16-bit integers at a time.
//
//       -----------------
//      | A | B | C | D |   Input
//      -----------------
//        0   1   2   3     Address
//      -----------------
//      | C | D | A | B |   Output
//      -----------------
//
#define CONVERT_SC16_1_TO_SC16_1_NSWAP_GUTS(_ialign_,_oalign_)          \
    for (; i+3 < nsamps; i+=4) {                                        \
        __m128i m0;                                                     \
                                                                        \
        /* load from input */                                           \
        m0 = _mm_load ## _ialign_ ## si128((const __m128i *) (input+i));\
                                                                        \
        /* swap 16-bit pairs */                                         \
        m0 = _mm_shufflelo_epi16(m0, _MM_SHUFFLE(2, 3, 0, 1));          \
        m0 = _mm_shufflehi_epi16(m0, _MM_SHUFFLE(2, 3, 0, 1));          \
                                                                        \
        /* store to output */                                           \
        _mm_store ## _oalign_ ## si128((__m128i *) (output+i), m0);     \
    }                                                                   \

//
// SSE byte swap
//
// Valid alignment macro arguments are 'u_' and '_' for unaligned and aligned
// access respectively. Macro operates on 4 complex 16-bit integers at a time.
//
//       -----------------
//      | A | B | C | D |   Input
//      -----------------
//        0   1   2   3     Address
//      -----------------
//      | B | A | D | C |   Output
//      -----------------
//
#define CONVERT_SC16_1_TO_SC16_1_BSWAP_GUTS(_ialign_,_oalign_)          \
    for (; i+3 < nsamps; i+=4) {                                        \
        __m128i m0, m1, m2;                                             \
                                                                        \
        /* load from input */                                           \
        m0 = _mm_load ## _ialign_ ## si128((const __m128i *) (input+i));\
                                                                        \
        /* byteswap 16 bit words */                                     \
        m1 = _mm_srli_epi16(m0, 8);                                     \
        m2 = _mm_slli_epi16(m0, 8);                                     \
        m0 = _mm_or_si128(m1, m2);                                      \
                                                                        \
        /* store to output */                                           \
        _mm_store ## _oalign_ ## si128((__m128i *) (output+i), m0);     \
    }                                                                   \

DECLARE_CONVERTER(sc16, 1, sc16_item32_le, 1, PRIORITY_SIMD){
    const sc16_t *input = reinterpret_cast<const sc16_t *>(inputs[0]);
    item32_t *output = reinterpret_cast<item32_t *>(outputs[0]);

    size_t i = 0;

    // need to dispatch according to alignment for fastest conversion
    switch (size_t(input) & 0xf){
    case 0x0:
        // the data is 16-byte aligned, so do the fast processing of the bulk of the samples
        CONVERT_SC16_1_TO_SC16_1_NSWAP_GUTS(_,u_)
        break;
    case 0x8:
        if (nsamps < 2)
            break;
        // the first sample is 8-byte aligned - process it to align the remainder of the samples to 16-bytes
        xx_to_item32_sc16<uhd::htowx>(input, output, 2, 1.0);
        i += 2;
        CONVERT_SC16_1_TO_SC16_1_NSWAP_GUTS(_,u_)
        // do faster processing of the bulk of the samples now that we are 16-byte aligned
        break;
    default:
        // we are not 8 or 16-byte aligned, so do fast processing with the unaligned load
        CONVERT_SC16_1_TO_SC16_1_NSWAP_GUTS(u_,u_)
    }

    // convert any remaining samples
    xx_to_item32_sc16<uhd::htowx>(input+i, output+i, nsamps-i, 1.0);
}

DECLARE_CONVERTER(sc16, 1, sc16_item32_be, 1, PRIORITY_SIMD){
    const sc16_t *input = reinterpret_cast<const sc16_t *>(inputs[0]);
    item32_t *output = reinterpret_cast<item32_t *>(outputs[0]);

    size_t i = 0;

    // need to dispatch according to alignment for fastest conversion
    switch (size_t(input) & 0xf){
    case 0x0:
        // the data is 16-byte aligned, so do the fast processing of the bulk of the samples
        CONVERT_SC16_1_TO_SC16_1_BSWAP_GUTS(_,u_)
        break;
    case 0x8:
        if (nsamps < 2)
            break;
        // the first value is 8-byte aligned - process it and prepare the bulk of the data for fast conversion
        xx_to_item32_sc16<uhd::htonx>(input, output, 2, 1.0);
        i += 2;
        // do faster processing of the remaining samples now that we are 16-byte aligned
        CONVERT_SC16_1_TO_SC16_1_BSWAP_GUTS(_,u_)
        break;
    default:
        // we are not 8 or 16-byte aligned, so do fast processing with the unaligned load
        CONVERT_SC16_1_TO_SC16_1_BSWAP_GUTS(u_,u_)
    }

    // convert any remaining samples
    xx_to_item32_sc16<uhd::htonx>(input+i, output+i, nsamps-i, 1.0);
}

DECLARE_CONVERTER(sc16_item32_le, 1, sc16, 1, PRIORITY_SIMD){
    const item32_t *input = reinterpret_cast<const item32_t *>(inputs[0]);
    sc16_t *output = reinterpret_cast<sc16_t *>(outputs[0]);

    size_t i = 0;

    // need to dispatch according to alignment for fastest conversion
    switch (size_t(output) & 0xf){
    case 0x0:
        // the data is 16-byte aligned, so do the fast processing of the bulk of the samples
        CONVERT_SC16_1_TO_SC16_1_NSWAP_GUTS(u_,_)
        break;
    case 0x8:
        if (nsamps < 2)
            break;
        // the first sample is 8-byte aligned - process it to align the remainder of the samples to 16-bytes
        item32_sc16_to_xx<uhd::htowx>(input, output, 2, 1.0);
        i += 2;
        // do faster processing of the bulk of the samples now that we are 16-byte aligned
        CONVERT_SC16_1_TO_SC16_1_NSWAP_GUTS(u_,_)
        break;
    default:
        // we are not 8 or 16-byte aligned, so do fast processing with the unaligned load and store
        CONVERT_SC16_1_TO_SC16_1_NSWAP_GUTS(u_,u_)
    }

    // convert any remaining samples
    item32_sc16_to_xx<uhd::htowx>(input+i, output+i, nsamps-i, 1.0);
}

DECLARE_CONVERTER(sc16_item32_be, 1, sc16, 1, PRIORITY_SIMD){
    const item32_t *input = reinterpret_cast<const item32_t *>(inputs[0]);
    sc16_t *output = reinterpret_cast<sc16_t *>(outputs[0]);

    size_t i = 0;

    // need to dispatch according to alignment for fastest conversion
    switch (size_t(output) & 0xf){
    case 0x0:
        // the data is 16-byte aligned, so do the fast processing of the bulk of the samples
        CONVERT_SC16_1_TO_SC16_1_BSWAP_GUTS(u_,_)
        break;
    case 0x8:
        if (nsamps < 2)
            break;
        // the first sample is 8-byte aligned - process it to align the remainder of the samples to 16-bytes
        item32_sc16_to_xx<uhd::htonx>(input, output, 2, 1.0);
        i += 2;
        // do faster processing of the bulk of the samples now that we are 16-byte aligned
        CONVERT_SC16_1_TO_SC16_1_BSWAP_GUTS(u_,_)
        break;
    default:
        // we are not 8 or 16-byte aligned, so do fast processing with the unaligned load and store
        CONVERT_SC16_1_TO_SC16_1_BSWAP_GUTS(u_,u_)
    }

    // convert any remaining samples
    item32_sc16_to_xx<uhd::htonx>(input+i, output+i, nsamps-i, 1.0);
}