1/*
2 * Copyright (c) 2015-2017, Intel Corporation
3 *
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions are met:
6 *
7 * * Redistributions of source code must retain the above copyright notice,
8 * this list of conditions and the following disclaimer.
9 * * Redistributions in binary form must reproduce the above copyright
10 * notice, this list of conditions and the following disclaimer in the
11 * documentation and/or other materials provided with the distribution.
12 * * Neither the name of Intel Corporation nor the names of its contributors
13 * may be used to endorse or promote products derived from this software
14 * without specific prior written permission.
15 *
16 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
17 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
20 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
21 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
22 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
23 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
24 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
25 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
26 * POSSIBILITY OF SUCH DAMAGE.
27 */
28
29/** \file
30 * \brief Truffle compiler
31 *
32 * truffle is always able to represent an entire character class, providing a
33 * backstop to other acceleration engines.
34 */
35
36#include "trufflecompile.h"
37
38#include "ue2common.h"
39#include "util/charreach.h"
40#include "util/dump_mask.h"
41#include "util/simd_types.h"
42
43#include <cstring>
44
45using namespace std;
46
47namespace ue2 {
48
49/*
50 * To represent an entire charclass (256 chars), truffle uses two 128 bit
51 * masks - the first is for chars that do not have the high bit/bit 7 set,
52 * i.e. chars {0..127}. The second mask is for chars with bit 7 set.
53 *
54 * Each char to be represented is split into the low nibble (bits {0..3}) and
55 * bits {4,5,6} - the low nibble is the offset into the mask and the value of
56 * bits 456 is the bit that is set at that offset.
57 */
58
59void truffleBuildMasks(const CharReach &cr, u8 *shuf_mask_lo_highclear,
60 u8 *shuf_mask_lo_highset) {
61 memset(shuf_mask_lo_highset, 0, sizeof(m128));
62 memset(shuf_mask_lo_highclear, 0, sizeof(m128));
63
64 for (size_t v = cr.find_first(); v != CharReach::npos;
65 v = cr.find_next(v)) {
66 DEBUG_PRINTF("adding 0x%02x to %s\n", (u8)v, (v & 0x80) ? "highset" : "highclear");
67 u8 *change_mask = (v & 0x80) ? shuf_mask_lo_highset : shuf_mask_lo_highclear;
68 u8 low_nibble = v & 0xf;
69 u8 bits_456 = (v & 0x70) >> 4;
70 change_mask[low_nibble] |= 1 << bits_456;
71 }
72}
73
74/*
75 * Reconstruct the charclass that the truffle masks represent
76 */
77CharReach truffle2cr(const u8 *highclear, const u8 *highset) {
78 CharReach cr;
79 for (u8 i = 0; i < 16; i++) {
80 u32 bits_456 = highclear[i];
81 while (bits_456) {
82 u32 pos = findAndClearLSB_32(&bits_456);
83 assert(pos < 8);
84 cr.set(pos << 4 | i);
85 }
86 bits_456 = highset[i];
87 while (bits_456) {
88 u32 pos = findAndClearLSB_32(&bits_456);
89 assert(pos < 8);
90 cr.set(0x80 | pos << 4 | i);
91 }
92 }
93 return cr;
94}
95
96} // namespc
97