1 /* 2 * Copyright 2010 Google Inc. 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); you may not 5 * use this file except in compliance with the License. You may obtain a copy of 6 * 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, WITHOUT 12 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the 13 * License for the specific language governing permissions and limitations under 14 * the License. 15 */ 16 17 package com.google.gwt.corp.compatibility; 18 19 import com.google.gwt.typedarrays.client.Float32ArrayNative; 20 import com.google.gwt.typedarrays.client.Int32ArrayNative; 21 import com.google.gwt.typedarrays.client.Int8ArrayNative; 22 import com.google.gwt.typedarrays.shared.Float32Array; 23 import com.google.gwt.typedarrays.shared.Int32Array; 24 import com.google.gwt.typedarrays.shared.Int8Array; 25 26 public class Numbers { 27 28 static final double LN2 = Math.log(2); 29 floatToIntBits(float f)30 public static final int floatToIntBits (float f) { 31 wfa.set(0, f); 32 return wia.get(0); 33 34 // if (Float.isNaN(f)) { 35 // return 0x7f800001; 36 // } 37 // int signBit; 38 // if (f == 0) { 39 // return (1/f == Float.NEGATIVE_INFINITY) ? 0x80000000 : 0; 40 // } else if (f < 0) { 41 // f = -f; 42 // signBit = 0x80000000; 43 // } else { 44 // signBit = 0; 45 // } 46 // if (f == Float.POSITIVE_INFINITY) { 47 // return signBit | 0x7f800000; 48 // } 49 // 50 // int exponent = (int) (Math.log(f) / LN2); 51 // if (exponent < -126) { 52 // exponent = -126; 53 // } 54 // int significand = (int) (0.5 + f * Math.exp(-(exponent - 23) * LN2)); 55 // 56 // // Handle exponent rounding issues & denorm 57 // if ((significand & 0x01000000) != 0) { 58 // significand >>= 1; 59 // exponent++; 60 // } else if ((significand & 0x00800000) == 0) { 61 // if (exponent == -126) { 62 // return signBit | significand; 63 // } else { 64 // significand <<= 1; 65 // exponent--; 66 // } 67 // } 68 // 69 // return signBit | ((exponent + 127) << 23) | (significand & 0x007fffff); 70 } 71 72 static Int8Array wba = Int8ArrayNative.create(4); 73 static Int32Array wia = Int32ArrayNative.create(wba.buffer(), 0, 1); 74 static Float32Array wfa = Float32ArrayNative.create(wba.buffer(), 0, 1); 75 intBitsToFloat(int i)76 public static final float intBitsToFloat (int i) { 77 // wba.set(0, (byte) (i >> 24)); 78 // wba.set(1, (byte) (i >> 16)); 79 // wba.set(2, (byte) (i >> 8)); 80 // wba.set(3, (byte) (i)); 81 wia.set(0, i); 82 return wfa.get(0); 83 // 84 // 85 // int exponent = (i >>> 23) & 255; 86 // int significand = i & 0x007fffff; 87 // float result; 88 // if (exponent == 0) { 89 // result = (float) (Math.exp((-126 - 23) * LN2) * significand); 90 // } else if (exponent == 255) { 91 // result = significand == 0 ? Float.POSITIVE_INFINITY : Float.NaN; 92 // } else { 93 // result = (float) (Math.exp((exponent - 127 - 23) * LN2) * (0x00800000 | significand)); 94 // } 95 // 96 // return (i & 0x80000000) == 0 ? result : -result; 97 } 98 doubleToLongBits(Double d)99 public static final long doubleToLongBits (Double d) { 100 throw new RuntimeException("NYI"); 101 } 102 longBitsToDouble(long l)103 public static final double longBitsToDouble (long l) { 104 throw new RuntimeException("NYI"); 105 } 106 doubleToRawLongBits(double value)107 public static long doubleToRawLongBits (double value) { 108 throw new RuntimeException("NYI: Numbers.doubleToRawLongBits"); 109 } 110 } 111