zstd-ruby 1.4.4.0 → 1.5.5.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.gitignore +2 -0
- data/README.md +78 -5
- data/Rakefile +8 -2
- data/ext/zstdruby/common.h +15 -0
- data/ext/zstdruby/extconf.rb +3 -2
- data/ext/zstdruby/libzstd/common/allocations.h +55 -0
- data/ext/zstdruby/libzstd/common/bits.h +200 -0
- data/ext/zstdruby/libzstd/common/bitstream.h +74 -97
- data/ext/zstdruby/libzstd/common/compiler.h +219 -20
- data/ext/zstdruby/libzstd/common/cpu.h +1 -3
- data/ext/zstdruby/libzstd/common/debug.c +11 -31
- data/ext/zstdruby/libzstd/common/debug.h +22 -49
- data/ext/zstdruby/libzstd/common/entropy_common.c +184 -80
- data/ext/zstdruby/libzstd/common/error_private.c +11 -2
- data/ext/zstdruby/libzstd/common/error_private.h +87 -4
- data/ext/zstdruby/libzstd/common/fse.h +47 -116
- data/ext/zstdruby/libzstd/common/fse_decompress.c +127 -127
- data/ext/zstdruby/libzstd/common/huf.h +112 -197
- data/ext/zstdruby/libzstd/common/mem.h +124 -142
- data/ext/zstdruby/libzstd/common/pool.c +54 -27
- data/ext/zstdruby/libzstd/common/pool.h +11 -5
- data/ext/zstdruby/libzstd/common/portability_macros.h +156 -0
- data/ext/zstdruby/libzstd/common/threading.c +78 -22
- data/ext/zstdruby/libzstd/common/threading.h +9 -13
- data/ext/zstdruby/libzstd/common/xxhash.c +15 -873
- data/ext/zstdruby/libzstd/common/xxhash.h +5572 -191
- data/ext/zstdruby/libzstd/common/zstd_common.c +2 -37
- data/ext/zstdruby/libzstd/common/zstd_deps.h +111 -0
- data/ext/zstdruby/libzstd/common/zstd_internal.h +186 -144
- data/ext/zstdruby/libzstd/common/zstd_trace.h +163 -0
- data/ext/zstdruby/libzstd/compress/clevels.h +134 -0
- data/ext/zstdruby/libzstd/compress/fse_compress.c +99 -196
- data/ext/zstdruby/libzstd/compress/hist.c +41 -63
- data/ext/zstdruby/libzstd/compress/hist.h +13 -33
- data/ext/zstdruby/libzstd/compress/huf_compress.c +968 -331
- data/ext/zstdruby/libzstd/compress/zstd_compress.c +4120 -1191
- data/ext/zstdruby/libzstd/compress/zstd_compress_internal.h +688 -159
- data/ext/zstdruby/libzstd/compress/zstd_compress_literals.c +121 -40
- data/ext/zstdruby/libzstd/compress/zstd_compress_literals.h +16 -6
- data/ext/zstdruby/libzstd/compress/zstd_compress_sequences.c +62 -35
- data/ext/zstdruby/libzstd/compress/zstd_compress_sequences.h +10 -3
- data/ext/zstdruby/libzstd/compress/zstd_compress_superblock.c +577 -0
- data/ext/zstdruby/libzstd/compress/zstd_compress_superblock.h +32 -0
- data/ext/zstdruby/libzstd/compress/zstd_cwksp.h +322 -115
- data/ext/zstdruby/libzstd/compress/zstd_double_fast.c +394 -154
- data/ext/zstdruby/libzstd/compress/zstd_double_fast.h +4 -3
- data/ext/zstdruby/libzstd/compress/zstd_fast.c +729 -253
- data/ext/zstdruby/libzstd/compress/zstd_fast.h +4 -3
- data/ext/zstdruby/libzstd/compress/zstd_lazy.c +1289 -247
- data/ext/zstdruby/libzstd/compress/zstd_lazy.h +61 -1
- data/ext/zstdruby/libzstd/compress/zstd_ldm.c +339 -212
- data/ext/zstdruby/libzstd/compress/zstd_ldm.h +15 -3
- data/ext/zstdruby/libzstd/compress/zstd_ldm_geartab.h +106 -0
- data/ext/zstdruby/libzstd/compress/zstd_opt.c +508 -282
- data/ext/zstdruby/libzstd/compress/zstd_opt.h +1 -1
- data/ext/zstdruby/libzstd/compress/zstdmt_compress.c +217 -466
- data/ext/zstdruby/libzstd/compress/zstdmt_compress.h +35 -114
- data/ext/zstdruby/libzstd/decompress/huf_decompress.c +1220 -572
- data/ext/zstdruby/libzstd/decompress/huf_decompress_amd64.S +576 -0
- data/ext/zstdruby/libzstd/decompress/zstd_ddict.c +23 -19
- data/ext/zstdruby/libzstd/decompress/zstd_ddict.h +3 -3
- data/ext/zstdruby/libzstd/decompress/zstd_decompress.c +859 -273
- data/ext/zstdruby/libzstd/decompress/zstd_decompress_block.c +1244 -375
- data/ext/zstdruby/libzstd/decompress/zstd_decompress_block.h +21 -7
- data/ext/zstdruby/libzstd/decompress/zstd_decompress_internal.h +74 -11
- data/ext/zstdruby/libzstd/dictBuilder/cover.c +75 -54
- data/ext/zstdruby/libzstd/dictBuilder/cover.h +20 -9
- data/ext/zstdruby/libzstd/dictBuilder/divsufsort.c +1 -1
- data/ext/zstdruby/libzstd/dictBuilder/fastcover.c +55 -36
- data/ext/zstdruby/libzstd/dictBuilder/zdict.c +126 -110
- data/ext/zstdruby/libzstd/{dictBuilder/zdict.h → zdict.h} +248 -56
- data/ext/zstdruby/libzstd/zstd.h +1277 -306
- data/ext/zstdruby/libzstd/{common/zstd_errors.h → zstd_errors.h} +29 -8
- data/ext/zstdruby/main.c +20 -0
- data/ext/zstdruby/skippable_frame.c +63 -0
- data/ext/zstdruby/streaming_compress.c +177 -0
- data/ext/zstdruby/streaming_compress.h +5 -0
- data/ext/zstdruby/streaming_decompress.c +123 -0
- data/ext/zstdruby/zstdruby.c +114 -32
- data/lib/zstd-ruby/version.rb +1 -1
- data/lib/zstd-ruby.rb +0 -1
- data/zstd-ruby.gemspec +1 -1
- metadata +24 -39
- data/.travis.yml +0 -14
- data/ext/zstdruby/libzstd/.gitignore +0 -3
- data/ext/zstdruby/libzstd/BUCK +0 -234
- data/ext/zstdruby/libzstd/Makefile +0 -289
- data/ext/zstdruby/libzstd/README.md +0 -159
- data/ext/zstdruby/libzstd/deprecated/zbuff.h +0 -214
- data/ext/zstdruby/libzstd/deprecated/zbuff_common.c +0 -26
- data/ext/zstdruby/libzstd/deprecated/zbuff_compress.c +0 -147
- data/ext/zstdruby/libzstd/deprecated/zbuff_decompress.c +0 -75
- data/ext/zstdruby/libzstd/dll/example/Makefile +0 -47
- data/ext/zstdruby/libzstd/dll/example/README.md +0 -69
- data/ext/zstdruby/libzstd/dll/example/build_package.bat +0 -20
- data/ext/zstdruby/libzstd/dll/example/fullbench-dll.sln +0 -25
- data/ext/zstdruby/libzstd/dll/example/fullbench-dll.vcxproj +0 -181
- data/ext/zstdruby/libzstd/legacy/zstd_legacy.h +0 -415
- data/ext/zstdruby/libzstd/legacy/zstd_v01.c +0 -2152
- data/ext/zstdruby/libzstd/legacy/zstd_v01.h +0 -94
- data/ext/zstdruby/libzstd/legacy/zstd_v02.c +0 -3514
- data/ext/zstdruby/libzstd/legacy/zstd_v02.h +0 -93
- data/ext/zstdruby/libzstd/legacy/zstd_v03.c +0 -3156
- data/ext/zstdruby/libzstd/legacy/zstd_v03.h +0 -93
- data/ext/zstdruby/libzstd/legacy/zstd_v04.c +0 -3641
- data/ext/zstdruby/libzstd/legacy/zstd_v04.h +0 -142
- data/ext/zstdruby/libzstd/legacy/zstd_v05.c +0 -4046
- data/ext/zstdruby/libzstd/legacy/zstd_v05.h +0 -162
- data/ext/zstdruby/libzstd/legacy/zstd_v06.c +0 -4150
- data/ext/zstdruby/libzstd/legacy/zstd_v06.h +0 -172
- data/ext/zstdruby/libzstd/legacy/zstd_v07.c +0 -4533
- data/ext/zstdruby/libzstd/legacy/zstd_v07.h +0 -187
- data/ext/zstdruby/libzstd/libzstd.pc.in +0 -15
- data/ext/zstdruby/zstdruby.h +0 -6
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 76dbffc6a0a13fccd92ea93abd90e33c4a26ab2ac2972877b7928a515e37033e
|
4
|
+
data.tar.gz: 21e3eba574ac94d9f34ac0d33732435ed3fd2373e9db921702a74d2a2c9d606a
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 4f8c40ad6eaa9b014467fc57651f857713767f7930b2e7e2060c50ff58d05262416d0e9cb1427f440298af577eeef5247daec1866a55003ca6d9b2e2ea6212de
|
7
|
+
data.tar.gz: 5836f8061f7588081df400bf0705fcc74003029ec9ef9225e43f5ba5a0b19ec747af71cf0e34f150212a5efd90b9e1ea5ebe49a7231297e3f2ab8080bbf8247d
|
data/.gitignore
CHANGED
data/README.md
CHANGED
@@ -1,5 +1,5 @@
|
|
1
1
|
[![Gem Version](https://badge.fury.io/rb/zstd-ruby.svg)](https://badge.fury.io/rb/zstd-ruby)
|
2
|
-
|
2
|
+
![Build Status](https://github.com/SpringMT/zstd-ruby/actions/workflows/ruby.yml/badge.svg?branch=main)
|
3
3
|
|
4
4
|
# zstd-ruby
|
5
5
|
|
@@ -10,7 +10,7 @@ See https://github.com/facebook/zstd
|
|
10
10
|
Fork from https://github.com/jarredholman/ruby-zstd.
|
11
11
|
|
12
12
|
## Zstd version
|
13
|
-
v1.
|
13
|
+
v1.5.5 (https://github.com/facebook/zstd/tree/v1.5.5)
|
14
14
|
|
15
15
|
## Installation
|
16
16
|
|
@@ -34,20 +34,93 @@ Or install it yourself as:
|
|
34
34
|
require 'zstd-ruby'
|
35
35
|
```
|
36
36
|
|
37
|
-
###
|
37
|
+
### Simple Compression
|
38
38
|
|
39
39
|
```ruby
|
40
40
|
compressed_data = Zstd.compress(data)
|
41
41
|
compressed_data = Zstd.compress(data, complession_level) # default compression_level is 0
|
42
42
|
```
|
43
43
|
|
44
|
+
### Compression using Dictionary
|
45
|
+
```ruby
|
46
|
+
# dictionary is supposed to have been created using `zstd --train`
|
47
|
+
compressed_using_dict = Zstd.compress_using_dict("", IO.read('dictionary_file'))
|
48
|
+
```
|
49
|
+
|
50
|
+
### Streaming Compression
|
51
|
+
```ruby
|
52
|
+
stream = Zstd::StreamingCompress.new
|
53
|
+
stream << "abc" << "def"
|
54
|
+
res = stream.flush
|
55
|
+
stream << "ghi"
|
56
|
+
res << stream.finish
|
57
|
+
```
|
44
58
|
|
45
|
-
|
59
|
+
or
|
60
|
+
|
61
|
+
```ruby
|
62
|
+
stream = Zstd::StreamingCompress.new
|
63
|
+
res = stream.compress("abc")
|
64
|
+
res << stream.flush
|
65
|
+
res << stream.compress("def")
|
66
|
+
res << stream.finish
|
67
|
+
```
|
68
|
+
|
69
|
+
### Simple Decompression
|
46
70
|
|
47
71
|
```ruby
|
48
72
|
data = Zstd.decompress(compressed_data)
|
49
73
|
```
|
50
74
|
|
75
|
+
### Decomporession using Dictionary
|
76
|
+
```ruby
|
77
|
+
# dictionary is supposed to have been created using `zstd --train`
|
78
|
+
Zstd.decompress_using_dict(compressed_using_dict, IO.read('dictionary_file'))
|
79
|
+
```
|
80
|
+
|
81
|
+
### Streaming Decompression
|
82
|
+
```ruby
|
83
|
+
cstr = "" # Compressed data
|
84
|
+
stream = Zstd::StreamingDecompress.new
|
85
|
+
result = ''
|
86
|
+
result << stream.decompress(cstr[0, 10])
|
87
|
+
result << stream.decompress(cstr[10..-1])
|
88
|
+
```
|
89
|
+
|
90
|
+
### Skippable flame
|
91
|
+
|
92
|
+
```ruby
|
93
|
+
compressed_data_with_skippable_frame = Zstd.write_skippable_frame(compressed_data, "sample data")
|
94
|
+
|
95
|
+
Zstd.read_skippable_frame(compressed_data_with_skippable_frame)
|
96
|
+
# => "sample data"
|
97
|
+
```
|
98
|
+
|
99
|
+
## JRuby
|
100
|
+
This gem does not support JRuby.
|
101
|
+
|
102
|
+
Please consider using https://github.com/luben/zstd-jni.
|
103
|
+
|
104
|
+
Sample code is below.
|
105
|
+
|
106
|
+
```
|
107
|
+
require 'java'
|
108
|
+
require_relative './zstd-jni-1.5.2-3.jar'
|
109
|
+
|
110
|
+
str = "testtest"
|
111
|
+
compressed = com.github.luben.zstd.Zstd.compress(str.to_java_bytes)
|
112
|
+
puts com.github.luben.zstd.Zstd.decompress(compressed, str.length)
|
113
|
+
```
|
114
|
+
|
115
|
+
```
|
116
|
+
% ls
|
117
|
+
test.rb zstd-jni-1.5.2-3.jar
|
118
|
+
% ruby -v
|
119
|
+
jruby 9.3.2.0 (2.6.8) 2021-12-01 0b8223f905 OpenJDK 64-Bit Server VM 11.0.12+0 on 11.0.12+0 +jit [darwin-x86_64]
|
120
|
+
% ruby test.rb
|
121
|
+
testtest
|
122
|
+
```
|
123
|
+
|
51
124
|
## Development
|
52
125
|
|
53
126
|
After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
|
@@ -56,7 +129,7 @@ To install this gem onto your local machine, run `bundle exec rake install`. To
|
|
56
129
|
|
57
130
|
## Contributing
|
58
131
|
|
59
|
-
Bug reports and pull requests are welcome on GitHub at https://github.com/SpringMT/
|
132
|
+
Bug reports and pull requests are welcome on GitHub at https://github.com/SpringMT/zstd-ruby. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [Contributor Covenant](http://contributor-covenant.org) code of conduct.
|
60
133
|
|
61
134
|
|
62
135
|
## License
|
data/Rakefile
CHANGED
@@ -17,6 +17,12 @@ task :default => [:clobber, :compile, :spec]
|
|
17
17
|
|
18
18
|
desc 'Sync zstd libs dirs to ext/zstdruby/libzstd'
|
19
19
|
task :zstd_update do
|
20
|
-
FileUtils.rm_r(
|
21
|
-
FileUtils.
|
20
|
+
FileUtils.rm_r("ext/zstdruby/libzstd")
|
21
|
+
FileUtils.mkdir_p("ext/zstdruby/libzstd")
|
22
|
+
["common", "compress", "decompress", "dictBuilder"].each do |dir|
|
23
|
+
FileUtils.cp_r("zstd/lib/#{dir}", "ext/zstdruby/libzstd/#{dir}")
|
24
|
+
end
|
25
|
+
FileUtils.cp_r('zstd/lib/zdict.h', 'ext/zstdruby/libzstd')
|
26
|
+
FileUtils.cp_r('zstd/lib/zstd.h', 'ext/zstdruby/libzstd')
|
27
|
+
FileUtils.cp_r('zstd/lib/zstd_errors.h', 'ext/zstdruby/libzstd')
|
22
28
|
end
|
@@ -0,0 +1,15 @@
|
|
1
|
+
#ifndef ZSTD_RUBY_H
|
2
|
+
#define ZSTD_RUBY_H 1
|
3
|
+
|
4
|
+
#include "ruby.h"
|
5
|
+
#include "./libzstd/zstd.h"
|
6
|
+
|
7
|
+
static int convert_compression_level(VALUE compression_level_value)
|
8
|
+
{
|
9
|
+
if (NIL_P(compression_level_value)) {
|
10
|
+
return ZSTD_CLEVEL_DEFAULT;
|
11
|
+
}
|
12
|
+
return NUM2INT(compression_level_value);
|
13
|
+
}
|
14
|
+
|
15
|
+
#endif /* ZSTD_RUBY_H */
|
data/ext/zstdruby/extconf.rb
CHANGED
@@ -1,9 +1,10 @@
|
|
1
1
|
require "mkmf"
|
2
2
|
|
3
|
-
$CFLAGS = '-I. -O3 -std=c99'
|
3
|
+
$CFLAGS = '-I. -O3 -std=c99 -DZSTD_STATIC_LINKING_ONLY'
|
4
|
+
$CPPFLAGS += " -fdeclspec" if CONFIG['CXX'] =~ /clang/
|
4
5
|
|
5
6
|
Dir.chdir File.expand_path('..', __FILE__) do
|
6
|
-
|
7
|
+
$srcs = Dir['**/*.c', '**/*.S']
|
7
8
|
|
8
9
|
Dir.glob('libzstd/*') do |path|
|
9
10
|
if Dir.exist?(path)
|
@@ -0,0 +1,55 @@
|
|
1
|
+
/*
|
2
|
+
* Copyright (c) Meta Platforms, Inc. and affiliates.
|
3
|
+
* All rights reserved.
|
4
|
+
*
|
5
|
+
* This source code is licensed under both the BSD-style license (found in the
|
6
|
+
* LICENSE file in the root directory of this source tree) and the GPLv2 (found
|
7
|
+
* in the COPYING file in the root directory of this source tree).
|
8
|
+
* You may select, at your option, one of the above-listed licenses.
|
9
|
+
*/
|
10
|
+
|
11
|
+
/* This file provides custom allocation primitives
|
12
|
+
*/
|
13
|
+
|
14
|
+
#define ZSTD_DEPS_NEED_MALLOC
|
15
|
+
#include "zstd_deps.h" /* ZSTD_malloc, ZSTD_calloc, ZSTD_free, ZSTD_memset */
|
16
|
+
|
17
|
+
#include "mem.h" /* MEM_STATIC */
|
18
|
+
#define ZSTD_STATIC_LINKING_ONLY
|
19
|
+
#include "../zstd.h" /* ZSTD_customMem */
|
20
|
+
|
21
|
+
#ifndef ZSTD_ALLOCATIONS_H
|
22
|
+
#define ZSTD_ALLOCATIONS_H
|
23
|
+
|
24
|
+
/* custom memory allocation functions */
|
25
|
+
|
26
|
+
MEM_STATIC void* ZSTD_customMalloc(size_t size, ZSTD_customMem customMem)
|
27
|
+
{
|
28
|
+
if (customMem.customAlloc)
|
29
|
+
return customMem.customAlloc(customMem.opaque, size);
|
30
|
+
return ZSTD_malloc(size);
|
31
|
+
}
|
32
|
+
|
33
|
+
MEM_STATIC void* ZSTD_customCalloc(size_t size, ZSTD_customMem customMem)
|
34
|
+
{
|
35
|
+
if (customMem.customAlloc) {
|
36
|
+
/* calloc implemented as malloc+memset;
|
37
|
+
* not as efficient as calloc, but next best guess for custom malloc */
|
38
|
+
void* const ptr = customMem.customAlloc(customMem.opaque, size);
|
39
|
+
ZSTD_memset(ptr, 0, size);
|
40
|
+
return ptr;
|
41
|
+
}
|
42
|
+
return ZSTD_calloc(1, size);
|
43
|
+
}
|
44
|
+
|
45
|
+
MEM_STATIC void ZSTD_customFree(void* ptr, ZSTD_customMem customMem)
|
46
|
+
{
|
47
|
+
if (ptr!=NULL) {
|
48
|
+
if (customMem.customFree)
|
49
|
+
customMem.customFree(customMem.opaque, ptr);
|
50
|
+
else
|
51
|
+
ZSTD_free(ptr);
|
52
|
+
}
|
53
|
+
}
|
54
|
+
|
55
|
+
#endif /* ZSTD_ALLOCATIONS_H */
|
@@ -0,0 +1,200 @@
|
|
1
|
+
/*
|
2
|
+
* Copyright (c) Meta Platforms, Inc. and affiliates.
|
3
|
+
* All rights reserved.
|
4
|
+
*
|
5
|
+
* This source code is licensed under both the BSD-style license (found in the
|
6
|
+
* LICENSE file in the root directory of this source tree) and the GPLv2 (found
|
7
|
+
* in the COPYING file in the root directory of this source tree).
|
8
|
+
* You may select, at your option, one of the above-listed licenses.
|
9
|
+
*/
|
10
|
+
|
11
|
+
#ifndef ZSTD_BITS_H
|
12
|
+
#define ZSTD_BITS_H
|
13
|
+
|
14
|
+
#include "mem.h"
|
15
|
+
|
16
|
+
MEM_STATIC unsigned ZSTD_countTrailingZeros32_fallback(U32 val)
|
17
|
+
{
|
18
|
+
assert(val != 0);
|
19
|
+
{
|
20
|
+
static const U32 DeBruijnBytePos[32] = {0, 1, 28, 2, 29, 14, 24, 3,
|
21
|
+
30, 22, 20, 15, 25, 17, 4, 8,
|
22
|
+
31, 27, 13, 23, 21, 19, 16, 7,
|
23
|
+
26, 12, 18, 6, 11, 5, 10, 9};
|
24
|
+
return DeBruijnBytePos[((U32) ((val & -(S32) val) * 0x077CB531U)) >> 27];
|
25
|
+
}
|
26
|
+
}
|
27
|
+
|
28
|
+
MEM_STATIC unsigned ZSTD_countTrailingZeros32(U32 val)
|
29
|
+
{
|
30
|
+
assert(val != 0);
|
31
|
+
# if defined(_MSC_VER)
|
32
|
+
# if STATIC_BMI2 == 1
|
33
|
+
return (unsigned)_tzcnt_u32(val);
|
34
|
+
# else
|
35
|
+
if (val != 0) {
|
36
|
+
unsigned long r;
|
37
|
+
_BitScanForward(&r, val);
|
38
|
+
return (unsigned)r;
|
39
|
+
} else {
|
40
|
+
/* Should not reach this code path */
|
41
|
+
__assume(0);
|
42
|
+
}
|
43
|
+
# endif
|
44
|
+
# elif defined(__GNUC__) && (__GNUC__ >= 4)
|
45
|
+
return (unsigned)__builtin_ctz(val);
|
46
|
+
# else
|
47
|
+
return ZSTD_countTrailingZeros32_fallback(val);
|
48
|
+
# endif
|
49
|
+
}
|
50
|
+
|
51
|
+
MEM_STATIC unsigned ZSTD_countLeadingZeros32_fallback(U32 val) {
|
52
|
+
assert(val != 0);
|
53
|
+
{
|
54
|
+
static const U32 DeBruijnClz[32] = {0, 9, 1, 10, 13, 21, 2, 29,
|
55
|
+
11, 14, 16, 18, 22, 25, 3, 30,
|
56
|
+
8, 12, 20, 28, 15, 17, 24, 7,
|
57
|
+
19, 27, 23, 6, 26, 5, 4, 31};
|
58
|
+
val |= val >> 1;
|
59
|
+
val |= val >> 2;
|
60
|
+
val |= val >> 4;
|
61
|
+
val |= val >> 8;
|
62
|
+
val |= val >> 16;
|
63
|
+
return 31 - DeBruijnClz[(val * 0x07C4ACDDU) >> 27];
|
64
|
+
}
|
65
|
+
}
|
66
|
+
|
67
|
+
MEM_STATIC unsigned ZSTD_countLeadingZeros32(U32 val)
|
68
|
+
{
|
69
|
+
assert(val != 0);
|
70
|
+
# if defined(_MSC_VER)
|
71
|
+
# if STATIC_BMI2 == 1
|
72
|
+
return (unsigned)_lzcnt_u32(val);
|
73
|
+
# else
|
74
|
+
if (val != 0) {
|
75
|
+
unsigned long r;
|
76
|
+
_BitScanReverse(&r, val);
|
77
|
+
return (unsigned)(31 - r);
|
78
|
+
} else {
|
79
|
+
/* Should not reach this code path */
|
80
|
+
__assume(0);
|
81
|
+
}
|
82
|
+
# endif
|
83
|
+
# elif defined(__GNUC__) && (__GNUC__ >= 4)
|
84
|
+
return (unsigned)__builtin_clz(val);
|
85
|
+
# else
|
86
|
+
return ZSTD_countLeadingZeros32_fallback(val);
|
87
|
+
# endif
|
88
|
+
}
|
89
|
+
|
90
|
+
MEM_STATIC unsigned ZSTD_countTrailingZeros64(U64 val)
|
91
|
+
{
|
92
|
+
assert(val != 0);
|
93
|
+
# if defined(_MSC_VER) && defined(_WIN64)
|
94
|
+
# if STATIC_BMI2 == 1
|
95
|
+
return (unsigned)_tzcnt_u64(val);
|
96
|
+
# else
|
97
|
+
if (val != 0) {
|
98
|
+
unsigned long r;
|
99
|
+
_BitScanForward64(&r, val);
|
100
|
+
return (unsigned)r;
|
101
|
+
} else {
|
102
|
+
/* Should not reach this code path */
|
103
|
+
__assume(0);
|
104
|
+
}
|
105
|
+
# endif
|
106
|
+
# elif defined(__GNUC__) && (__GNUC__ >= 4) && defined(__LP64__)
|
107
|
+
return (unsigned)__builtin_ctzll(val);
|
108
|
+
# else
|
109
|
+
{
|
110
|
+
U32 mostSignificantWord = (U32)(val >> 32);
|
111
|
+
U32 leastSignificantWord = (U32)val;
|
112
|
+
if (leastSignificantWord == 0) {
|
113
|
+
return 32 + ZSTD_countTrailingZeros32(mostSignificantWord);
|
114
|
+
} else {
|
115
|
+
return ZSTD_countTrailingZeros32(leastSignificantWord);
|
116
|
+
}
|
117
|
+
}
|
118
|
+
# endif
|
119
|
+
}
|
120
|
+
|
121
|
+
MEM_STATIC unsigned ZSTD_countLeadingZeros64(U64 val)
|
122
|
+
{
|
123
|
+
assert(val != 0);
|
124
|
+
# if defined(_MSC_VER) && defined(_WIN64)
|
125
|
+
# if STATIC_BMI2 == 1
|
126
|
+
return (unsigned)_lzcnt_u64(val);
|
127
|
+
# else
|
128
|
+
if (val != 0) {
|
129
|
+
unsigned long r;
|
130
|
+
_BitScanReverse64(&r, val);
|
131
|
+
return (unsigned)(63 - r);
|
132
|
+
} else {
|
133
|
+
/* Should not reach this code path */
|
134
|
+
__assume(0);
|
135
|
+
}
|
136
|
+
# endif
|
137
|
+
# elif defined(__GNUC__) && (__GNUC__ >= 4)
|
138
|
+
return (unsigned)(__builtin_clzll(val));
|
139
|
+
# else
|
140
|
+
{
|
141
|
+
U32 mostSignificantWord = (U32)(val >> 32);
|
142
|
+
U32 leastSignificantWord = (U32)val;
|
143
|
+
if (mostSignificantWord == 0) {
|
144
|
+
return 32 + ZSTD_countLeadingZeros32(leastSignificantWord);
|
145
|
+
} else {
|
146
|
+
return ZSTD_countLeadingZeros32(mostSignificantWord);
|
147
|
+
}
|
148
|
+
}
|
149
|
+
# endif
|
150
|
+
}
|
151
|
+
|
152
|
+
MEM_STATIC unsigned ZSTD_NbCommonBytes(size_t val)
|
153
|
+
{
|
154
|
+
if (MEM_isLittleEndian()) {
|
155
|
+
if (MEM_64bits()) {
|
156
|
+
return ZSTD_countTrailingZeros64((U64)val) >> 3;
|
157
|
+
} else {
|
158
|
+
return ZSTD_countTrailingZeros32((U32)val) >> 3;
|
159
|
+
}
|
160
|
+
} else { /* Big Endian CPU */
|
161
|
+
if (MEM_64bits()) {
|
162
|
+
return ZSTD_countLeadingZeros64((U64)val) >> 3;
|
163
|
+
} else {
|
164
|
+
return ZSTD_countLeadingZeros32((U32)val) >> 3;
|
165
|
+
}
|
166
|
+
}
|
167
|
+
}
|
168
|
+
|
169
|
+
MEM_STATIC unsigned ZSTD_highbit32(U32 val) /* compress, dictBuilder, decodeCorpus */
|
170
|
+
{
|
171
|
+
assert(val != 0);
|
172
|
+
return 31 - ZSTD_countLeadingZeros32(val);
|
173
|
+
}
|
174
|
+
|
175
|
+
/* ZSTD_rotateRight_*():
|
176
|
+
* Rotates a bitfield to the right by "count" bits.
|
177
|
+
* https://en.wikipedia.org/w/index.php?title=Circular_shift&oldid=991635599#Implementing_circular_shifts
|
178
|
+
*/
|
179
|
+
MEM_STATIC
|
180
|
+
U64 ZSTD_rotateRight_U64(U64 const value, U32 count) {
|
181
|
+
assert(count < 64);
|
182
|
+
count &= 0x3F; /* for fickle pattern recognition */
|
183
|
+
return (value >> count) | (U64)(value << ((0U - count) & 0x3F));
|
184
|
+
}
|
185
|
+
|
186
|
+
MEM_STATIC
|
187
|
+
U32 ZSTD_rotateRight_U32(U32 const value, U32 count) {
|
188
|
+
assert(count < 32);
|
189
|
+
count &= 0x1F; /* for fickle pattern recognition */
|
190
|
+
return (value >> count) | (U32)(value << ((0U - count) & 0x1F));
|
191
|
+
}
|
192
|
+
|
193
|
+
MEM_STATIC
|
194
|
+
U16 ZSTD_rotateRight_U16(U16 const value, U32 count) {
|
195
|
+
assert(count < 16);
|
196
|
+
count &= 0x0F; /* for fickle pattern recognition */
|
197
|
+
return (value >> count) | (U16)(value << ((0U - count) & 0x0F));
|
198
|
+
}
|
199
|
+
|
200
|
+
#endif /* ZSTD_BITS_H */
|