aboutsummaryrefslogtreecommitdiffstats
path: root/src/l1/interleave.c
blob: e04c9bae00e241daf47a67d6b7560c5141620021 (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
192
/* GMR-1 interleaving */
/* See GMR-1 05.003 (ETSI TS 101 376-5-3 V1.2.1) - Section 4.8 */

/* (C) 2011-2016 by Sylvain Munaut <tnt@246tNt.com>
 * All Rights Reserved
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU Affero General Public License as published by
 * the Free Software Foundation; either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Affero General Public License for more details.
 *
 * You should have received a copy of the GNU Affero General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

/*! \addtogroup interleave
 *  @{
 */

/*! \file l1/interleave.c
 *  \brief Osmocom GMR-1 interleaving implementation
 */

#include <errno.h>
#include <stdint.h>
#include <stdlib.h>
#include <string.h>

#include <osmocom/core/bits.h>

#include <osmocom/gmr1/l1/interleave.h>


/*! \brief GMR-1 intra burst inteleaver
 *  \param[out] out Interleaved bit array to write to
 *  \param[in] in Original bit array to read from
 *  \param[in] N Dimension of the interleaving matrix
 *
 * Both arrays need to have a length of (8*N).
 * This routine works for any type that has the same size as uint8_t like
 * sbit_t or ubit_t.
 */
void
gmr1_interleave_intra(void *out, const void *in, int N)
{
	const uint8_t *inb = (uint8_t *)in;
	uint8_t *outb = (uint8_t *)out;
	int len = N << 3;
	int kc, kep, i, j;

	for (kc=0; kc<len; kc++) {
		i = kc >> 3;
		j = (5 * kc) & 7;
		kep = N * j + i;
		outb[kep] = inb[kc];
	}
}

/*! \brief GMR-1 intra burst de-interleaver
 *  \param[out] out Deinterleaved bit array to write to
 *  \param[in] in Interleaved bit array to read from
 *  \param[in] N Dimension of the interleaving matrix
 *
 * Both arrays need to have a length of (8*N).
 * This routine works for any type that has the same size as uint8_t like
 * sbit_t or ubit_t.
 */
void
gmr1_deinterleave_intra(void *out, const void *in, int N)
{
	const uint8_t *inb = (uint8_t *)in;
	uint8_t *outb = (uint8_t *)out;
	int len = N << 3;
	int kc, kep, i, j;

	for (kc=0; kc<len; kc++) {
		i = kc >> 3;
		j = (5 * kc) & 7;
		kep = N * j + i;
		outb[kc] = inb[kep];
	}
}


/*! \brief GMR-1 inter burst interleaver initializer
 *  \param[in] il The interleaver object to init
 *  \param[in] N The interleaving depth
 *  \param[in] K The interleaving width
 */
int
gmr1_interleaver_init(struct gmr1_interleaver *il, int N, int K)
{
	int l;
	uint8_t *b;

	memset(il, 0x00, sizeof(struct gmr1_interleaver));

	l = N * K * sizeof(uint8_t);
	b = malloc(l);
	if (!b)
		return -ENOMEM;

	memset(b, 0x00, l);

	il->N = N;
	il->K = K;
	il->bits_cpp = b;

	return 0;
}

/*! \brief GMR-1 inter burst interleaver cleanup
 *  \param[in] il The interleaver object to release
 */
void
gmr1_interleaver_fini(struct gmr1_interleaver *il)
{
	free(il->bits_cpp);

	memset(il, 0x00, sizeof(struct gmr1_interleaver));
}

/*! \brief GMR-1 inter burst interleaver
 *  \param[in] il The interleaver object
 *  \param[out] bits_epp N bits output of interleaver
 *  \param[in] bits_ep N bits input to interleaver
 *
 *  bits_ep and bits_epp can be equal for inplace processing
 */
void
gmr1_interleave_inter(struct gmr1_interleaver *il,
                      void *bits_epp, void *bits_ep)
{
	int i, jk;
	uint8_t *d, *s;

	/* Copy ep to cpp */
	i = il->n % il->N;
	d = &il->bits_cpp[i * il->K];
	memcpy(d, bits_ep, il->K * sizeof(uint8_t));

	/* Copy cpp to epp */
	d = bits_epp;
	s = il->bits_cpp;

	for (jk=0; jk<il->K; jk++) {
		i = ((il->n % il->N) - (jk % il->N) + il->N) % il->N;
		d[jk] = s[(i * il->K) + jk];
	}

	/* Next burst */
	il->n++;
}

/*! \brief GMR-1 inter burst de-interleaver
 *  \param[in] il The interleaver object
 *  \param[out] bits_ep N bits output from de-interleaver
 *  \param[in] bits_epp N bits input to de-interleaver
 *
 *  bits_ep and bits_epp can be equal for inplace processing
 */
void
gmr1_deinterleave_inter(struct gmr1_interleaver *il,
                        void *bits_ep, void *bits_epp)
{
	int i, jk;
	uint8_t *d, *s;

	/* Copy epp to cpp */
	s = bits_epp;
	d = il->bits_cpp;

	for (jk=0; jk<il->K; jk++) {
		i = ((il->n % il->N) - (jk % il->N) + il->N) % il->N;
		d[(i * il->K) + jk] = s[jk];
	}

	/* Copy cpp to ep */
	i = (il->n + 1) % il->N;
	s = &il->bits_cpp[i * il->K];
	memcpy(bits_ep, s, il->K * sizeof(uint8_t));

	/* Next burst */
	il->n++;
}

/*! @} */