Home | History | Annotate | Download | only in renderscript
      1 /*
      2  * Copyright (C) 2008 The Android Open Source Project
      3  *
      4  * Licensed under the Apache License, Version 2.0 (the "License");
      5  * you may not use this file except in compliance with the License.
      6  * You may obtain a copy of the License at
      7  *
      8  *      http://www.apache.org/licenses/LICENSE-2.0
      9  *
     10  * Unless required by applicable law or agreed to in writing, software
     11  * distributed under the License is distributed on an "AS IS" BASIS,
     12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     13  * See the License for the specific language governing permissions and
     14  * limitations under the License.
     15  */
     16 
     17 package android.renderscript;
     18 
     19 
     20 /**
     21  * @hide
     22  *
     23  **/
     24 public class FieldPacker {
     25     public FieldPacker(int len) {
     26         mPos = 0;
     27         mData = new byte[len];
     28     }
     29 
     30     public void align(int v) {
     31         while ((mPos & (v - 1)) != 0) {
     32             mData[mPos++] = 0;
     33         }
     34     }
     35 
     36     void reset() {
     37         mPos = 0;
     38     }
     39 
     40     void addI8(byte v) {
     41         mData[mPos++] = v;
     42     }
     43 
     44     void addI16(short v) {
     45         align(2);
     46         mData[mPos++] = (byte)(v & 0xff);
     47         mData[mPos++] = (byte)(v >> 8);
     48     }
     49 
     50     void addI32(int v) {
     51         align(4);
     52         mData[mPos++] = (byte)(v & 0xff);
     53         mData[mPos++] = (byte)((v >> 8) & 0xff);
     54         mData[mPos++] = (byte)((v >> 16) & 0xff);
     55         mData[mPos++] = (byte)((v >> 24) & 0xff);
     56     }
     57 
     58     void addI64(long v) {
     59         align(8);
     60         mData[mPos++] = (byte)(v & 0xff);
     61         mData[mPos++] = (byte)((v >> 8) & 0xff);
     62         mData[mPos++] = (byte)((v >> 16) & 0xff);
     63         mData[mPos++] = (byte)((v >> 24) & 0xff);
     64         mData[mPos++] = (byte)((v >> 32) & 0xff);
     65         mData[mPos++] = (byte)((v >> 40) & 0xff);
     66         mData[mPos++] = (byte)((v >> 48) & 0xff);
     67         mData[mPos++] = (byte)((v >> 56) & 0xff);
     68     }
     69 
     70     void addU8(short v) {
     71         if ((v < 0) || (v > 0xff)) {
     72             throw new IllegalArgumentException("Saving value out of range for type");
     73         }
     74         mData[mPos++] = (byte)v;
     75     }
     76 
     77     void addU16(int v) {
     78         if ((v < 0) || (v > 0xffff)) {
     79             throw new IllegalArgumentException("Saving value out of range for type");
     80         }
     81         align(2);
     82         mData[mPos++] = (byte)(v & 0xff);
     83         mData[mPos++] = (byte)(v >> 8);
     84     }
     85 
     86     void addU32(long v) {
     87         if ((v < 0) || (v > 0xffffffff)) {
     88             throw new IllegalArgumentException("Saving value out of range for type");
     89         }
     90         align(4);
     91         mData[mPos++] = (byte)(v & 0xff);
     92         mData[mPos++] = (byte)((v >> 8) & 0xff);
     93         mData[mPos++] = (byte)((v >> 16) & 0xff);
     94         mData[mPos++] = (byte)((v >> 24) & 0xff);
     95     }
     96 
     97     void addU64(long v) {
     98         if (v < 0) {
     99             throw new IllegalArgumentException("Saving value out of range for type");
    100         }
    101         align(8);
    102         mData[mPos++] = (byte)(v & 0xff);
    103         mData[mPos++] = (byte)((v >> 8) & 0xff);
    104         mData[mPos++] = (byte)((v >> 16) & 0xff);
    105         mData[mPos++] = (byte)((v >> 24) & 0xff);
    106         mData[mPos++] = (byte)((v >> 32) & 0xff);
    107         mData[mPos++] = (byte)((v >> 40) & 0xff);
    108         mData[mPos++] = (byte)((v >> 48) & 0xff);
    109         mData[mPos++] = (byte)((v >> 56) & 0xff);
    110     }
    111 
    112     void addF32(float v) {
    113         addI32(Float.floatToRawIntBits(v));
    114     }
    115 
    116     void addF64(float v) {
    117         addI64(Double.doubleToRawLongBits(v));
    118     }
    119 
    120     final byte[] getData() {
    121         return mData;
    122     }
    123 
    124     private final byte mData[];
    125     private int mPos;
    126 
    127 }
    128 
    129 
    130