source: proto/Compiler/template.c @ 390

Last change on this file since 390 was 390, checked in by eamiri, 9 years ago

adc128 changed

File size: 4.9 KB
Line 
1#include <stdio.h>
2#include <stdlib.h>
3#include <errno.h>
4#include "sse_simd.h"
5
6
7typedef SIMD_type BytePack;
8typedef SIMD_type BitBlock;
9
10
11// Profiling
12
13#ifdef BUFFER_PROFILING
14#include "../Profiling/BOM_Profiler.c"
15BOM_Table * transcode_timer;
16#endif
17
18
19#define s2p_step(s0,s1,hi_mask,shift,p0,p1)  \
20{ \
21BitBlock t0,t1; \
22t0= simd_pack_16_hh(s0,s1) ; \
23t1= simd_pack_16_ll(s0,s1) ; \
24p0= simd_if(hi_mask,t0,simd_srli_16(t1,shift) ) ; \
25p1= simd_if(hi_mask,simd_slli_16(t0,shift) ,t1) ; \
26} \
27
28#define s2p_bytepack(s0,s1,s2,s3,s4,s5,s6,s7,p0,p1,p2,p3,p4,p5,p6,p7)  \
29{BitBlock bit00224466_0,bit00224466_1,bit00224466_2,bit00224466_3; \
30BitBlock bit11335577_0,bit11335577_1,bit11335577_2,bit11335577_3; \
31BitBlock bit00004444_0,bit22226666_0,bit00004444_1,bit22226666_1; \
32BitBlock bit11115555_0,bit33337777_0,bit11115555_1,bit33337777_1; \
33s2p_step(s0,s1,mask_2,1,bit00224466_0,bit11335577_0)  \
34s2p_step(s2,s3,mask_2,1,bit00224466_1,bit11335577_1)  \
35s2p_step(s4,s5,mask_2,1,bit00224466_2,bit11335577_2)  \
36s2p_step(s6,s7,mask_2,1,bit00224466_3,bit11335577_3)  \
37s2p_step(bit00224466_0,bit00224466_1,mask_4,2,bit00004444_0,bit22226666_0)  \
38s2p_step(bit00224466_2,bit00224466_3,mask_4,2,bit00004444_1,bit22226666_1)  \
39s2p_step(bit11335577_0,bit11335577_1,mask_4,2,bit11115555_0,bit33337777_0)  \
40s2p_step(bit11335577_2,bit11335577_3,mask_4,2,bit11115555_1,bit33337777_1)  \
41s2p_step(bit00004444_0,bit00004444_1,mask_8,4,p0,p4)  \
42s2p_step(bit11115555_0,bit11115555_1,mask_8,4,p1,p5)  \
43s2p_step(bit22226666_0,bit22226666_1,mask_8,4,p2,p6)  \
44s2p_step(bit33337777_0,bit33337777_1,mask_8,4,p3,p7)  \
45} \
46
47
48
49
50#define double_int64_adc(x1, x2, y1, y2, rslt1, rslt2, carry) \
51  __asm__  ("sahf\n\t" \
52        "adc %[e1], %[z1]\n\t" \
53        "adc %[e2], %[z2]\n\t" \
54        "lahf\n\t" \
55     : [z1] "=r" (rslt1), [z2] "=r" (rslt2), [carryflag] "=a" (carry) \
56         : "[z1]" (x1), "[z2]" (x2), \
57           [e1] "r" (y1), [e2] "r" (y2), \
58           "[carryflag]" (carry) \
59         : "cc")
60
61#define adc128(first, second, carry, sum) \
62do\
63{\
64  union {__m128i bitblock;\
65         uint64_t int64[2];} rslt;\
66\
67  union {__m128i bitblock;\
68         uint64_t int64[2];} x;\
69\
70  union {__m128i bitblock;\
71         uint64_t int64[2];} y;\
72\
73  x.bitblock = first;\
74  y.bitblock = second;\
75\
76  double_int64_adc(x.int64[0], x.int64[1], y.int64[0], y.int64[1], rslt.int64[0], rslt.int64[1], carry);\
77  sum = rslt.bitblock;\
78}while(0)
79
80
81
82#define double_int64_sbb(x1, x2, y1, y2, rslt1, rslt2, carry) \
83  __asm__  ("sahf\n\t" \
84        "sbb %[e1], %[z1]\n\t" \
85        "sbb %[e2], %[z2]\n\t" \
86        "lahf\n\t" \
87     : [z1] "=r" (rslt1), [z2] "=r" (rslt2), [carryflag] "=a" (carry) \
88         : "[z1]" (x1), "[z2]" (x2), \
89           [e1] "r" (y1), [e2] "r" (y2), \
90           "[carryflag]" (carry) \
91         : "cc")
92
93#define sbb128(first, second, carry, sum) \
94do\
95{ union {__m128i bitblock;\
96         uint64_t int64[2];} rslt;\
97\
98  union {__m128i bitblock;\
99         uint64_t int64[2];} x;\
100\
101  union {__m128i bitblock;\
102         uint64_t int64[2];} y;\
103\
104  x.bitblock = first;\
105  y.bitblock = second;\
106\
107  double_int64_sbb(x.int64[0], x.int64[1], y.int64[0], y.int64[1], \
108                   rslt.int64[0], rslt.int64[1], carry);\
109  sum = rslt.bitblock;\
110}while(0)
111
112       
113           
114#define BLOCK_SIZE 128
115
116void do_process(FILE *infile, FILE *outfile) {
117
118  @decl
119
120  BytePack U8[8];
121//  BitBlock u8bit[8];
122
123  int block_pos = 0;
124
125  int chars_read = fread(&U8[0], 1, BLOCK_SIZE, infile);
126  while (chars_read > 0) {
127    for (int i = chars_read;i < BLOCK_SIZE; i++) {
128       ((char *) &U8[0])[i] = 0;
129    }
130    BitBlock mask_2 = simd_himask_2;
131    BitBlock mask_4 = simd_himask_4;
132    BitBlock mask_8 = simd_himask_8;
133    s2p_bytepack(U8[7], U8[6], U8[5], U8[4], U8[3], U8[2], U8[1], U8[0],
134    array_u8bit__0_,array_u8bit__1_,array_u8bit__2_,array_u8bit__3_,array_u8bit__4_,array_u8bit__5_,array_u8bit__6_,array_u8bit__7_);
135
136    @stmts
137
138    if (bitblock_has_bit(error_mask)) {
139      int errpos = count_forward_zeroes(error_mask);
140      fprintf(stderr, "Error at byte #%i\n", block_pos + errpos);
141    }
142    block_pos += BLOCK_SIZE;
143    chars_read = fread(&U8[0], 1, BLOCK_SIZE, infile);
144  }
145
146  fclose(infile);
147  fclose(outfile);
148}
149
150
151
152
153int
154main(int argc, char * argv[]) {
155  if (argc < 2) {
156    printf("Usage: %s <filename> [<outputfile>]\n", argv[0]);
157          exit(-1);
158  }
159  char * filename = argv[1];
160#ifdef BUFFER_PROFILING
161  transcode_timer = init_BOM_timer(BUFFER_SIZE);
162#endif
163  FILE *infile, *outfile;
164  infile = fopen(filename, "rb");
165  if (!infile) {
166      fprintf(stderr, "Error: cannot open %s for input.\n", filename);
167      exit(-1);
168  }
169
170  if (argc < 3) outfile = stdout;
171  else {
172    outfile = fopen(argv[2], "wb");
173    if (!outfile) {
174      fprintf(stderr, "Error: cannot open %s for writing.\n", argv[2]);
175      exit(-1);
176    }
177  }
178
179  do_process(infile, outfile);
180
181#ifdef BUFFER_PROFILING
182  printf("Buffer conversion timing.\n");
183  dump_BOM_table(transcode_timer);
184#endif
185  return(0);
186}
Note: See TracBrowser for help on using the repository browser.