source: proto/u16u8/src/u16u8.c @ 471

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

Update u16u8 template and generated u16u8.c

File size: 20.4 KB
Line 
1#include <stdio.h>
2#include <stdlib.h>
3#include <errno.h>
4#include <stdint.h>
5#include <string.h>
6#include <sys/types.h>
7#include <sys/stat.h>
8
9#include "../lib/lib_simd.h"
10#include "../lib/block_carry.h"
11
12typedef SIMD_type BytePack;
13typedef SIMD_type BitBlock;
14
15
16// Profiling
17#define BUFFER_PROFILING
18#include "../lib/perflib/perfsec.h"
19
20#ifdef BUFFER_PROFILING
21BOM_Table * transcode_timer;
22#endif
23
24/*===========================================================================*/
25/* UErrorCode */
26/* Extracted from ICU */
27/*===========================================================================*/
28
29typedef enum UErrorCode {
30
31    U_ZERO_ERROR              =  0,     /**< No error, no warning. */
32    U_TRUNCATED_CHAR_FOUND    = 11,     /**< Character conversion: Incomplete input sequence. */
33    U_ILLEGAL_CHAR_FOUND      = 12,     /**< Character conversion: Illegal input sequence/combination of input units. */
34} UErrorCode;
35
36typedef  uint16_t UChar;
37
38#define s2p_step(s0,s1,hi_mask,shift,p0,p1)  \
39{ \
40BitBlock t0,t1; \
41t0= simd_pack_16_hh(s0,s1) ; \
42t1= simd_pack_16_ll(s0,s1) ; \
43p0= simd_if(hi_mask,t0,simd_srli_16(t1,shift) ) ; \
44p1= simd_if(hi_mask,simd_slli_16(t0,shift) ,t1) ; \
45} \
46
47#define s2p_bytepack(s0,s1,s2,s3,s4,s5,s6,s7,p0,p1,p2,p3,p4,p5,p6,p7)  \
48{BitBlock bit00224466_0,bit00224466_1,bit00224466_2,bit00224466_3; \
49BitBlock bit11335577_0,bit11335577_1,bit11335577_2,bit11335577_3; \
50BitBlock bit00004444_0,bit22226666_0,bit00004444_1,bit22226666_1; \
51BitBlock bit11115555_0,bit33337777_0,bit11115555_1,bit33337777_1; \
52s2p_step(s0,s1,simd_himask_2,1,bit00224466_0,bit11335577_0)  \
53s2p_step(s2,s3,simd_himask_2,1,bit00224466_1,bit11335577_1)  \
54s2p_step(s4,s5,simd_himask_2,1,bit00224466_2,bit11335577_2)  \
55s2p_step(s6,s7,simd_himask_2,1,bit00224466_3,bit11335577_3)  \
56s2p_step(bit00224466_0,bit00224466_1,simd_himask_4,2,bit00004444_0,bit22226666_0)  \
57s2p_step(bit00224466_2,bit00224466_3,simd_himask_4,2,bit00004444_1,bit22226666_1)  \
58s2p_step(bit11335577_0,bit11335577_1,simd_himask_4,2,bit11115555_0,bit33337777_0)  \
59s2p_step(bit11335577_2,bit11335577_3,simd_himask_4,2,bit11115555_1,bit33337777_1)  \
60s2p_step(bit00004444_0,bit00004444_1,simd_himask_8,4,p0,p4)  \
61s2p_step(bit11115555_0,bit11115555_1,simd_himask_8,4,p1,p5)  \
62s2p_step(bit22226666_0,bit22226666_1,simd_himask_8,4,p2,p6)  \
63s2p_step(bit33337777_0,bit33337777_1,simd_himask_8,4,p3,p7)  \
64} \
65
66#define p2s_step(p0,p1,hi_mask,shift,s0,s1)  \
67{ \
68BitBlock t0,t1; \
69t0= simd_if(hi_mask,p0,simd_srli_16(p1,shift) ) ; \
70t1= simd_if(hi_mask,simd_slli_16(p0,shift) ,p1) ; \
71s0= simd_mergeh_8(t0,t1) ; \
72s1= simd_mergel_8(t0,t1) ; \
73} \
74
75#define p2s_bytemerge(p0,p1,p2,p3,p4,p5,p6,p7,s0,s1,s2,s3,s4,s5,s6,s7)  \
76{ \
77BitBlock bit00004444_0,bit22226666_0,bit00004444_1,bit22226666_1; \
78BitBlock bit11115555_0,bit33337777_0,bit11115555_1,bit33337777_1; \
79BitBlock bit00224466_0,bit00224466_1,bit00224466_2,bit00224466_3; \
80BitBlock bit11335577_0,bit11335577_1,bit11335577_2,bit11335577_3; \
81p2s_step(p0,p4,simd_himask_8,4,bit00004444_0,bit00004444_1)  \
82p2s_step(p1,p5,simd_himask_8,4,bit11115555_0,bit11115555_1)  \
83p2s_step(p2,p6,simd_himask_8,4,bit22226666_0,bit22226666_1)  \
84p2s_step(p3,p7,simd_himask_8,4,bit33337777_0,bit33337777_1)  \
85p2s_step(bit00004444_0,bit22226666_0,simd_himask_4,2,bit00224466_0,bit00224466_1)  \
86p2s_step(bit11115555_0,bit33337777_0,simd_himask_4,2,bit11335577_0,bit11335577_1)  \
87p2s_step(bit00004444_1,bit22226666_1,simd_himask_4,2,bit00224466_2,bit00224466_3)  \
88p2s_step(bit11115555_1,bit33337777_1,simd_himask_4,2,bit11335577_2,bit11335577_3)  \
89p2s_step(bit00224466_0,bit11335577_0,simd_himask_2,1,s0,s1)  \
90p2s_step(bit00224466_1,bit11335577_1,simd_himask_2,1,s2,s3)  \
91p2s_step(bit00224466_2,bit11335577_2,simd_himask_2,1,s4,s5)  \
92p2s_step(bit00224466_3,bit11335577_3,simd_himask_2,1,s6,s7)  \
93} \
94
95
96static inline void interleave4(BitBlock p0, BitBlock p1, BitBlock p2, BitBlock p3, 
97                               BitBlock& s0, BitBlock& s1, BitBlock& s2, BitBlock& s3);
98
99static inline void interleave4(BitBlock p0, BitBlock p1, BitBlock p2, BitBlock p3, 
100                               BitBlock& s0, BitBlock& s1, BitBlock& s2, BitBlock& s3){
101        BitBlock hpair0 = simd_mergel_1(p1, p0);
102        BitBlock hpair1 = simd_mergeh_1(p1, p0);
103        BitBlock lpair0 = simd_mergel_1(p3, p2);
104        BitBlock lpair1 = simd_mergeh_1(p3, p2);
105        s0 = simd_mergel_2(lpair0,hpair0);
106        s1 = simd_mergeh_2(lpair0,hpair0);
107        s2 = simd_mergel_2(lpair1,hpair1);
108        s3 = simd_mergeh_2(lpair1,hpair1);
109}
110
111
112
113
114#define do_right16_shifts(vec,rshift1,rshift2,rshift4,rshift8)  \
115{BitBlock s2; \
116vec= simd_sub_16(vec,simd_srli_16(simd_and(rshift1,vec) ,1) ) ; \
117s2= simd_and(rshift2,vec) ; \
118vec= simd_or(simd_srli_16(s2,2) ,simd_xor(vec,s2) ) ; \
119s2= simd_and(rshift4,vec) ; \
120vec= simd_or(simd_srli_16(s2,4) ,simd_xor(vec,s2) ) ; \
121s2= simd_and(rshift8,vec) ; \
122vec= simd_or(simd_srli_16(s2,8) ,simd_xor(vec,s2) ) ; \
123}
124
125static inline void bit_del_16(BitBlock &s, BitBlock delmask){
126        BitBlock del16_rshift1;
127        BitBlock del16_rshift2;
128        BitBlock del16_rshift4;
129        BitBlock del16_rshift8;
130       
131        BitBlock del16_trans2;
132        BitBlock del16_trans4;
133        BitBlock del16_trans8;
134        BitBlock shift_bits;
135       
136        del16_rshift1= simd_xor(simd_slli_16(delmask,1),simd_slli_16(delmask,2));
137        del16_rshift1= simd_xor(del16_rshift1,simd_slli_16(del16_rshift1,2));
138        del16_rshift1= simd_xor(del16_rshift1,simd_slli_16(del16_rshift1,4));
139        del16_rshift1= simd_xor(del16_rshift1,simd_slli_16(del16_rshift1,8));
140       
141        del16_trans2= simd_and(del16_rshift1,delmask);
142        del16_rshift2= simd_xor(simd_slli_16(del16_trans2,1),simd_slli_16(del16_trans2,2));
143        del16_rshift2= simd_xor(del16_rshift2,simd_slli_16(del16_rshift2,2));
144        del16_rshift2= simd_xor(del16_rshift2,simd_slli_16(del16_rshift2,4));
145        del16_rshift2= simd_xor(del16_rshift2,simd_slli_16(del16_rshift2,8));
146       
147        del16_trans4= simd_and(del16_rshift2,del16_trans2);
148        del16_rshift4= simd_xor(simd_slli_16(del16_trans4,1),simd_slli_16(del16_trans4,2));
149        del16_rshift4= simd_xor(del16_rshift4,simd_slli_16(del16_rshift4,2));
150        del16_rshift4= simd_xor(del16_rshift4,simd_slli_16(del16_rshift4,4));
151        del16_rshift4= simd_xor(del16_rshift4,simd_slli_16(del16_rshift4,8));
152       
153        del16_trans8= simd_and(del16_rshift4,del16_trans4);
154        del16_rshift8= simd_xor(simd_slli_16(del16_trans8,1),simd_slli_16(del16_trans8,2));
155        del16_rshift8= simd_xor(del16_rshift8,simd_slli_16(del16_rshift8,2));
156        del16_rshift8= simd_xor(del16_rshift8,simd_slli_16(del16_rshift8,4));
157        del16_rshift8= simd_xor(del16_rshift8,simd_slli_16(del16_rshift8,8));
158       
159        del16_rshift1= simd_andc(del16_rshift1,delmask);
160        del16_rshift2= simd_andc(del16_rshift2,delmask);
161        del16_rshift4= simd_andc(del16_rshift4,delmask);
162        del16_rshift8= simd_andc(del16_rshift8,delmask);
163       
164        del16_rshift2= simd_sub_16(del16_rshift2,simd_srli_16(simd_and(del16_rshift1,del16_rshift2),1));
165       
166        del16_rshift4= simd_sub_16(del16_rshift4,simd_srli_16(simd_and(del16_rshift1,del16_rshift4),1));
167        shift_bits= simd_and(del16_rshift2,del16_rshift4);
168        del16_rshift4= simd_or(simd_srli_16(shift_bits,2),simd_xor(del16_rshift4,shift_bits));
169       
170        del16_rshift8= simd_sub_16(del16_rshift8,simd_srli_16(simd_and(del16_rshift1,del16_rshift8),1));
171        shift_bits= simd_and(del16_rshift2,del16_rshift8);
172        del16_rshift8= simd_or(simd_srli_16(shift_bits,2),simd_xor(del16_rshift8,shift_bits)); 
173        shift_bits= simd_and(del16_rshift4,del16_rshift8);
174        del16_rshift8= simd_or(simd_srli_16(shift_bits,4),simd_xor(del16_rshift8,shift_bits));
175       
176        s = simd_andc(s, delmask);
177        do_right16_shifts (s, del16_rshift1, del16_rshift2, del16_rshift4,  del16_rshift8)
178
179}
180
181
182static inline void del_count(BitBlock delmask, short *u8_bytes_per_reg){
183
184BitBlock delcounts_2;
185BitBlock delcounts_4;
186BitBlock delcounts_8;
187BitBlock delcounts_16;
188
189delcounts_2 = simd_add_2_lh(delmask, delmask);
190delcounts_4 = simd_add_4_lh(delcounts_2, delcounts_2);
191delcounts_8 = simd_add_8_lh(delcounts_4, delcounts_4);
192delcounts_16 = simd_add_16_lh(delcounts_8, delcounts_8);
193sisd_store_aligned(simd_sub_16(simd_const_16(16), delcounts_16), (BytePack *) &u8_bytes_per_reg[0]);
194}
195
196
197
198#define BLOCK_SIZE 128
199
200void u16u8(char ** targetbuf, const char * targetlimit, const UChar ** srcbuf, const UChar * srclimit, UErrorCode * err){
201
202  BitBlock hcarry;
203BitBlock array_u16l__4_;
204BitBlock array_u16h__4_;
205BitBlock array_u8_butlast__5_;
206BitBlock strct_u16__utf8_3_;
207BitBlock array_u8_butlast__2_;
208BitBlock array_u8_last__5_;
209BitBlock Temp37;
210BitBlock array_u16l__0_;
211BitBlock efghi_e;
212BitBlock strct_u16__ASCII_;
213BitBlock array_u8_last__1_;
214BitBlock array_u8_pre__3_;
215BitBlock Temp31;
216BitBlock Temp30;
217BitBlock Temp33;
218BitBlock Temp32;
219BitBlock Temp35;
220BitBlock Temp34;
221BitBlock array_u8_butlast__6_;
222BitBlock Temp36;
223BitBlock array_u16h__0_;
224BitBlock array_u16l__1_;
225BitBlock strct_u16__surrogate_;
226BitBlock array_u8_last__0_;
227BitBlock array_u16l__7_;
228BitBlock array_u8_last__4_;
229BitBlock gcarry;
230BitBlock array_u8_butlast__3_;
231BitBlock strct_u16__error_;
232BitBlock array_u8_pre__7_;
233BitBlock strct_u16__lsurrogate_;
234BitBlock array_u8_butlast__7_;
235BitBlock array_u16h__7_;
236BitBlock array_u8_pre__4_;
237BitBlock Temp17;
238BitBlock array_u16h__3_;
239BitBlock array_u8_pre__0_;
240BitBlock array_u16l__3_;
241BitBlock array_u8_last__3_;
242BitBlock array_u16l__6_;
243BitBlock u8_2or3;
244BitBlock array_u16h__6_;
245BitBlock Temp13;
246BitBlock Temp12;
247BitBlock Temp11;
248BitBlock Temp10;
249BitBlock array_u8_butlast__4_;
250BitBlock Temp16;
251BitBlock Temp15;
252BitBlock Temp14;
253BitBlock array_u8_butlast__0_;
254BitBlock array_u8_last__7_;
255BitBlock Temp19;
256BitBlock Temp18;
257BitBlock array_u16l__2_;
258BitBlock array_u8_pre__1_;
259BitBlock array_u16l__5_;
260BitBlock array_u8_pre__5_;
261BitBlock array_u16h__2_;
262BitBlock Temp5;
263BitBlock array_u8_last__2_;
264BitBlock array_u16h__5_;
265BitBlock above_0x7F;
266BitBlock above_0x7FF;
267BitBlock strct_u16__hsurrogate_;
268BitBlock efghi_i;
269BitBlock efghi_h;
270BitBlock efghi_g;
271BitBlock efghi_f;
272BitBlock array_u8_last__6_;
273BitBlock array_u8_butlast__1_;
274BitBlock AllOne = simd_const_1(1);
275BitBlock AllZero = simd_const_1(0);
276BitBlock Temp28;
277BitBlock Temp29;
278BitBlock array_u8_pre__2_;
279BitBlock Temp22;
280BitBlock Temp23;
281BitBlock Temp20;
282BitBlock Temp21;
283BitBlock Temp26;
284BitBlock strct_u16__utf8_2_;
285BitBlock Temp24;
286BitBlock Temp25;
287BitBlock Temp3;
288BitBlock Temp2;
289BitBlock Temp1;
290BitBlock Temp27;
291BitBlock Temp7;
292BitBlock Temp6;
293BitBlock array_u16h__1_;
294BitBlock Temp4;
295BitBlock array_u8_pre__6_;
296BitBlock Temp9;
297BitBlock Temp8;
298CarryType carry1 = Carry0;
299CarryType carry0 = Carry0;
300CarryType carry2 = Carry0;
301
302
303SIMD_type u16l [8];
304SIMD_type u16h [8];
305
306
307  BytePack * U16;
308  BytePack U16h[8];
309  BytePack U16l[8];
310
311  BitBlock doublepair[32];
312
313  BitBlock endmask;
314
315  BitBlock delmask[4];
316
317  BitBlock U8[32];
318
319  int target_bckup_steps = 0;
320  int pos = 0;
321
322  intptr_t  units_remaining  = 0;
323  intptr_t  units_read  = 0;
324
325  bool error_found = false;
326  int err_pos;
327
328 
329
330  do {
331
332    U16 = (BytePack *)(*srcbuf);
333    units_remaining = srclimit-(*srcbuf);
334
335    if(units_remaining < BLOCK_SIZE){
336        endmask = sisd_sll(simd_const_1(1),sisd_from_int((int) units_remaining));
337        units_read = units_remaining;
338
339//      ((uint16_t *) U16)[units_read/2] = 0;
340    }
341    else {
342        endmask = simd_const_1(0);
343        units_read = BLOCK_SIZE;
344    }
345
346    for (int i=0; i< 8; i++){
347      U16h[i] = simd_pack_16_ll(sisd_load_unaligned(&U16[i*2+1]),sisd_load_unaligned(&U16[i*2]));
348      U16l[i] = simd_pack_16_hh(sisd_load_unaligned(&U16[i*2+1]),sisd_load_unaligned(&U16[i*2]));
349    }
350
351
352    s2p_bytepack(U16h[7], U16h[6], U16h[5], U16h[4], U16h[3], U16h[2], U16h[1], U16h[0],
353    u16h[0], u16h[1], u16h[2], u16h[3], u16h[4], u16h[5], u16h[6], u16h[7]);
354
355    s2p_bytepack(U16l[7], U16l[6], U16l[5], U16l[4], U16l[3], U16l[2], U16l[1], U16l[0],
356    u16l[0], u16l[1], u16l[2], u16l[3], u16l[4], u16l[5], u16l[6], u16l[7]);
357
358array_u16l__0_ = u16l [0];
359array_u16l__1_ = u16l [1];
360array_u16l__2_ = u16l [2];
361array_u16l__3_ = u16l [3];
362array_u16l__4_ = u16l [4];
363array_u16l__5_ = u16l [5];
364array_u16l__6_ = u16l [6];
365array_u16l__7_ = u16l [7];
366
367array_u16h__0_ = u16h [0];
368array_u16h__1_ = u16h [1];
369array_u16h__2_ = u16h [2];
370array_u16h__3_ = u16h [3];
371array_u16h__4_ = u16h [4];
372array_u16h__5_ = u16h [5];
373array_u16h__6_ = u16h [6];
374array_u16h__7_ = u16h [7];
375
376    Temp1 = simd_or(array_u16h__0_,array_u16h__1_);
377Temp2 = simd_or(array_u16h__2_,array_u16h__3_);
378Temp3 = simd_or(Temp1,Temp2);
379above_0x7FF = simd_or(Temp3,array_u16h__4_);
380Temp4 = simd_or(above_0x7FF,array_u16h__5_);
381Temp5 = simd_or(array_u16h__6_,array_u16h__7_);
382Temp6 = simd_or(Temp4,Temp5);
383above_0x7F = simd_or(Temp6,array_u16l__0_);
384Temp7 = simd_and(array_u16h__0_,array_u16h__1_);
385Temp8 = simd_andc(array_u16h__3_,array_u16h__2_);
386Temp9 = simd_and(Temp7,Temp8);
387strct_u16__surrogate_ = simd_and(Temp9,array_u16h__4_);
388strct_u16__hsurrogate_ = simd_andc(strct_u16__surrogate_,array_u16h__5_);
389strct_u16__lsurrogate_ = simd_and(strct_u16__surrogate_,array_u16h__5_);
390strct_u16__utf8_3_ = simd_andc(above_0x7FF,strct_u16__surrogate_);
391strct_u16__utf8_2_ = simd_andc(above_0x7F,above_0x7FF);
392strct_u16__ASCII_ = simd_andc(AllOne,above_0x7F);
393advance_with_carry(strct_u16__hsurrogate_, carry0, Temp10);
394strct_u16__error_ = simd_xor(Temp10,strct_u16__lsurrogate_);
395efghi_i = simd_andc(AllOne,array_u16l__1_);
396efghi_h = simd_xor(array_u16l__0_,array_u16l__1_);
397hcarry = simd_andc(array_u16l__0_,efghi_h);
398efghi_g = simd_xor(array_u16h__7_,hcarry);
399gcarry = simd_andc(array_u16h__7_,efghi_g);
400efghi_f = simd_xor(array_u16h__6_,gcarry);
401efghi_e = simd_andc(array_u16h__6_,efghi_f);
402array_u8_last__0_ = simd_andc(AllOne,strct_u16__ASCII_);
403array_u8_last__1_ = simd_and(strct_u16__ASCII_,array_u16l__1_);
404Temp11 = simd_andc(array_u16l__2_,strct_u16__hsurrogate_);
405Temp12 = simd_and(efghi_h,strct_u16__hsurrogate_);
406array_u8_last__2_ = simd_or(Temp11,Temp12);
407Temp13 = simd_andc(array_u16l__3_,strct_u16__hsurrogate_);
408Temp14 = simd_and(efghi_i,strct_u16__hsurrogate_);
409array_u8_last__3_ = simd_or(Temp13,Temp14);
410Temp15 = simd_andc(array_u16l__4_,strct_u16__hsurrogate_);
411Temp16 = simd_and(array_u16l__2_,strct_u16__hsurrogate_);
412array_u8_last__4_ = simd_or(Temp15,Temp16);
413Temp17 = simd_andc(array_u16l__5_,strct_u16__hsurrogate_);
414Temp18 = simd_and(array_u16l__3_,strct_u16__hsurrogate_);
415array_u8_last__5_ = simd_or(Temp17,Temp18);
416Temp19 = simd_andc(array_u16l__6_,strct_u16__hsurrogate_);
417Temp20 = simd_and(array_u16l__4_,strct_u16__hsurrogate_);
418array_u8_last__6_ = simd_or(Temp19,Temp20);
419Temp21 = simd_andc(array_u16l__7_,strct_u16__hsurrogate_);
420Temp22 = simd_and(array_u16l__5_,strct_u16__hsurrogate_);
421array_u8_last__7_ = simd_or(Temp21,Temp22);
422u8_2or3 = simd_or(strct_u16__utf8_2_,strct_u16__utf8_3_);
423array_u8_butlast__0_ = simd_andc(AllOne,strct_u16__ASCII_);
424array_u8_butlast__1_ = simd_or(strct_u16__utf8_2_,strct_u16__hsurrogate_);
425Temp23 = simd_and(strct_u16__utf8_3_,array_u16h__4_);
426Temp24 = simd_or(strct_u16__hsurrogate_,Temp23);
427advance_with_carry(array_u16l__6_, carry1, Temp25);
428Temp26 = simd_and(strct_u16__lsurrogate_,Temp25);
429array_u8_butlast__2_ = simd_or(Temp24,Temp26);
430Temp27 = simd_and(u8_2or3,array_u16h__5_);
431Temp28 = simd_or(strct_u16__hsurrogate_,Temp27);
432advance_with_carry(array_u16l__7_, carry2, Temp29);
433Temp30 = simd_and(strct_u16__lsurrogate_,Temp29);
434array_u8_butlast__3_ = simd_or(Temp28,Temp30);
435Temp31 = simd_or(strct_u16__hsurrogate_,strct_u16__ASCII_);
436array_u8_butlast__4_ = simd_andc(array_u16h__6_,Temp31);
437Temp32 = simd_andc(array_u16h__7_,Temp31);
438Temp33 = simd_and(efghi_e,strct_u16__hsurrogate_);
439array_u8_butlast__5_ = simd_or(Temp32,Temp33);
440Temp34 = simd_andc(array_u16l__0_,Temp31);
441Temp35 = simd_and(efghi_f,strct_u16__hsurrogate_);
442array_u8_butlast__6_ = simd_or(Temp34,Temp35);
443Temp36 = simd_andc(array_u16l__1_,Temp31);
444Temp37 = simd_and(efghi_g,strct_u16__hsurrogate_);
445array_u8_butlast__7_ = simd_or(Temp36,Temp37);
446array_u8_pre__0_ = simd_andc(above_0x7FF,strct_u16__surrogate_);
447array_u8_pre__1_ = simd_andc(above_0x7FF,strct_u16__surrogate_);
448array_u8_pre__2_ = simd_andc(above_0x7FF,strct_u16__surrogate_);
449array_u8_pre__3_ = AllZero;
450array_u8_pre__4_ = simd_and(strct_u16__utf8_3_,array_u16h__0_);
451array_u8_pre__5_ = simd_and(strct_u16__utf8_3_,array_u16h__1_);
452array_u8_pre__6_ = simd_and(strct_u16__utf8_3_,array_u16h__2_);
453array_u8_pre__7_ = simd_and(strct_u16__utf8_3_,array_u16h__3_);
454
455
456/* Convert flattened array values to arrays */
457SIMD_type u8_pre [8];
458u8_pre[0] = array_u8_pre__0_;
459u8_pre[1] = array_u8_pre__1_;
460u8_pre[2] = array_u8_pre__2_;
461u8_pre[3] = array_u8_pre__3_;
462u8_pre[4] = array_u8_pre__4_;
463u8_pre[5] = array_u8_pre__5_;
464u8_pre[6] = array_u8_pre__6_;
465u8_pre[7] = array_u8_pre__7_;
466
467SIMD_type u8_butlast [8];
468u8_butlast[0] = array_u8_butlast__0_;
469u8_butlast[1] = array_u8_butlast__1_;
470u8_butlast[2] = array_u8_butlast__2_;
471u8_butlast[3] = array_u8_butlast__3_;
472u8_butlast[4] = array_u8_butlast__4_;
473u8_butlast[5] = array_u8_butlast__5_;
474u8_butlast[6] = array_u8_butlast__6_;
475u8_butlast[7] = array_u8_butlast__7_;
476
477SIMD_type u8_last [8];
478u8_last[0] = array_u8_last__0_;
479u8_last[1] = array_u8_last__1_;
480u8_last[2] = array_u8_last__2_;
481u8_last[3] = array_u8_last__3_;
482u8_last[4] = array_u8_last__4_;
483u8_last[5] = array_u8_last__5_;
484u8_last[6] = array_u8_last__6_;
485u8_last[7] = array_u8_last__7_;
486
487
488    if (bitblock_has_bit(strct_u16__error_)) {
489       
490       err_pos = count_forward_zeroes(strct_u16__error_);
491       error_found = true;
492        if ((err_pos == units_read)) {
493                err_pos--;
494                *err = U_TRUNCATED_CHAR_FOUND;
495                (*srcbuf) += err_pos;
496                if(err_pos<0){
497                        target_bckup_steps = 2;
498                        (*targetbuf) -= target_bckup_steps;
499                        return;
500                }               
501        }
502        else {
503          if((((unsigned char *)U16h)[err_pos]< 0xDC) || (((unsigned char *)U16h)[err_pos] >= 0xE0))
504                err_pos--;
505                *err = U_ILLEGAL_CHAR_FOUND;
506                (*srcbuf) += err_pos;
507                if(err_pos<0){
508                        target_bckup_steps = 2;
509                        (*targetbuf) -= target_bckup_steps;
510                        return;
511                }
512        }
513        endmask = sisd_sll(simd_const_1(1),sisd_from_int(err_pos));
514    }
515
516    for (int i=0; i< 8; i++){
517        interleave4(simd_const_8(0), u8_pre[i], u8_butlast[i], u8_last[i],
518                                doublepair[i*4], doublepair[i*4+1], 
519                                doublepair[i*4+2], doublepair[i*4+3]);
520    }
521
522        interleave4(simd_const_1(1), simd_or(endmask, simd_not(strct_u16__utf8_3_)), 
523                                simd_or(endmask, strct_u16__ASCII_), endmask,
524                                delmask[0], delmask[1], delmask[2], delmask[3]);
525
526
527     for (int i=0; i< 32; i=i+4){
528        bit_del_16(doublepair[i],delmask[0]);
529        bit_del_16(doublepair[i+1],delmask[1]);
530        bit_del_16(doublepair[i+2],delmask[2]);
531        bit_del_16(doublepair[i+3],delmask[3]);
532     }
533
534    short u8_bytes_per_reg[8];
535
536    for (int i=0; i< 4; i++){
537      p2s_bytemerge(doublepair[i],doublepair[4+i],doublepair[8+i],doublepair[12+i],doublepair[16+i],doublepair[20+i],doublepair[24+i],doublepair[28+i],
538                        U8[i*8+7],U8[i*8+6],U8[i*8+5],U8[i*8+4],U8[i*8+3],U8[i*8+2],U8[i*8+1],U8[i*8]);
539       for(int k=0; k<8; k++) u8_bytes_per_reg[k] = 0;
540       del_count(delmask[i],u8_bytes_per_reg);
541 
542       for(int j=0; j<8; j++){
543        sisd_store_unaligned(U8[i*8+j],(SIMD_type *) (*targetbuf));
544        *targetbuf += u8_bytes_per_reg[j];
545       }
546    }
547
548    if(!error_found) (*srcbuf) += units_read;
549
550    } while ((units_read == BLOCK_SIZE) && !error_found);
551
552}
553
554
555
556
557int
558main(int argc, char * argv[]) {
559        char * infilename, * outfilename;       
560        FILE *infile, *outfile;
561        struct stat fileinfo;
562        const UChar * srcbuf;
563        const UChar * srclimit;
564        const char * targetlimit;
565        char * targetbuf;
566        char * target_head;
567        UErrorCode status;
568        int chars_read;
569
570        if (argc < 2) {
571                printf("Usage: %s <filename> [<outputfile>]\n", argv[0]);
572                exit(-1);
573        }
574
575        infilename = argv[1];
576        stat(infilename, &fileinfo);
577        infile = fopen(infilename, "rb");
578        if (!infile) {
579                fprintf(stderr, "Error: cannot open %s for input.\n", infilename);
580                exit(-1);
581        }
582       
583        if (argc < 3) outfile = stdout;
584        else {
585                outfilename = argv[2];
586                outfile = fopen(outfilename, "wb");
587                if (!outfile) {
588                        fprintf(stderr, "Error: cannot open %s for writing.\n", outfilename);
589                        exit(-1);
590                }
591        }
592
593
594        srcbuf = (UChar *) malloc(fileinfo.st_size+1);
595        if (!srcbuf) {
596              fprintf(stderr, "Error: buffer for %s of size %zu cannot be created.\n", infilename, fileinfo.st_size+1);
597              exit(-1);
598        }
599
600        chars_read = fread((void *)srcbuf, 1, fileinfo.st_size, infile);
601
602        srclimit = srcbuf + chars_read/2;
603//      (*srcbuf)[fileinfo.st_size] = '\0';
604        fclose(infile);
605
606        targetbuf = (char *) malloc(chars_read*2);
607        targetlimit = targetbuf + chars_read*2;
608        target_head = targetbuf;
609
610        u16u8(&targetbuf, targetlimit, &srcbuf, srclimit, &status);
611        fwrite(target_head , 1 ,  targetbuf - target_head, outfile );
612
613        if (status == U_ILLEGAL_CHAR_FOUND) {
614                fprintf(stderr, "Illegal UTF-16 sequence at position %lu in source.\n", chars_read-(srclimit-srcbuf)*2);
615        }
616        else if (status == U_TRUNCATED_CHAR_FOUND) {
617                fprintf(stderr, "EOF with incomplete UTF-16 sequence at position %lu in source.\n",chars_read-(srclimit-srcbuf)*2);
618        }
619
620        fclose(outfile);
621
622
623
624        return(0);
625}
Note: See TracBrowser for help on using the repository browser.