source: trunk/lib/bitblock128.hpp @ 1975

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

Defining carry_t; allows different storage represenations for carries.

File size: 3.4 KB
Line 
1#ifndef BITBLOCK128_HPP_
2#define BITBLOCK128_HPP_
3/*=============================================================================
4    bitblock128 - Specific 128 bit IDISA implementations.
5
6    Idealized SIMD Operations with SSE versions
7    Copyright (C) 2011, Robert D. Cameron, Kenneth S. Herdy, Hua Huang and Nigel Medforth.
8    Licensed to the public under the Open Software License 3.0.
9    Licensed to International Characters Inc.
10       under the Academic Free License version 3.0.
11
12=============================================================================*/
13
14#include "idisa128.hpp"
15#include "builtins.hpp"
16
17union ubitblock {
18        bitblock128_t _128;
19        uint64_t _64[sizeof(bitblock128_t)/sizeof(uint64_t)];
20        uint32_t _32[sizeof(bitblock128_t)/sizeof(uint32_t)];
21        uint16_t _16[sizeof(bitblock128_t)/sizeof(uint16_t)];
22        uint8_t _8[sizeof(bitblock128_t)/sizeof(uint8_t)];
23};
24
25/* The type used to store a carry bit. */
26typedef bitblock128_t carry_t;
27
28static IDISA_ALWAYS_INLINE bitblock128_t carry2bitblock(carry_t carry);
29static IDISA_ALWAYS_INLINE carry_t bitblock2carry(bitblock128_t carry);
30
31static IDISA_ALWAYS_INLINE void adc(bitblock128_t x, bitblock128_t y, carry_t & carry, bitblock128_t & sum);
32static IDISA_ALWAYS_INLINE void sbb(bitblock128_t x, bitblock128_t y, carry_t & borrow, bitblock128_t & difference);
33static IDISA_ALWAYS_INLINE void advance_with_carry(bitblock128_t cursor, carry_t & carry, bitblock128_t & rslt);
34
35static IDISA_ALWAYS_INLINE bitblock128_t convert (uint64_t s);
36static IDISA_ALWAYS_INLINE uint64_t convert (bitblock128_t v);
37
38static IDISA_ALWAYS_INLINE bitblock128_t carry2bitblock(carry_t carry) {  return carry;} 
39static IDISA_ALWAYS_INLINE carry_t bitblock2carry(bitblock128_t carry) {  return carry;}
40
41IDISA_ALWAYS_INLINE void adc(bitblock128_t x, bitblock128_t y, carry_t & carry, bitblock128_t & sum)
42{
43        bitblock128_t gen = simd_and(x, y);
44        bitblock128_t prop = simd_or(x, y);
45        bitblock128_t partial = simd128<64>::add(simd128<64>::add(x, y), carry2bitblock(carry));
46        bitblock128_t c1 = simd128<128>::slli<64>(simd128<64>::srli<63>(simd_or(gen, simd_andc(prop, partial))));
47        sum = simd128<64>::add(c1, partial);
48        carry = bitblock2carry(simd128<128>::srli<127>(simd_or(gen, simd_andc(prop, sum))));
49}
50
51IDISA_ALWAYS_INLINE void sbb(bitblock128_t x, bitblock128_t y, carry_t & borrow, bitblock128_t & difference)
52{
53        bitblock128_t gen = simd_andc(y, x);
54        bitblock128_t prop = simd_not(simd_xor(x, y));
55        bitblock128_t partial = simd128<64>::sub(simd128<64>::sub(x, y), carry2bitblock(borrow));
56        bitblock128_t b1 = simd128<128>::slli<64>(simd128<64>::srli<63>(simd_or(gen, simd_and(prop, partial))));
57        difference = simd128<64>::sub(partial, b1);
58        borrow = bitblock2carry(simd128<128>::srli<127>(simd_or(gen, simd_and(prop, difference))));
59}
60
61IDISA_ALWAYS_INLINE void advance_with_carry(bitblock128_t cursor, carry_t & carry, bitblock128_t & rslt)
62{
63bitblock128_t shift_out = simd128<64>::srli<63>(cursor);
64bitblock128_t low_bits = esimd128<64>::mergel(shift_out, carry2bitblock(carry));
65carry = bitblock2carry(simd128<128>::srli<64>(shift_out));
66rslt = simd_or(simd128<64>::add(cursor, cursor), low_bits);
67}
68
69IDISA_ALWAYS_INLINE bitblock128_t convert(uint64_t s)
70{
71        ubitblock b = {b._128 = simd128<128>::constant<0>()}; // = {0};
72        b._64[0] = s;
73        return b._128;
74}
75
76IDISA_ALWAYS_INLINE uint64_t convert (bitblock128_t v)
77{
78        return (uint64_t) mvmd128<64>::extract<0>(v);
79}
80
81#endif // BITBLOCK128_HPP_
Note: See TracBrowser for help on using the repository browser.