Line data Source code
1 : /* GRAPHITE2 LICENSING
2 :
3 : Copyright 2015, SIL International
4 : All rights reserved.
5 :
6 : This library is free software; you can redistribute it and/or modify
7 : it under the terms of the GNU Lesser General Public License as published
8 : by the Free Software Foundation; either version 2.1 of License, or
9 : (at your option) any later version.
10 :
11 : This program is distributed in the hope that it will be useful,
12 : but WITHOUT ANY WARRANTY; without even the implied warranty of
13 : MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 : Lesser General Public License for more details.
15 :
16 : You should also have received a copy of the GNU Lesser General Public
17 : License along with this library in the file named "LICENSE".
18 : If not, write to the Free Software Foundation, 51 Franklin Street,
19 : Suite 500, Boston, MA 02110-1335, USA or visit their web page on the
20 : internet at http://www.fsf.org/licenses/lgpl.html.
21 :
22 : Alternatively, the contents of this file may be used under the terms of the
23 : Mozilla Public License (http://mozilla.org/MPL) or the GNU General Public
24 : License, as published by the Free Software Foundation, either version 2
25 : of the License or (at your option) any later version.
26 : */
27 :
28 : #pragma once
29 :
30 : #include <cassert>
31 : #include <cstddef>
32 : #include <cstring>
33 :
34 : namespace
35 : {
36 :
37 : #if defined(_MSC_VER)
38 : typedef unsigned __int8 u8;
39 : typedef unsigned __int16 u16;
40 : typedef unsigned __int32 u32;
41 : typedef unsigned __int64 u64;
42 : #else
43 : #include <stdint.h>
44 : typedef uint8_t u8;
45 : typedef uint16_t u16;
46 : typedef uint32_t u32;
47 : typedef uint64_t u64;
48 : #endif
49 :
50 : ptrdiff_t const MINMATCH = 4;
51 :
52 : template<int S>
53 : inline
54 0 : void unaligned_copy(void * d, void const * s) {
55 0 : ::memcpy(d, s, S);
56 0 : }
57 :
58 : inline
59 0 : size_t align(size_t p) {
60 0 : return (p + sizeof(unsigned long)-1) & ~(sizeof(unsigned long)-1);
61 : }
62 :
63 : inline
64 0 : u8 * safe_copy(u8 * d, u8 const * s, size_t n) {
65 0 : while (n--) *d++ = *s++;
66 0 : return d;
67 : }
68 :
69 : inline
70 0 : u8 * overrun_copy(u8 * d, u8 const * s, size_t n) {
71 0 : size_t const WS = sizeof(unsigned long);
72 0 : u8 const * e = s + n;
73 0 : do
74 : {
75 0 : unaligned_copy<WS>(d, s);
76 0 : d += WS;
77 0 : s += WS;
78 : }
79 0 : while (s < e);
80 0 : d-=(s-e);
81 :
82 0 : return d;
83 : }
84 :
85 :
86 : inline
87 0 : u8 * fast_copy(u8 * d, u8 const * s, size_t n) {
88 0 : size_t const WS = sizeof(unsigned long);
89 0 : size_t wn = n/WS;
90 0 : while (wn--)
91 : {
92 0 : unaligned_copy<WS>(d, s);
93 0 : d += WS;
94 0 : s += WS;
95 : }
96 0 : n &= WS-1;
97 0 : return safe_copy(d, s, n);
98 : }
99 :
100 :
101 : } // end of anonymous namespace
102 :
103 :
|