xref: /aosp_15_r20/external/fec/encode_rs_8.c (revision 638691a093b4f9473cd6ee8f3e0139deef159a86)
1*638691a0SAndroid Build Coastguard Worker /* Reed-Solomon encoder
2*638691a0SAndroid Build Coastguard Worker  * Copyright 2004, Phil Karn, KA9Q
3*638691a0SAndroid Build Coastguard Worker  * May be used under the terms of the GNU Lesser General Public License (LGPL)
4*638691a0SAndroid Build Coastguard Worker  */
5*638691a0SAndroid Build Coastguard Worker #include <string.h>
6*638691a0SAndroid Build Coastguard Worker #include "fixed.h"
7*638691a0SAndroid Build Coastguard Worker #ifdef __VEC__
8*638691a0SAndroid Build Coastguard Worker #include <sys/sysctl.h>
9*638691a0SAndroid Build Coastguard Worker #endif
10*638691a0SAndroid Build Coastguard Worker 
11*638691a0SAndroid Build Coastguard Worker 
12*638691a0SAndroid Build Coastguard Worker static enum {UNKNOWN=0,MMX,SSE,SSE2,ALTIVEC,PORT} cpu_mode;
13*638691a0SAndroid Build Coastguard Worker 
14*638691a0SAndroid Build Coastguard Worker static void encode_rs_8_c(data_t *data, data_t *parity,int pad);
15*638691a0SAndroid Build Coastguard Worker #if __vec__
16*638691a0SAndroid Build Coastguard Worker static void encode_rs_8_av(data_t *data, data_t *parity,int pad);
17*638691a0SAndroid Build Coastguard Worker #endif
18*638691a0SAndroid Build Coastguard Worker #if __i386__
19*638691a0SAndroid Build Coastguard Worker int cpu_features(void);
20*638691a0SAndroid Build Coastguard Worker #endif
21*638691a0SAndroid Build Coastguard Worker 
encode_rs_8(data_t * data,data_t * parity,int pad)22*638691a0SAndroid Build Coastguard Worker void encode_rs_8(data_t *data, data_t *parity,int pad){
23*638691a0SAndroid Build Coastguard Worker   if(cpu_mode == UNKNOWN){
24*638691a0SAndroid Build Coastguard Worker #ifdef __i386__
25*638691a0SAndroid Build Coastguard Worker     int f;
26*638691a0SAndroid Build Coastguard Worker     /* Figure out what kind of CPU we have */
27*638691a0SAndroid Build Coastguard Worker     f = cpu_features();
28*638691a0SAndroid Build Coastguard Worker     if(f & (1<<26)){ /* SSE2 is present */
29*638691a0SAndroid Build Coastguard Worker       cpu_mode = SSE2;
30*638691a0SAndroid Build Coastguard Worker     } else if(f & (1<<25)){ /* SSE is present */
31*638691a0SAndroid Build Coastguard Worker       cpu_mode = SSE;
32*638691a0SAndroid Build Coastguard Worker     } else if(f & (1<<23)){ /* MMX is present */
33*638691a0SAndroid Build Coastguard Worker       cpu_mode = MMX;
34*638691a0SAndroid Build Coastguard Worker     } else { /* No SIMD at all */
35*638691a0SAndroid Build Coastguard Worker       cpu_mode = PORT;
36*638691a0SAndroid Build Coastguard Worker     }
37*638691a0SAndroid Build Coastguard Worker #elif __VEC__
38*638691a0SAndroid Build Coastguard Worker     /* Ask the OS if we have Altivec support */
39*638691a0SAndroid Build Coastguard Worker     int selectors[2] = { CTL_HW, HW_VECTORUNIT };
40*638691a0SAndroid Build Coastguard Worker     int hasVectorUnit = 0;
41*638691a0SAndroid Build Coastguard Worker     size_t length = sizeof(hasVectorUnit);
42*638691a0SAndroid Build Coastguard Worker     int error = sysctl(selectors, 2, &hasVectorUnit, &length, NULL, 0);
43*638691a0SAndroid Build Coastguard Worker     if(0 == error && hasVectorUnit)
44*638691a0SAndroid Build Coastguard Worker       cpu_mode = ALTIVEC;
45*638691a0SAndroid Build Coastguard Worker     else
46*638691a0SAndroid Build Coastguard Worker       cpu_mode = PORT;
47*638691a0SAndroid Build Coastguard Worker #else
48*638691a0SAndroid Build Coastguard Worker     cpu_mode = PORT;
49*638691a0SAndroid Build Coastguard Worker #endif
50*638691a0SAndroid Build Coastguard Worker   }
51*638691a0SAndroid Build Coastguard Worker   switch(cpu_mode){
52*638691a0SAndroid Build Coastguard Worker #if __vec__
53*638691a0SAndroid Build Coastguard Worker   case ALTIVEC:
54*638691a0SAndroid Build Coastguard Worker     encode_rs_8_av(data,parity,pad);
55*638691a0SAndroid Build Coastguard Worker     return;
56*638691a0SAndroid Build Coastguard Worker #endif
57*638691a0SAndroid Build Coastguard Worker #if __i386__
58*638691a0SAndroid Build Coastguard Worker   case MMX:
59*638691a0SAndroid Build Coastguard Worker   case SSE:
60*638691a0SAndroid Build Coastguard Worker   case SSE2:
61*638691a0SAndroid Build Coastguard Worker #endif
62*638691a0SAndroid Build Coastguard Worker   default:
63*638691a0SAndroid Build Coastguard Worker     encode_rs_8_c(data,parity,pad);
64*638691a0SAndroid Build Coastguard Worker     return;
65*638691a0SAndroid Build Coastguard Worker   }
66*638691a0SAndroid Build Coastguard Worker }
67*638691a0SAndroid Build Coastguard Worker 
68*638691a0SAndroid Build Coastguard Worker #if __vec__ /* PowerPC G4/G5 Altivec instructions are available */
69*638691a0SAndroid Build Coastguard Worker 
70*638691a0SAndroid Build Coastguard Worker static vector unsigned char reverse = (vector unsigned char)(0,15,14,13,12,11,10,9,8,7,6,5,4,3,2,1);
71*638691a0SAndroid Build Coastguard Worker static vector unsigned char shift_right = (vector unsigned char)(15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30);
72*638691a0SAndroid Build Coastguard Worker 
73*638691a0SAndroid Build Coastguard Worker /* Lookup table for feedback multiplications
74*638691a0SAndroid Build Coastguard Worker  * These are the low half of the coefficients. Since the generator polynomial is
75*638691a0SAndroid Build Coastguard Worker  * palindromic, we form the other half by reversing this one
76*638691a0SAndroid Build Coastguard Worker  */
77*638691a0SAndroid Build Coastguard Worker extern static union { vector unsigned char v; unsigned char c[16]; } table[256];
78*638691a0SAndroid Build Coastguard Worker 
encode_rs_8_av(data_t * data,data_t * parity,int pad)79*638691a0SAndroid Build Coastguard Worker static void encode_rs_8_av(data_t *data, data_t *parity,int pad){
80*638691a0SAndroid Build Coastguard Worker   union { vector unsigned char v[2]; unsigned char c[32]; } shift_register;
81*638691a0SAndroid Build Coastguard Worker   int i;
82*638691a0SAndroid Build Coastguard Worker 
83*638691a0SAndroid Build Coastguard Worker   shift_register.v[0] = (vector unsigned char)(0);
84*638691a0SAndroid Build Coastguard Worker   shift_register.v[1] = (vector unsigned char)(0);
85*638691a0SAndroid Build Coastguard Worker 
86*638691a0SAndroid Build Coastguard Worker   for(i=0;i<NN-NROOTS-pad;i++){
87*638691a0SAndroid Build Coastguard Worker     vector unsigned char feedback0,feedback1;
88*638691a0SAndroid Build Coastguard Worker     unsigned char f;
89*638691a0SAndroid Build Coastguard Worker 
90*638691a0SAndroid Build Coastguard Worker     f = data[i] ^ shift_register.c[31];
91*638691a0SAndroid Build Coastguard Worker     feedback1 = table[f].v;
92*638691a0SAndroid Build Coastguard Worker     feedback0 = vec_perm(feedback1,feedback1,reverse);
93*638691a0SAndroid Build Coastguard Worker 
94*638691a0SAndroid Build Coastguard Worker     /* Shift right one byte */
95*638691a0SAndroid Build Coastguard Worker     shift_register.v[1] = vec_perm(shift_register.v[0],shift_register.v[1],shift_right) ^ feedback1;
96*638691a0SAndroid Build Coastguard Worker     shift_register.v[0] = vec_sro(shift_register.v[0],(vector unsigned char)(8)) ^ feedback0;
97*638691a0SAndroid Build Coastguard Worker     shift_register.c[0] = f;
98*638691a0SAndroid Build Coastguard Worker   }
99*638691a0SAndroid Build Coastguard Worker   for(i=0;i<NROOTS;i++)
100*638691a0SAndroid Build Coastguard Worker     parity[NROOTS-i-1] = shift_register.c[i];
101*638691a0SAndroid Build Coastguard Worker }
102*638691a0SAndroid Build Coastguard Worker #endif
103*638691a0SAndroid Build Coastguard Worker 
104*638691a0SAndroid Build Coastguard Worker /* Portable C version */
encode_rs_8_c(data_t * data,data_t * parity,int pad)105*638691a0SAndroid Build Coastguard Worker static void encode_rs_8_c(data_t *data, data_t *parity,int pad){
106*638691a0SAndroid Build Coastguard Worker 
107*638691a0SAndroid Build Coastguard Worker #include "encode_rs.h"
108*638691a0SAndroid Build Coastguard Worker 
109*638691a0SAndroid Build Coastguard Worker }
110