blob: f03b51cbbb8eb6c0520af001b3facc7d402077bd [file] [log] [blame]
/*
* Copyright (C) 2008 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package android.renderscript;
/**
* @hide
*
**/
public class FieldPacker {
public FieldPacker(int len) {
mPos = 0;
mData = new byte[len];
}
public void align(int v) {
while ((mPos & (v - 1)) != 0) {
mData[mPos++] = 0;
}
}
public void reset() {
mPos = 0;
}
public void reset(int i) {
mPos = i;
}
public void skip(int i) {
mPos += i;
}
public void addI8(byte v) {
mData[mPos++] = v;
}
public void addI16(short v) {
align(2);
mData[mPos++] = (byte)(v & 0xff);
mData[mPos++] = (byte)(v >> 8);
}
public void addI32(int v) {
align(4);
mData[mPos++] = (byte)(v & 0xff);
mData[mPos++] = (byte)((v >> 8) & 0xff);
mData[mPos++] = (byte)((v >> 16) & 0xff);
mData[mPos++] = (byte)((v >> 24) & 0xff);
}
public void addI64(long v) {
align(8);
mData[mPos++] = (byte)(v & 0xff);
mData[mPos++] = (byte)((v >> 8) & 0xff);
mData[mPos++] = (byte)((v >> 16) & 0xff);
mData[mPos++] = (byte)((v >> 24) & 0xff);
mData[mPos++] = (byte)((v >> 32) & 0xff);
mData[mPos++] = (byte)((v >> 40) & 0xff);
mData[mPos++] = (byte)((v >> 48) & 0xff);
mData[mPos++] = (byte)((v >> 56) & 0xff);
}
public void addU8(short v) {
if ((v < 0) || (v > 0xff)) {
throw new IllegalArgumentException("Saving value out of range for type");
}
mData[mPos++] = (byte)v;
}
public void addU16(int v) {
if ((v < 0) || (v > 0xffff)) {
throw new IllegalArgumentException("Saving value out of range for type");
}
align(2);
mData[mPos++] = (byte)(v & 0xff);
mData[mPos++] = (byte)(v >> 8);
}
public void addU32(long v) {
if ((v < 0) || (v > 0xffffffff)) {
throw new IllegalArgumentException("Saving value out of range for type");
}
align(4);
mData[mPos++] = (byte)(v & 0xff);
mData[mPos++] = (byte)((v >> 8) & 0xff);
mData[mPos++] = (byte)((v >> 16) & 0xff);
mData[mPos++] = (byte)((v >> 24) & 0xff);
}
public void addU64(long v) {
if (v < 0) {
throw new IllegalArgumentException("Saving value out of range for type");
}
align(8);
mData[mPos++] = (byte)(v & 0xff);
mData[mPos++] = (byte)((v >> 8) & 0xff);
mData[mPos++] = (byte)((v >> 16) & 0xff);
mData[mPos++] = (byte)((v >> 24) & 0xff);
mData[mPos++] = (byte)((v >> 32) & 0xff);
mData[mPos++] = (byte)((v >> 40) & 0xff);
mData[mPos++] = (byte)((v >> 48) & 0xff);
mData[mPos++] = (byte)((v >> 56) & 0xff);
}
public void addF32(float v) {
addI32(Float.floatToRawIntBits(v));
}
public void addF64(float v) {
addI64(Double.doubleToRawLongBits(v));
}
public void addObj(BaseObj obj) {
if (obj != null) {
addI32(obj.getID());
} else {
addI32(0);
}
}
public void addF32(Float2 v) {
addF32(v.x);
addF32(v.y);
}
public void addF32(Float3 v) {
addF32(v.x);
addF32(v.y);
addF32(v.z);
}
public void addF32(Float4 v) {
addF32(v.x);
addF32(v.y);
addF32(v.z);
addF32(v.w);
}
public void addI8(Byte2 v) {
addI8(v.x);
addI8(v.y);
}
public void addI8(Byte3 v) {
addI8(v.x);
addI8(v.y);
addI8(v.z);
}
public void addI8(Byte4 v) {
addI8(v.x);
addI8(v.y);
addI8(v.z);
addI8(v.w);
}
public void addU8(Short2 v) {
addU8(v.x);
addU8(v.y);
}
public void addU8(Short3 v) {
addU8(v.x);
addU8(v.y);
addU8(v.z);
}
public void addU8(Short4 v) {
addU8(v.x);
addU8(v.y);
addU8(v.z);
addU8(v.w);
}
public void addI16(Short2 v) {
addI16(v.x);
addI16(v.y);
}
public void addI16(Short3 v) {
addI16(v.x);
addI16(v.y);
addI16(v.z);
}
public void addI16(Short4 v) {
addI16(v.x);
addI16(v.y);
addI16(v.z);
addI16(v.w);
}
public void addU16(Int2 v) {
addU16(v.x);
addU16(v.y);
}
public void addU16(Int3 v) {
addU16(v.x);
addU16(v.y);
addU16(v.z);
}
public void addU16(Int4 v) {
addU16(v.x);
addU16(v.y);
addU16(v.z);
addU16(v.w);
}
public void addI32(Int2 v) {
addI32(v.x);
addI32(v.y);
}
public void addI32(Int3 v) {
addI32(v.x);
addI32(v.y);
addI32(v.z);
}
public void addI32(Int4 v) {
addI32(v.x);
addI32(v.y);
addI32(v.z);
addI32(v.w);
}
public void addU32(Int2 v) {
addU32(v.x);
addU32(v.y);
}
public void addU32(Int3 v) {
addU32(v.x);
addU32(v.y);
addU32(v.z);
}
public void addU32(Int4 v) {
addU32(v.x);
addU32(v.y);
addU32(v.z);
addU32(v.w);
}
public void addBoolean(boolean v) {
addI8((byte)(v ? 1 : 0));
}
public final byte[] getData() {
return mData;
}
private final byte mData[];
private int mPos;
}