|
| 1 | +// Copyright The Tuweni Authors |
| 2 | +// SPDX-License-Identifier: Apache-2.0 |
| 3 | +package org.apache.tuweni.units.bigints; |
| 4 | + |
| 5 | +public class Utils { |
| 6 | + static void and( |
| 7 | + int[] sourceBytesArray, int sourceOffset, byte[] destBytesArray, int destOffset, int length) { |
| 8 | + for (int i = 0; i < length; i++) { |
| 9 | + // TODO: Speed this up with SIMD |
| 10 | + destBytesArray[destOffset + i] = |
| 11 | + (byte) (unpackByte(sourceBytesArray, sourceOffset + i) & destBytesArray[destOffset + i]); |
| 12 | + } |
| 13 | + } |
| 14 | + |
| 15 | + static void or( |
| 16 | + int[] sourceBytesArray, int sourceOffset, byte[] destBytesArray, int destOffset, int length) { |
| 17 | + for (int i = 0; i < length; i++) { |
| 18 | + // TODO: Speed this up with SIMD |
| 19 | + destBytesArray[destOffset + i] = |
| 20 | + (byte) (unpackByte(sourceBytesArray, sourceOffset + i) | destBytesArray[destOffset + i]); |
| 21 | + } |
| 22 | + } |
| 23 | + |
| 24 | + static void xor( |
| 25 | + int[] sourceBytesArray, int sourceOffset, byte[] destBytesArray, int destOffset, int length) { |
| 26 | + for (int i = 0; i < length; i++) { |
| 27 | + // TODO: Speed this up with SIMD |
| 28 | + destBytesArray[destOffset + i] = |
| 29 | + (byte) (unpackByte(sourceBytesArray, sourceOffset + i) ^ destBytesArray[destOffset + i]); |
| 30 | + } |
| 31 | + } |
| 32 | + |
| 33 | + static byte unpackByte(int[] ints, int index) { |
| 34 | + int whichInt = index / 4; |
| 35 | + return unpackByte(ints[whichInt], index); |
| 36 | + } |
| 37 | + |
| 38 | + static byte unpackByte(int integer, int index) { |
| 39 | + int whichIndex = 3 - index % 4; |
| 40 | + return (byte) ((integer >> (8 * whichIndex)) & 0xFF); |
| 41 | + } |
| 42 | + |
| 43 | + static byte unpackByte(long value, int index) { |
| 44 | + int whichIndex = 7 - index; |
| 45 | + return (byte) ((value >> (8 * whichIndex)) & 0xFF); |
| 46 | + } |
| 47 | +} |
0 commit comments