source: proto/parabix2/compiled/block_carry.h @ 454

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

Generate advance_with_carry in all modes

File size: 3.4 KB
Line 
1/*  Block Addition, Subtraction and Shifts with Carry
2    Copyright (C) 2010, Robert D. Cameron
3    Licensed to the public under the Open Software License 3.0.
4    Licensed to International Characters Inc.
5       under the Academic Free License version 3.0.
6*/
7#ifndef BLOCK_CARRY_H
8#define BLOCK_CARRY_H
9
10/*------------------------------------------------------------*/
11#include "sse_simd.h"
12
13#define ADC_64 1
14#define SIMD_CARRY 2
15#define CARRY_STRATEGY ADC_64
16
17#if (CARRY_STRATEGY == ADC_64)
18typedef uint64_t CarryType;
19
20#define Carry0 0
21
22#define double_int64_adc(x1, x2, y1, y2, rslt1, rslt2, carry) \
23  __asm__  ("sahf\n\t" \
24        "adc %[e1], %[z1]\n\t" \
25        "adc %[e2], %[z2]\n\t" \
26        "lahf\n\t" \
27     : [z1] "=r" (rslt1), [z2] "=r" (rslt2), [carryflag] "=a" (carry) \
28         : "[z1]" (x1), "[z2]" (x2), \
29           [e1] "r" (y1), [e2] "r" (y2), \
30           "[carryflag]" (carry) \
31         : "cc")
32
33#define adc128(first, second, carry, sum) \
34do\
35{\
36  union {__m128i bitblock;\
37         uint64_t int64[2];} rslt;\
38\
39  union {__m128i bitblock;\
40         uint64_t int64[2];} x;\
41\
42  union {__m128i bitblock;\
43         uint64_t int64[2];} y;\
44\
45  x.bitblock = first;\
46  y.bitblock = second;\
47\
48  double_int64_adc(x.int64[0], x.int64[1], y.int64[0], y.int64[1], rslt.int64[0], rslt.int64[1], carry);\
49  sum = rslt.bitblock;\
50}while(0)
51
52
53
54#define advance_with_carry(cursor, carry, rslt)\
55    adc128(cursor, cursor, carry, rslt)
56
57
58#define double_int64_sbb(x1, x2, y1, y2, rslt1, rslt2, carry) \
59  __asm__  ("sahf\n\t" \
60        "sbb %[e1], %[z1]\n\t" \
61        "sbb %[e2], %[z2]\n\t" \
62        "lahf\n\t" \
63     : [z1] "=r" (rslt1), [z2] "=r" (rslt2), [carryflag] "=a" (carry) \
64         : "[z1]" (x1), "[z2]" (x2), \
65           [e1] "r" (y1), [e2] "r" (y2), \
66           "[carryflag]" (carry) \
67         : "cc")
68
69#define sbb128(first, second, carry, sum) \
70do\
71{ union {__m128i bitblock;\
72         uint64_t int64[2];} rslt;\
73\
74  union {__m128i bitblock;\
75         uint64_t int64[2];} x;\
76\
77  union {__m128i bitblock;\
78         uint64_t int64[2];} y;\
79\
80  x.bitblock = first;\
81  y.bitblock = second;\
82\
83  double_int64_sbb(x.int64[0], x.int64[1], y.int64[0], y.int64[1], \
84                   rslt.int64[0], rslt.int64[1], carry);\
85  sum = rslt.bitblock;\
86}while(0)
87
88#endif
89
90#if (CARRY_STRATEGY == SIMD_CARRY)
91
92#typedef SIMD_type CarryType;
93
94#define Carry0 simd_const_1(0)
95
96#define adc128(x, y, carry,  sum) \
97do{ \
98  SIMD_type gen = simd_and(x, y); \
99  SIMD_type prop = simd_or(x, y); \
100  SIMD_type partial = simd_add_64(simd_add_64(x, y), carry); \
101  SIMD_type c1 = sisd_slli(simd_srli_64(simd_or(gen, simd_andc(prop, partial)), 63), 64); \
102  sum = simd_add_64(c1, partial); \
103  carry = sisd_srli(simd_or(gen, simd_andc(prop, sum)), 127); \
104} while(0)
105
106
107#define sbb128(x, y, borrow, difference) \
108do {\
109  SIMD_type gen = simd_andc(y, x); \
110  SIMD_type prop = simd_not(simd_xor(x, y)); \
111  SIMD_type partial = simd_sub_64(simd_sub_64(x, y), borrow); \
112  SIMD_type b1 = sisd_slli(simd_srli_64(simd_or(gen, simd_and(prop, partial)), 63), 64); \
113  difference = simd_sub_64(partial, b1); \
114  borrow = sisd_srli(simd_or(gen, simd_and(prop, difference)), 127); \
115}while(0)
116
117
118#define advance_with_carry(cursor, carry, rslt)\
119do{\
120  SIMD_type shift_out = simd_srli_64(cursor, 63);\
121  SIMD_type low_bits = simd_mergel_64(shift_out, carry);\
122  carry = sisd_srli(shift_out, 64);\
123  rslt = simd_or(simd_add_64(cursor, cursor), low_bits);\
124}while(0)
125
126#endif
127#endif
128
129
Note: See TracBrowser for help on using the repository browser.