source: trunk/lib/byte_compare.hpp @ 2546

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

Removed problematic 'using namespace std;'

File size: 3.2 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    Created on:
14    Author: Ken Herdy
15=============================================================================*/
16
17///////////////////////////////////////////////////////////////////////////////
18//
19//   WARNING: L (Length)
20//
21//   for L is 1, choose T = uint8_t
22//   for L in [2,3], choose T = uint16_t
23//   for L in [4,7], choose T = uint32_t
24//   for L in [8,15], choose T = uint64_t
25//   for L in [16,), choose T = BitBlock
26//
27//   WARNING: sizeof(T) <= L in the due to pointer cast.
28//              i.e !( (T*) p1 ^ (T*) p2) logic
29//
30///////////////////////////////////////////////////////////////////////////////
31
32#include "bitblock.hpp"
33#include <string.h>
34#include <stdint.h>
35#include <iostream>
36
37template<class T, uint32_t L> IDISA_ALWAYS_INLINE bool overlap_compare(const uint8_t * x, const uint8_t * y);
38template<class T> IDISA_ALWAYS_INLINE bool overlap_compare(const uint8_t * x, const uint8_t * y, uint32_t lgth);
39template<class T> IDISA_ALWAYS_INLINE bool compare(const uint8_t * x, const uint8_t * y, const uint32_t offset);
40IDISA_ALWAYS_INLINE bool mem_compare(const unsigned char * x, const unsigned char * y, uint32_t lgth);
41
42// WARNING: sizeof(T) <= L
43template<uint32_t L, class T>
44IDISA_ALWAYS_INLINE bool overlap_compare(const uint8_t * x, const uint8_t * y) {
45
46    bool accum = true;
47    uint8_t * p_x = (uint8_t*)x;
48    uint8_t * p_y = (uint8_t*)y;
49
50    for(int i=0; i < L/sizeof(T); i++) {
51        accum = accum && compare<T>(p_x,p_y,0);
52        p_x+=sizeof(T);
53        p_y+=sizeof(T);
54    }
55    if(L & (sizeof(T)-1)) {
56        accum = accum && compare<T>(x,y,L-sizeof(T));
57    }
58    return accum;
59}
60
61// WARNING: sizeof(T) <= L
62template<class T>
63IDISA_ALWAYS_INLINE bool overlap_compare(const uint8_t * x, const uint8_t * y, uint32_t lgth) {
64
65    bool accum = true;
66    uint8_t * p_x = (uint8_t*)x;
67    uint8_t * p_y = (uint8_t*)y;
68
69    for(int i=0; i < lgth/sizeof(T); i++) {
70        accum = accum && compare<T>(p_x,p_y,0);
71        p_x+=sizeof(T);
72        p_y+=sizeof(T);
73    }
74    if(lgth & (sizeof(T)-1)) {
75        accum = accum && compare<T>(x,y,lgth-sizeof(T));
76    }
77    return accum;
78}
79
80// WARNING: sizeof(T) <= L
81template<class T>
82IDISA_ALWAYS_INLINE bool compare(const uint8_t * x, const uint8_t * y, const uint32_t offset) {
83    return !((*((T*)((uint8_t *)x + offset))) ^
84             (*((T*)((uint8_t *)y + offset)))); // << offset
85}
86
87template<>
88IDISA_ALWAYS_INLINE bool compare<BitBlock>(const uint8_t * x, const uint8_t * y, const uint32_t offset) {
89    BitBlock temp = simd_xor(bitblock::load_unaligned((BitBlock*) ((uint8_t *)x + offset)),
90                             bitblock::load_unaligned((BitBlock*) ((uint8_t *)y + offset))); // << shift offset
91    return bitblock::all(simd_not(temp));
92}
93
94IDISA_ALWAYS_INLINE bool mem_compare(const unsigned char * x, const unsigned char * y, uint32_t lgth) {
95        return (0 == memcmp(x, y, lgth));
96}
97
98
99#endif // BYTE_COMPARE_HPP
100
101
Note: See TracBrowser for help on using the repository browser.