Java Float to floatToS390IntBits(float ieeeFloat)

Here you can find the source of floatToS390IntBits(float ieeeFloat)

Description

Converts an IEEE float to an S390 float (as an int).

License

Open Source License

Parameter

Parameter Description
ieeeFloat the IEEE float.

Return

the number as an S390 float.

Declaration

public static int floatToS390IntBits(float ieeeFloat) 

Method Source Code

//package com.java2s;
/*******************************************************************************
 * Copyright ? 2006, 2013 IBM Corporation and others.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:/*from  w  ww. ja va2s .c om*/
 * IBM Corporation - initial API and implementation
 *
 *******************************************************************************/

public class Main {
    private final static int FLOAT_SIGN_MASK = 0x80000000;
    private final static int FLOAT_EXPONENT_MASK = 0x7f800000;
    private final static int FLOAT_MANTISSA_MASK = 0x007fffff;
    private final static int FLOAT_MANTISSA_MSB_MASK = 0x00800000;
    private final static int FLOAT_BIAS = 126;
    private final static int S390_FLOAT_BIAS = 64;

    /**
     * Converts an IEEE float to an S390 float (as an int). 
     * 
     * @param ieeeFloat  the IEEE float.
     * @return the number as an S390 float.
     */
    public static int floatToS390IntBits(float ieeeFloat) {
        // To convert from IEEE to S390 we use the following formula:
        // let r = exponent % 4;  q = exponent / 4;
        // if q == 0 then m.2^x = m.16^q
        // if q != 0 then m.2^x = (m.2^(r-4)).16^(q+1)  for positive q,
        //                      = (m.2^-r).16^q) for negative q

        // Get the bit pattern
        int ieeeIntBits = Float.floatToIntBits(ieeeFloat);

        // Test the sign bit (0 = positive, 1 = negative)
        boolean positive = ((ieeeIntBits & FLOAT_SIGN_MASK) == 0);

        // Deal with zero straight away...
        if ((ieeeIntBits & 0x7fffffff) == 0) {
            // + or - 0.0
            return ieeeIntBits;
        }

        // Extract the exponent
        int exponent = ieeeIntBits & FLOAT_EXPONENT_MASK;
        // shift right 23 bits to get exponent in least significant byte
        exponent = exponent >>> 23;
        // subtract the bias to get the true value
        exponent = exponent - FLOAT_BIAS;

        // Extract the mantissa
        int mantissa = ieeeIntBits & FLOAT_MANTISSA_MASK;
        // for an exponent greater than -FLOAT_BIAS, add in the implicit bit
        if (exponent > (-FLOAT_BIAS)) {
            mantissa = mantissa | FLOAT_MANTISSA_MSB_MASK;
        }

        // Now begin the conversion to S390
        int remainder = Math.abs(exponent) % 4;
        int quotient = Math.abs(exponent) / 4;
        int s390Exponent = quotient;
        if ((exponent > 0) && (remainder != 0)) {
            s390Exponent = s390Exponent + 1;
        }

        // put the sign back in
        if (exponent < 0) {
            s390Exponent = -s390Exponent;
        }

        // Add the bias
        s390Exponent += S390_FLOAT_BIAS;

        // Now adjust the mantissa part
        int s390Mantissa = mantissa;
        if (remainder > 0) {
            if (exponent > 0) {
                // the next two lines perform the (m.2^(r-4)) part of the
                // conversion
                int shift_places = 4 - remainder;
                s390Mantissa = s390Mantissa >>> shift_places;
            } else {
                // to avoid loss of precision when the exponent is at a minimum,
                // we may need to shift the mantissa four places left, and
                // decrease the
                // s390Exponent by one before shifting right
                if ((exponent == -(FLOAT_BIAS)) && ((s390Mantissa & 0x00f00000) == 0)) {
                    s390Mantissa = s390Mantissa << 4;
                    s390Exponent = s390Exponent - 1;
                }
                // the next two line perform the (m.2^-r) part of the conversion
                int shift_places = remainder;
                s390Mantissa = s390Mantissa >>> shift_places;
            }
        }

        // An exponent of -FLOAT_BIAS is the smallest that IEEE can do. S390 has
        // a wider range, and hence may be able to normalise the mantissa more
        // than
        // is possible for IEEE
        // Also, since an exponent of -FLOAT_BIAS has no implicit bit set, the
        // mantissa
        // starts with a value of 2^-1 at the second bit of the second byte. We
        // thus need
        // to shift one place left to move the mantissa to the S390 position
        // Follwoing that, we notmalise as follows:
        // Each shift left of four bits is equivalent to multiplying by 16,
        // so the exponent must be reduced by 1
        if (exponent == -(FLOAT_BIAS)) {
            s390Mantissa = s390Mantissa << 1;
            while ((s390Mantissa != 0) && ((s390Mantissa & 0x00f00000) == 0)) {
                s390Mantissa = s390Mantissa << 4;
                s390Exponent = s390Exponent - 1;
            }
        }

        // Assemble the s390BitPattern
        int s390Float = 0;
        int s390ExponentBits = s390Exponent & 0x0000007F;
        // make sure we only deal with 7 bits
        // add the exponent
        s390Float = s390ExponentBits << 24; // shift to MSB
        // add the sign
        if (!positive) {
            s390Float = s390Float | FLOAT_SIGN_MASK;
        }
        // add the mantissa
        s390Float = s390Float | s390Mantissa;

        return s390Float;
    }
}

Related

  1. floatToHalf(float f)
  2. FloatToInt(double x)
  3. floatToIntColor(float value)
  4. floatToPrefixCoded(float val)
  5. floatToRegisters(float f)
  6. floatToSemiPrecision(float f, byte abyte0[], int i)
  7. floatToSortableInt(float val)
  8. floatToSortableInt(float value)
  9. floatToTime(float aTimeDuration)