source: proto/RE2Pablo/ref/re2pbs/lib/byte_compare.hpp @ 2226

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

Initial check in.

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