source: trunk/lib/bitblock128.hpp @ 3148

Last change on this file since 3148 was 2605, checked in by cameron, 7 years ago

Operations preserving carry-out form add_ci_co, sub_bi_bo, adv_ci_co

File size: 6.4 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 void adc(bitblock128_t x, bitblock128_t y, carry_t & carry, bitblock128_t & sum);
39static IDISA_ALWAYS_INLINE void sbb(bitblock128_t x, bitblock128_t y, carry_t & borrow, bitblock128_t & difference);
40static IDISA_ALWAYS_INLINE void advance_with_carry(bitblock128_t cursor, carry_t & carry, bitblock128_t & rslt);
41
42static IDISA_ALWAYS_INLINE void adc(bitblock128_t x, bitblock128_t y, carry_t carry_in, carry_t & carry_out, bitblock128_t & sum);
43static IDISA_ALWAYS_INLINE void sbb(bitblock128_t x, bitblock128_t y, carry_t borrow_in, carry_t & borrow_out, bitblock128_t & difference);
44static IDISA_ALWAYS_INLINE void advance_with_carry(bitblock128_t cursor, carry_t carry_in, carry_t & carry_out, bitblock128_t & rslt);
45
46static IDISA_ALWAYS_INLINE bitblock128_t convert (uint64_t s);
47static IDISA_ALWAYS_INLINE uint64_t convert (bitblock128_t v);
48
49static IDISA_ALWAYS_INLINE bitblock128_t carry2bitblock(carry_t carry) {  return carry;} 
50static IDISA_ALWAYS_INLINE carry_t bitblock2carry(bitblock128_t carry) {  return carry;}
51
52
53
54
55static IDISA_ALWAYS_INLINE void add_ci_co(bitblock128_t x, bitblock128_t y, bitblock128_t carry_in, bitblock128_t & carry_out, bitblock128_t & sum) {
56        bitblock128_t gen = simd_and(x, y);
57        bitblock128_t prop = simd_or(x, y);
58        bitblock128_t partial = simd128<64>::add(simd128<64>::add(x, y), carry_in);
59        bitblock128_t c1 = simd128<128>::slli<64>(simd128<64>::srli<63>(simd_or(gen, simd_andc(prop, partial))));
60        sum = simd128<64>::add(c1, partial);
61        carry_out = simd_or(gen, simd_andc(prop, sum));
62}
63static IDISA_ALWAYS_INLINE void sub_bi_bo(bitblock128_t x, bitblock128_t y, bitblock128_t borrow_in, bitblock128_t & borrow_out, bitblock128_t & difference){
64        bitblock128_t gen = simd_andc(y, x);
65        bitblock128_t prop = simd_not(simd_xor(x, y));
66        bitblock128_t partial = simd128<64>::sub(simd128<64>::sub(x, y), borrow_in);
67        bitblock128_t b1 = simd128<128>::slli<64>(simd128<64>::srli<63>(simd_or(gen, simd_and(prop, partial))));
68        difference = simd128<64>::sub(partial, b1);
69        borrow_out = simd_or(gen, simd_and(prop, difference));
70}
71static IDISA_ALWAYS_INLINE void adv_ci_co(bitblock128_t cursor, bitblock128_t carry_in, bitblock128_t & carry_out, bitblock128_t & rslt){
72        bitblock128_t shift_out = simd128<64>::srli<63>(cursor);
73        bitblock128_t low_bits = esimd128<64>::mergel(shift_out, carry_in);
74        carry_out = cursor;
75        rslt = simd_or(simd128<64>::add(cursor, cursor), low_bits);
76}
77
78IDISA_ALWAYS_INLINE void adc(bitblock128_t x, bitblock128_t y, carry_t & carry, bitblock128_t & sum)
79{
80        bitblock128_t gen = simd_and(x, y);
81        bitblock128_t prop = simd_or(x, y);
82        bitblock128_t partial = simd128<64>::add(simd128<64>::add(x, y), carry2bitblock(carry));
83        bitblock128_t c1 = simd128<128>::slli<64>(simd128<64>::srli<63>(simd_or(gen, simd_andc(prop, partial))));
84        sum = simd128<64>::add(c1, partial);
85        carry = bitblock2carry(simd128<128>::srli<127>(simd_or(gen, simd_andc(prop, sum))));
86}
87
88IDISA_ALWAYS_INLINE void adc(bitblock128_t x, bitblock128_t y, carry_t carry_in, carry_t & carry_out, bitblock128_t & sum)
89{
90        bitblock128_t co;
91        add_ci_co(x, y, carry2bitblock(carry_in), co, sum);
92        carry_out = bitblock2carry(simd128<128>::srli<127>(co));
93}
94
95IDISA_ALWAYS_INLINE void sbb(bitblock128_t x, bitblock128_t y, carry_t & borrow, bitblock128_t & difference)
96{
97        bitblock128_t gen = simd_andc(y, x);
98        bitblock128_t prop = simd_not(simd_xor(x, y));
99        bitblock128_t partial = simd128<64>::sub(simd128<64>::sub(x, y), carry2bitblock(borrow));
100        bitblock128_t b1 = simd128<128>::slli<64>(simd128<64>::srli<63>(simd_or(gen, simd_and(prop, partial))));
101        difference = simd128<64>::sub(partial, b1);
102        borrow = bitblock2carry(simd128<128>::srli<127>(simd_or(gen, simd_and(prop, difference))));
103}
104
105IDISA_ALWAYS_INLINE void sbb(bitblock128_t x, bitblock128_t y, carry_t borrow_in, carry_t & borrow_out, bitblock128_t & difference)
106{
107        bitblock128_t bo;
108        sub_bi_bo(x, y, carry2bitblock(borrow_in), bo, difference);
109        borrow_out = bitblock2carry(simd128<128>::srli<127>(bo));
110}
111
112IDISA_ALWAYS_INLINE void advance_with_carry(bitblock128_t cursor, carry_t & carry, bitblock128_t & rslt)
113{
114bitblock128_t shift_out = simd128<64>::srli<63>(cursor);
115bitblock128_t low_bits = esimd128<64>::mergel(shift_out, carry2bitblock(carry));
116carry = bitblock2carry(simd128<128>::srli<64>(shift_out));
117rslt = simd_or(simd128<64>::add(cursor, cursor), low_bits);
118}
119
120IDISA_ALWAYS_INLINE void advance_with_carry(bitblock128_t cursor, carry_t carry_in, carry_t & carry_out, bitblock128_t & rslt)
121{
122bitblock128_t shift_out = simd128<64>::srli<63>(cursor);
123bitblock128_t low_bits = esimd128<64>::mergel(shift_out, carry2bitblock(carry_in));
124carry_out = bitblock2carry(simd128<128>::srli<64>(shift_out));
125rslt = simd_or(simd128<64>::add(cursor, cursor), low_bits);
126}
127
128IDISA_ALWAYS_INLINE bitblock128_t convert(uint64_t s)
129{
130        ubitblock b;
131        b._128 = simd128<128>::constant<0>();
132        b._64[0] = s;
133        return b._128;
134}
135
136IDISA_ALWAYS_INLINE uint64_t convert (bitblock128_t v)
137{
138        return (uint64_t) mvmd128<64>::extract<0>(v);
139}
140
141#endif // BITBLOCK128_HPP_
Note: See TracBrowser for help on using the repository browser.