source: trunk/lib/s2p.h @ 626

Last change on this file since 626 was 626, checked in by cameron, 9 years ago

s2p_movemask implementation

File size: 6.5 KB
RevLine 
[612]1/*  s2p - Serial to Parallel Bit Stream Transposition
2    Copyright (c) 2007, 2008, 2010, Robert D. Cameron.
3    Licensed to the public under the Open Software License 3.0.
4*/
5
6#ifndef S2P_H
7#define S2P_H
8
9#define BytePack SIMD_type
10#define BitBlock SIMD_type
11
[618]12/* Given a block of bytes in 8 consecutive registers s0, s1, ..., s7,
13   s2p transposes the block into 8 parallel bitstream blocks p0, p1, ..., p7.
14
15   The following header shows the intent, although a macro is used for
16   speed.
[612]17static inline void s2p(BytePack s0, BytePack s1, BytePack s2, BytePack s3,
18                       BytePack s5, BytePack s6, BytePack s7, BytePack s8,
19                       BitBlock& p0, BitBlock& p1, BitBlock& p2, BitBlock& p3,
20                       BitBlock& p4, BitBlock& p5, BitBlock& p6, BitBlock& p7);
[618]21*/
[612]22
[626]23/*  1.  ALGORITHM Selection. 
24        Choice of 3 algorithms: s2p_ideal, s2p_movemask, s2p_bytepack
25        Default is s2p_bytepack.
26        Compiling with -DUSE_S2P_IDEAL or -DUSE_S2P_MOVEMASK to override.
27*/
28
[612]29#ifdef USE_S2P_IDEAL
30#define S2P_ALGORITHM s2p_ideal
31#endif
32
33#ifdef USE_S2P_MOVEMASK
34#define S2P_ALGORITHM s2p_movemask  /* Not yet implemented. */
35#endif
36
37#ifndef S2P_ALGORITHM
38#define S2P_ALGORITHM s2p_bytepack
39#endif
40
41
42
[626]43#if (BYTE_ORDER == BIG_ENDIAN)
44#define s2p(s0, s1, s2, s3, s4, s5, s6, s7, p0, p1, p2, p3, p4, p5, p6, p7)\
45  S2P_ALGORITHM(s0, s1, s2, s3, s4, s5, s6, s7, p0, p1, p2, p3, p4, p5, p6, p7)
46#endif
47#if (BYTE_ORDER == LITTLE_ENDIAN)
48#define s2p(s0, s1, s2, s3, s4, s5, s6, s7, p0, p1, p2, p3, p4, p5, p6, p7)\
49  S2P_ALGORITHM(s7, s6, s5, s4, s3, s2, s1, s0, p0, p1, p2, p3, p4, p5, p6, p7)
50#endif
51
52
[612]53/*  s2p_ideal is an ideal serial to parallel transposition
54    algorithm given an architecture with native support for
55    simd_pack_{8,4,2}_{hh,ll} operations, achieving transposition
56    of 8 serial bytepacks into 8 parallel bitblocks in only 24 pack
57    operations.
58*/
59
60#define s2p_ideal(s0, s1, s2, s3, s4, s5, s6, s7, p0, p1, p2, p3, p4, p5, p6, p7) \
61  do {\
62        BitBlock bit0123_0, bit0123_1, bit0123_2, bit0123_3,\
63        bit4567_0, bit4567_1, bit4567_2, bit4567_3;\
64        BitBlock bit01_0, bit01_1, bit23_0, bit23_1, bit45_0, bit45_1, bit67_0, bit67_1;\
65        bit0123_0 = simd_pack_8_hh(s0, s1);\
66        bit0123_1 = simd_pack_8_hh(s2, s3);\
67        bit0123_2 = simd_pack_8_hh(s4, s5);\
68        bit0123_3 = simd_pack_8_hh(s6, s7);\
69        bit4567_0 = simd_pack_8_ll(s0, s1);\
70        bit4567_1 = simd_pack_8_ll(s2, s3);\
71        bit4567_2 = simd_pack_8_ll(s4, s5);\
72        bit4567_3 = simd_pack_8_ll(s6, s7);\
73        bit01_0 = simd_pack_4_hh(bit0123_0, bit0123_1);\
74        bit01_1 = simd_pack_4_hh(bit0123_2, bit0123_3);\
75        bit23_0 = simd_pack_4_ll(bit0123_0, bit0123_1);\
76        bit23_1 = simd_pack_4_ll(bit0123_2, bit0123_3);\
77        bit45_0 = simd_pack_4_hh(bit4567_0, bit4567_1);\
78        bit45_1 = simd_pack_4_hh(bit4567_2, bit4567_3);\
79        bit67_0 = simd_pack_4_ll(bit4567_0, bit4567_1);\
80        bit67_1 = simd_pack_4_ll(bit4567_2, bit4567_3);\
81        p0 = simd_pack_2_hh(bit01_0, bit01_1);\
82        p1 = simd_pack_2_ll(bit01_0, bit01_1);\
83        p2 = simd_pack_2_hh(bit23_0, bit23_1);\
84        p3 = simd_pack_2_ll(bit23_0, bit23_1);\
85        p4 = simd_pack_2_hh(bit45_0, bit45_1);\
86        p5 = simd_pack_2_ll(bit45_0, bit45_1);\
87        p6 = simd_pack_2_hh(bit67_0, bit67_1);\
88        p7 = simd_pack_2_ll(bit67_0, bit67_1);\
89  } while(0)
90
91
92/*  s2p_bytepack is a fast serial to parallel transposition
93    algorithm given an architecture with simd_pack_16 operations,
94    but not at small field widths.
95    MMX, SSE, Altivec ...
96*/
97
98#define s2p_step(s0, s1, hi_mask, shift, p0, p1)  \
99  do {\
100        BitBlock t0,t1;\
101        t0 = simd_pack_16_hh(s0, s1);\
102        t1 = simd_pack_16_ll(s0, s1);\
103        p0 = simd_if(hi_mask, t0, simd_srli_16(t1, shift));\
104        p1 = simd_if(hi_mask, simd_slli_16(t0, shift), t1);\
105  } while(0)
106
107#define s2p_bytepack(s0, s1, s2, s3, s4, s5, s6, s7, p0, p1, p2, p3, p4, p5, p6, p7) \
108  do {\
109        BitBlock bit00224466_0,bit00224466_1,bit00224466_2,bit00224466_3;\
110        BitBlock bit11335577_0,bit11335577_1,bit11335577_2,bit11335577_3;\
111        BitBlock bit00004444_0,bit22226666_0,bit00004444_1,bit22226666_1;\
112        BitBlock bit11115555_0,bit33337777_0,bit11115555_1,bit33337777_1;\
113        s2p_step(s0,s1,simd_himask_2,1,bit00224466_0,bit11335577_0);\
114        s2p_step(s2,s3,simd_himask_2,1,bit00224466_1,bit11335577_1);\
115        s2p_step(s4,s5,simd_himask_2,1,bit00224466_2,bit11335577_2);\
116        s2p_step(s6,s7,simd_himask_2,1,bit00224466_3,bit11335577_3);\
117        s2p_step(bit00224466_0,bit00224466_1,simd_himask_4,2,bit00004444_0,bit22226666_0);\
118        s2p_step(bit00224466_2,bit00224466_3,simd_himask_4,2,bit00004444_1,bit22226666_1);\
119        s2p_step(bit11335577_0,bit11335577_1,simd_himask_4,2,bit11115555_0,bit33337777_0);\
120        s2p_step(bit11335577_2,bit11335577_3,simd_himask_4,2,bit11115555_1,bit33337777_1);\
121        s2p_step(bit00004444_0,bit00004444_1,simd_himask_8,4,p0,p4);\
122        s2p_step(bit11115555_0,bit11115555_1,simd_himask_8,4,p1,p5);\
123        s2p_step(bit22226666_0,bit22226666_1,simd_himask_8,4,p2,p6);\
124        s2p_step(bit33337777_0,bit33337777_1,simd_himask_8,4,p3,p7);\
125  } while(0)
126
[626]127/* For sizeof(SIMD_type) = 16 */ 
128typedef uint16_t BitPack;
[612]129
[626]130#define movemask_step(s0, s1, s2, s3, s4, s5, s6, s7, p) \
131  do { \
132        union { BitPack bit_pack[8];\
133                SIMD_type bit_block;\
134              } b;\
135        b.bit_pack[0] = simd_movemask_8(s7);\
136        b.bit_pack[1] = simd_movemask_8(s6);\
137        b.bit_pack[2] = simd_movemask_8(s5);\
138        b.bit_pack[3] = simd_movemask_8(s4);\
139        b.bit_pack[4] = simd_movemask_8(s3);\
140        b.bit_pack[5] = simd_movemask_8(s2);\
141        b.bit_pack[6] = simd_movemask_8(s1);\
142        b.bit_pack[7] = simd_movemask_8(s0);\
143        p = b.bit_block;\
144   } while (0)
145
146#define bitshift_step(s0, s1, s2, s3, s4, s5, s6, s7) \
147  do { \
148        s0 = simd_add_8(s0, s0);\
149        s1 = simd_add_8(s1, s1);\
150        s2 = simd_add_8(s2, s2);\
151        s3 = simd_add_8(s3, s3);\
152        s4 = simd_add_8(s4, s4);\
153        s5 = simd_add_8(s5, s5);\
154        s6 = simd_add_8(s6, s6);\
155        s7 = simd_add_8(s7, s7);\
156  } while (0)
157
158
159#define s2p_movemask(s0, s1, s2, s3, s4, s5, s6, s7, p0, p1, p2, p3, p4, p5, p6, p7) \
160  do { \
161        movemask_step(s0, s1, s2, s3, s4, s5, s6, s7, p0);\
162        bitshift_step(s0, s1, s2, s3, s4, s5, s6, s7);\
163        movemask_step(s0, s1, s2, s3, s4, s5, s6, s7, p1);\
164        bitshift_step(s0, s1, s2, s3, s4, s5, s6, s7);\
165        movemask_step(s0, s1, s2, s3, s4, s5, s6, s7, p2);\
166        bitshift_step(s0, s1, s2, s3, s4, s5, s6, s7);\
167        movemask_step(s0, s1, s2, s3, s4, s5, s6, s7, p3);\
168        bitshift_step(s0, s1, s2, s3, s4, s5, s6, s7);\
169        movemask_step(s0, s1, s2, s3, s4, s5, s6, s7, p4);\
170        bitshift_step(s0, s1, s2, s3, s4, s5, s6, s7);\
171        movemask_step(s0, s1, s2, s3, s4, s5, s6, s7, p5);\
172        bitshift_step(s0, s1, s2, s3, s4, s5, s6, s7);\
173        movemask_step(s0, s1, s2, s3, s4, s5, s6, s7, p6);\
174        bitshift_step(s0, s1, s2, s3, s4, s5, s6, s7);\
175        movemask_step(s0, s1, s2, s3, s4, s5, s6, s7, p7);\
176  } while (0)
177
178
[612]179#endif
180
Note: See TracBrowser for help on using the repository browser.