1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34 package org.waarp.compress.zstdunsafe;
35
36 import org.waarp.compress.MalformedInputException;
37
38 import static org.waarp.compress.zstdunsafe.Constants.*;
39
40 final class Util {
41 private Util() {
42 }
43
44 public static int highestBit(final int value) {
45 return 31 - Integer.numberOfLeadingZeros(value);
46 }
47
48 public static boolean isPowerOf2(final int value) {
49 return (value & (value - 1)) == 0;
50 }
51
52 public static int mask(final int bits) {
53 return (1 << bits) - 1;
54 }
55
56 public static void verify(final boolean condition, final long offset,
57 final String reason) {
58 if (!condition) {
59 throw new MalformedInputException(offset, reason);
60 }
61 }
62
63 public static void checkArgument(final boolean condition,
64 final String reason) {
65 if (!condition) {
66 throw new IllegalArgumentException(reason);
67 }
68 }
69
70 public static void checkState(final boolean condition, final String reason) {
71 if (!condition) {
72 throw new IllegalStateException(reason);
73 }
74 }
75
76 public static MalformedInputException fail(final long offset,
77 final String reason) {
78 throw new MalformedInputException(offset, reason);
79 }
80
81 public static int cycleLog(final int hashLog,
82 final CompressionParameters.Strategy strategy) {
83 int cycleLog = hashLog;
84 if (strategy == CompressionParameters.Strategy.BTLAZY2 ||
85 strategy == CompressionParameters.Strategy.BTOPT ||
86 strategy == CompressionParameters.Strategy.BTULTRA) {
87 cycleLog = hashLog - 1;
88 }
89 return cycleLog;
90 }
91
92 public static void put24BitLittleEndian(final Object outputBase,
93 final long outputAddress,
94 final int value) {
95 UnsafeUtil.UNSAFE.putShort(outputBase, outputAddress, (short) value);
96 UnsafeUtil.UNSAFE.putByte(outputBase, outputAddress + SIZE_OF_SHORT,
97 (byte) (value >>> Short.SIZE));
98 }
99
100
101 public static int minTableLog(final int inputSize, final int maxSymbolValue) {
102 if (inputSize <= 1) {
103 throw new IllegalArgumentException(
104 "Not supported. RLE should be used instead");
105 }
106
107 final int minBitsSrc = highestBit((inputSize - 1)) + 1;
108 final int minBitsSymbols = highestBit(maxSymbolValue) + 2;
109 return Math.min(minBitsSrc, minBitsSymbols);
110 }
111 }