source: trunk/lib/byte_compare.hpp @ 1872

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

Update template parameter structure.

File size: 2.7 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 "bitblock.hpp"
29#include <string.h>
30#include <stdint.h>
31#include <iostream>
32using namespace std;
33
34template<class T, uint32_t L> IDISA_ALWAYS_INLINE bool overlap_compare(const uint8_t * x, const uint8_t * y);
35template<class T> IDISA_ALWAYS_INLINE bool overlap_compare(const uint8_t * x, const uint8_t * y, uint32_t lgth);
36template<class T> IDISA_ALWAYS_INLINE bool compare(const uint8_t * x, const uint8_t * y, const uint32_t offset);
37IDISA_ALWAYS_INLINE bool mem_compare(const unsigned char * x, const unsigned char * y, uint32_t lgth);
38
39template<class T, uint32_t L>
40IDISA_ALWAYS_INLINE bool overlap_compare(const uint8_t * x, const uint8_t * y) {
41
42    bool accum = true;
43    uint8_t * p_x = (uint8_t*)x;
44    uint8_t * p_y = (uint8_t*)y;
45
46    for(int i=0; i < L/sizeof(T); i++) {
47        accum = accum && compare<T>(p_x,p_y,0);
48        p_x+=sizeof(T);
49        p_y+=sizeof(T);
50    }
51    if(L & (sizeof(T)-1)) {
52        accum = accum && compare<T>(x,y,L-sizeof(T));
53    }
54    return accum;
55}
56
57template<class T>
58IDISA_ALWAYS_INLINE bool overlap_compare(const uint8_t * x, const uint8_t * y, uint32_t lgth) {
59
60    bool accum = true;
61    uint8_t * p_x = (uint8_t*)x;
62    uint8_t * p_y = (uint8_t*)y;
63
64    for(int i=0; i < lgth/sizeof(T); i++) {
65        accum = accum && compare<T>(p_x,p_y,0);
66        p_x+=sizeof(T);
67        p_y+=sizeof(T);
68    }
69    if(lgth & (sizeof(T)-1)) {
70        accum = accum && compare<T>(x,y,lgth-sizeof(T));
71    }
72    return accum;
73}
74
75template<class T>
76IDISA_ALWAYS_INLINE bool compare(const uint8_t * x, const uint8_t * y, const uint32_t offset) {
77    return !((*((T*)((uint8_t *)x + offset))) ^
78             (*((T*)((uint8_t *)y + offset))));
79}
80
81template<>
82IDISA_ALWAYS_INLINE bool compare<BitBlock>(const uint8_t * x, const uint8_t * y, const uint32_t offset) {
83    BitBlock temp = simd_xor(bitblock::load_unaligned((BitBlock*) ((uint8_t *)x + offset)),
84                             bitblock::load_unaligned((BitBlock*) ((uint8_t *)y + offset)));
85    return bitblock::all(simd_not(temp));
86}
87
88IDISA_ALWAYS_INLINE bool mem_compare(const unsigned char * x, const unsigned char * y, uint32_t lgth) {
89        return (0 == memcmp(x, y, lgth));
90}
91
92
93#endif // BYTE_COMPARE_HPP
94
95
Note: See TracBrowser for help on using the repository browser.