public int set_halffloat(float fpin, int offset)
    {   // converts float to half precision with proper rounding        
        float absfpin, signfpin;        
        int signbit, n, man,e, halfExponent;
//System.out.println("conversion constants   " + HALF_MXMAG + "  " + HALF_NOMRTRIG + "  "  + HALF_NORMFACTOR);
    if (offset > 6)
        {
            return -1;
        }        
        if(fpin == 0.0)
        {
            pb[6 + offset] = (byte) (0);
            pb[7 + offset] = (byte) (0);
            return 0;
        }        
        if (fpin >= 0)
        {
            absfpin = fpin;
            signfpin = 1;
            signbit = 0;
        }
        else
        {
            absfpin = - fpin;
            signfpin = -1;
            signbit = 0x80;
        }        
        fpin = (absfpin > HALF_MXMAG) ? HALF_MXMAG * signfpin : fpin;        
//System.out.println("fpin:  " +fpin + "  " + HALF_MXMAG);
        if (absfpin <= HALF_NOMRTRIG)
        {   //  subnormal
            n = Float.floatToIntBits(fpin * HALF_NORMFACTOR) + (1 << (SINGLE_MBITS - HALF_MBITS - 1));
//System.out.println("here subnorm:  " + n + "  "  + HALF_NORMFACTOR + "  "+ fpin + "  " +  signbit);
            pb[6 + offset] = (byte) (n >> (SINGLE_MBITS - HALF_MBITS));
            pb[7 + offset] = (byte) ((n >> (SINGLE_MBITS - HALF_MBITS + 8))
                    | signbit);  // note < smallest representable magnitude/2 codes as 0
         }    
        else
        {   //  normal(ized) mantissa
            n = Float.floatToIntBits(fpin) + (1 << (SINGLE_MBITS - HALF_MBITS - 1));
//System.out.println("here norm:  " + n +  "  "+ fpin);
//System.out.println("here norm after round operation:  " + n);
            halfExponent = ((n & 0x7f800000) >> (SINGLE_MBITS - 2)) + ((HALF_EBIAS - SINGLE_EBIAS) * 4);
            pb[6 + offset] = (byte) (n >> (SINGLE_MBITS - HALF_MBITS));                        
            pb[7 + offset] = (byte) ((n >> (SINGLE_MBITS - HALF_MBITS + 8) & 0x3)
                    | halfExponent | signbit);
        }
        return 0;
    }