source: trunk/lib/bitblock128.hpp @ 3449

Last change on this file since 3449 was 3449, checked in by cameron, 6 years ago

Add carryout2carry for legacy CCGO

File size: 6.6 KB
Line 
1#ifndef BITBLOCK128_HPP_
2#define BITBLOCK128_HPP_
3/*=============================================================================
4    bitblock128 - Specific 128 bit IDISA implementations.
5
6    Copyright (C) 2011, Robert D. Cameron, Kenneth S. Herdy, Hua Huang and Nigel Medforth.
7    Licensed to the public under the Open Software License 3.0.
8    Licensed to International Characters Inc.
9       under the Academic Free License version 3.0.
10
11=============================================================================*/
12
13#include "idisa128.hpp"
14#include "builtins.hpp"
15
16union ubitblock {
17        bitblock128_t _128;
18        uint64_t _64[sizeof(bitblock128_t)/sizeof(uint64_t)];
19        uint32_t _32[sizeof(bitblock128_t)/sizeof(uint32_t)];
20        uint16_t _16[sizeof(bitblock128_t)/sizeof(uint16_t)];
21        uint8_t _8[sizeof(bitblock128_t)/sizeof(uint8_t)];
22};
23
24static IDISA_ALWAYS_INLINE void add_ci_co(bitblock128_t x, bitblock128_t y, bitblock128_t carry_in, bitblock128_t & carry_out, bitblock128_t & sum);
25static IDISA_ALWAYS_INLINE void sub_bi_bo(bitblock128_t x, bitblock128_t y, bitblock128_t borrow_in, bitblock128_t & borrow_out, bitblock128_t & difference);
26static IDISA_ALWAYS_INLINE void adv_ci_co(bitblock128_t cursor, bitblock128_t carry_in, bitblock128_t & carry_out, bitblock128_t & rslt);
27
28/* The type used to store a carry bit. */
29typedef bitblock128_t carry_t;
30
31
32
33
34
35static IDISA_ALWAYS_INLINE bitblock128_t carry2bitblock(carry_t carry);
36static IDISA_ALWAYS_INLINE carry_t bitblock2carry(bitblock128_t carry);
37
38static IDISA_ALWAYS_INLINE carry_t carryout2carry(bitblock128_t carryout);
39
40static IDISA_ALWAYS_INLINE void adc(bitblock128_t x, bitblock128_t y, carry_t & carry, bitblock128_t & sum);
41static IDISA_ALWAYS_INLINE void sbb(bitblock128_t x, bitblock128_t y, carry_t & borrow, bitblock128_t & difference);
42static IDISA_ALWAYS_INLINE void advance_with_carry(bitblock128_t cursor, carry_t & carry, bitblock128_t & rslt);
43
44static IDISA_ALWAYS_INLINE void adc(bitblock128_t x, bitblock128_t y, carry_t carry_in, carry_t & carry_out, bitblock128_t & sum);
45static IDISA_ALWAYS_INLINE void sbb(bitblock128_t x, bitblock128_t y, carry_t borrow_in, carry_t & borrow_out, bitblock128_t & difference);
46static IDISA_ALWAYS_INLINE void advance_with_carry(bitblock128_t cursor, carry_t carry_in, carry_t & carry_out, bitblock128_t & rslt);
47
48static IDISA_ALWAYS_INLINE bitblock128_t convert (uint64_t s);
49static IDISA_ALWAYS_INLINE uint64_t convert (bitblock128_t v);
50
51static IDISA_ALWAYS_INLINE bitblock128_t carry2bitblock(carry_t carry) {  return carry;} 
52static IDISA_ALWAYS_INLINE carry_t bitblock2carry(bitblock128_t carry) {  return carry;}
53
54static IDISA_ALWAYS_INLINE carry_t carryout2carry(bitblock128_t carryout) {
55 return bitblock::srli<BLOCK_SIZE-1>(carryout);
56}
57
58
59
60static IDISA_ALWAYS_INLINE void add_ci_co(bitblock128_t x, bitblock128_t y, bitblock128_t carry_in, bitblock128_t & carry_out, bitblock128_t & sum) {
61        bitblock128_t gen = simd_and(x, y);
62        bitblock128_t prop = simd_or(x, y);
63        bitblock128_t partial = simd128<64>::add(simd128<64>::add(x, y), carry_in);
64        bitblock128_t c1 = simd128<128>::slli<64>(simd128<64>::srli<63>(simd_or(gen, simd_andc(prop, partial))));
65        sum = simd128<64>::add(c1, partial);
66        carry_out = simd_or(gen, simd_andc(prop, sum));
67}
68static IDISA_ALWAYS_INLINE void sub_bi_bo(bitblock128_t x, bitblock128_t y, bitblock128_t borrow_in, bitblock128_t & borrow_out, bitblock128_t & difference){
69        bitblock128_t gen = simd_andc(y, x);
70        bitblock128_t prop = simd_not(simd_xor(x, y));
71        bitblock128_t partial = simd128<64>::sub(simd128<64>::sub(x, y), borrow_in);
72        bitblock128_t b1 = simd128<128>::slli<64>(simd128<64>::srli<63>(simd_or(gen, simd_and(prop, partial))));
73        difference = simd128<64>::sub(partial, b1);
74        borrow_out = simd_or(gen, simd_and(prop, difference));
75}
76static IDISA_ALWAYS_INLINE void adv_ci_co(bitblock128_t cursor, bitblock128_t carry_in, bitblock128_t & carry_out, bitblock128_t & rslt){
77        bitblock128_t shift_out = simd128<64>::srli<63>(cursor);
78        bitblock128_t low_bits = esimd128<64>::mergel(shift_out, carry_in);
79        carry_out = cursor;
80        rslt = simd_or(simd128<64>::add(cursor, cursor), low_bits);
81}
82
83IDISA_ALWAYS_INLINE void adc(bitblock128_t x, bitblock128_t y, carry_t & carry, bitblock128_t & sum)
84{
85        bitblock128_t gen = simd_and(x, y);
86        bitblock128_t prop = simd_or(x, y);
87        bitblock128_t partial = simd128<64>::add(simd128<64>::add(x, y), carry2bitblock(carry));
88        bitblock128_t c1 = simd128<128>::slli<64>(simd128<64>::srli<63>(simd_or(gen, simd_andc(prop, partial))));
89        sum = simd128<64>::add(c1, partial);
90        carry = bitblock2carry(simd128<128>::srli<127>(simd_or(gen, simd_andc(prop, sum))));
91}
92
93IDISA_ALWAYS_INLINE void adc(bitblock128_t x, bitblock128_t y, carry_t carry_in, carry_t & carry_out, bitblock128_t & sum)
94{
95        bitblock128_t co;
96        add_ci_co(x, y, carry2bitblock(carry_in), co, sum);
97        carry_out = bitblock2carry(simd128<128>::srli<127>(co));
98}
99
100IDISA_ALWAYS_INLINE void sbb(bitblock128_t x, bitblock128_t y, carry_t & borrow, bitblock128_t & difference)
101{
102        bitblock128_t gen = simd_andc(y, x);
103        bitblock128_t prop = simd_not(simd_xor(x, y));
104        bitblock128_t partial = simd128<64>::sub(simd128<64>::sub(x, y), carry2bitblock(borrow));
105        bitblock128_t b1 = simd128<128>::slli<64>(simd128<64>::srli<63>(simd_or(gen, simd_and(prop, partial))));
106        difference = simd128<64>::sub(partial, b1);
107        borrow = bitblock2carry(simd128<128>::srli<127>(simd_or(gen, simd_and(prop, difference))));
108}
109
110IDISA_ALWAYS_INLINE void sbb(bitblock128_t x, bitblock128_t y, carry_t borrow_in, carry_t & borrow_out, bitblock128_t & difference)
111{
112        bitblock128_t bo;
113        sub_bi_bo(x, y, carry2bitblock(borrow_in), bo, difference);
114        borrow_out = bitblock2carry(simd128<128>::srli<127>(bo));
115}
116
117IDISA_ALWAYS_INLINE void advance_with_carry(bitblock128_t cursor, carry_t & carry, bitblock128_t & rslt)
118{
119bitblock128_t shift_out = simd128<64>::srli<63>(cursor);
120bitblock128_t low_bits = esimd128<64>::mergel(shift_out, carry2bitblock(carry));
121carry = bitblock2carry(simd128<128>::srli<64>(shift_out));
122rslt = simd_or(simd128<64>::add(cursor, cursor), low_bits);
123}
124
125IDISA_ALWAYS_INLINE void advance_with_carry(bitblock128_t cursor, carry_t carry_in, carry_t & carry_out, bitblock128_t & rslt)
126{
127bitblock128_t shift_out = simd128<64>::srli<63>(cursor);
128bitblock128_t low_bits = esimd128<64>::mergel(shift_out, carry2bitblock(carry_in));
129carry_out = bitblock2carry(simd128<128>::srli<64>(shift_out));
130rslt = simd_or(simd128<64>::add(cursor, cursor), low_bits);
131}
132
133IDISA_ALWAYS_INLINE bitblock128_t convert(uint64_t s)
134{
135        ubitblock b;
136        b._128 = simd128<128>::constant<0>();
137        b._64[0] = s;
138        return b._128;
139}
140
141IDISA_ALWAYS_INLINE uint64_t convert (bitblock128_t v)
142{
143        return (uint64_t) mvmd128<64>::extract<0>(v);
144}
145
146#endif // BITBLOCK128_HPP_
Note: See TracBrowser for help on using the repository browser.