source: trunk/lib/byte_compare.hpp @ 1950

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

Updated comments format.

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