source: trunk/lib/byte_compare.hpp @ 1841

Last change on this file since 1841 was 1841, checked in by ksherdy, 7 years ago

Template parameter fix.

File size: 2.6 KB
Line 
1/*
2    byte_compare - Byte comparison methods.
3
4    Idealized SIMD Operations with SSE versions
5    Copyright (C) 2011, Robert D. Cameron, Kenneth S. Herdy.
6    Licensed to the public under the Open Software License 3.0.
7    Licensed to International Characters Inc.
8       under the Academic Free License version 3.0.
9
10
11   WARNING:
12       
13   for L is 1, choose T = uint8_t
14   for L in [2,3], choose T = uint16_t
15   for L in [4,7], choose T = uint32_t
16   for L in [8,15], choose = uint64_t
17   for L in [16,00), T = BitBlock
18
19   Otherwise results are not valid due to pointer casts.
20
21   TODO - STATIC ASSERT - sizeof(T) <= L
22
23*/
24
25#ifndef BYTE_COMPARE_HPP
26#define BYTE_COMPARE_HPP
27
28#include <string.h>
29#include <stdint.h>
30#include "bitblock.hpp"
31
32#include <iostream>
33using namespace std;
34
35template<class T, uint32_t L> IDISA_ALWAYS_INLINE bool overlap_compare(const T * x, const T * y);
36template<class T> IDISA_ALWAYS_INLINE bool overlap_compare(const T * x, const T* y, uint32_t lgth);
37template<class T> IDISA_ALWAYS_INLINE bool compare(const T * x, const T * y, const uint32_t offset);
38IDISA_ALWAYS_INLINE bool mem_compare(const unsigned char * x, const unsigned char * y, uint32_t lgth);
39
40
41template<class T, uint32_t L>
42IDISA_ALWAYS_INLINE bool overlap_compare(const T * x, const T * y) {
43
44    bool accum = true;
45    T * p_x = (T*) x;
46    T * p_y = (T*) y;
47
48    for(int i=0; i < L/sizeof(T); i++) {
49        accum = accum && compare(p_x,p_y,0);
50        p_x++;
51        p_y++;
52    }
53    if(L & (sizeof(T)-1)) {
54        accum = accum && compare(x,y,L-sizeof(T));
55    }
56    return accum;
57}
58
59template<class T>
60IDISA_ALWAYS_INLINE bool overlap_compare(const T * x, const T* y, uint32_t lgth) {
61
62    bool accum = true;
63    T * p_x = (T*) x;
64    T * p_y = (T*) y;
65
66    for(int i=0; i < lgth/sizeof(T); i++) {
67        accum = accum && compare(p_x,p_y,0);
68        p_x++;
69        p_y++;
70    }
71    if(lgth & (sizeof(T)-1)) {
72        accum = accum && compare(x,y,lgth-sizeof(T));
73    }
74    return accum;
75}
76
77template<class T>
78IDISA_ALWAYS_INLINE bool compare(const T * x, const T * y, const uint32_t offset) {
79    return !((*((T*)((uint8_t *)x + offset))) ^
80             (*((T*)((uint8_t *)y + offset))));
81}
82
83template<>
84IDISA_ALWAYS_INLINE bool compare<BitBlock>(const BitBlock * x, const BitBlock * y, const uint32_t offset) {
85    BitBlock temp = simd_xor(bitblock::load_unaligned((BitBlock*) ((uint8_t *)x + offset)),
86                             bitblock::load_unaligned((BitBlock*) ((uint8_t *)y + offset)));
87    return bitblock::all(simd_not(temp));
88}
89
90IDISA_ALWAYS_INLINE bool mem_compare(const unsigned char * x, const unsigned char * y, uint32_t lgth) {
91        return (0 == memcmp(x, y, lgth));
92}
93
94
95#endif // BYTE_COMPARE_HPP
96
97
Note: See TracBrowser for help on using the repository browser.