source: trunk/lib_c/byte_compare.h @ 3478

Last change on this file since 3478 was 3391, checked in by linmengl, 6 years ago

check in IDISA C library and other support libraries. Some template features still remain.

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