source: trunk/lib/byte_compare.hpp @ 3148

Last change on this file since 3148 was 2719, checked in by cameron, 6 years ago

Some copyright and description fixes

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