package jxl.biff;

public class DoubleHelper {
  public static double getIEEEDouble(byte[] data, int pos) {
    int num1 = IntegerHelper.getInt(data[pos], data[pos + 1], data[pos + 2], data[pos + 3]);
    int num2 = IntegerHelper.getInt(data[pos + 4], data[pos + 5], data[pos + 6], data[pos + 7]);
    boolean negative = ((num2 & Integer.MIN_VALUE) != 0);
    long val = (num2 & Integer.MAX_VALUE) * 4294967296L + ((num1 < 0) ? (4294967296L + num1) : num1);
    double value = Double.longBitsToDouble(val);
    if (negative)
      value = -value; 
    return value;
  }
  
  public static void getIEEEBytes(double d, byte[] target, int pos) {
    long val = Double.doubleToLongBits(d);
    target[pos] = (byte)(int)(val & 0xFFL);
    target[pos + 1] = (byte)(int)((val & 0xFF00L) >> 8L);
    target[pos + 2] = (byte)(int)((val & 0xFF0000L) >> 16L);
    target[pos + 3] = (byte)(int)((val & 0xFFFFFFFFFF000000L) >> 24L);
    target[pos + 4] = (byte)(int)((val & 0xFF00000000L) >> 32L);
    target[pos + 5] = (byte)(int)((val & 0xFF0000000000L) >> 40L);
    target[pos + 6] = (byte)(int)((val & 0xFF000000000000L) >> 48L);
    target[pos + 7] = (byte)(int)((val & 0xFF00000000000000L) >> 56L);
  }
}
