Skip to contentSkip to conversation

softfloat missing functions

PATCH In review J. Mayer 2 participants 5 messages Mar 19, 2007

lore
Share & tools
lore

J. Mayer

author
#0 lore raw

PATCH 1/1 softfloat missing functions

Some functions are missing from the softfloat API. Those are:
float32 uint32_to_float32( unsigned int STATUS_PARAM); float64 uint64_to_float64( uint64_t v STATUS_PARAM); unsigned int float32_to_uint32( float32 a STATUS_PARAM); unsigned int float32_to_uint32_round_to_zero( float32 a STATUS_PARAM); unsigned int float64_to_uint32( float64 STATUS_PARAM ); unsigned int float64_to_uint32_round_to_zero( float64 STATUS_PARAM ); uint64_t float64_to_uint64( float64 STATUS_PARAM ); uint64_t float64_to_uint64_round_to_zero( float64 STATUS_PARAM );

As I need those function to emulate some PowerPC operations, here's a proposal for their implementation.
Note that float64_to_uint64 functions are not correct, as they won't return results between INT64_MAX and UINT64_MAX. Hope someone may know the proper solution for this.
Please comment and help me fix this.

Signature · 2 lines
J. Mayer <[email protected]>
Never organized

Index: fpu/softfloat-native.c
===================================================================
RCS file: /sources/qemu/qemu/fpu/softfloat-native.c,v retrieving revision 1.6

softfloat-native.c +97 -0
diff -u -d -d -p -r1.6 softfloat-native.c
@@ -59,6 +59,11 @@ float32 int32_to_float32(int v STATUS_PA
     return (float32)v;
 }
 
+float32 uint32_to_float32(unsigned int v STATUS_PARAM)
+{
+    return (float32)v;
+}
+
 float64 int32_to_float64(int v STATUS_PARAM)
 {
     return (float64)v;
@@ -78,6 +83,10 @@ float64 int64_to_float64( int64_t v STAT
 {
     return (float64)v;
 }
+float64 uint64_to_float64( uint64_t v STATUS_PARAM)
+{
+    return (float64)v;
+}
 #ifdef FLOATX80
 floatx80 int64_to_floatx80( int64_t v STATUS_PARAM)
 {
@@ -132,6 +141,37 @@ floatx80 float32_to_floatx80( float32 a 
 }
 #endif
 
+unsigned int float32_to_uint32( float32 a STATUS_PARAM)
+{
+    int64_t v;
+    unsigned int res;
+
+    v = llrintf(a);
+    if (v < 0) {
+        res = 0;
+    } else if (v > 0xffffffff) {
+        res = 0xffffffff;
+    } else {
+        res = v;
+    }
+    return res;
+}
+unsigned int float32_to_uint32_round_to_zero( float32 a STATUS_PARAM)
+{
+    int64_t v;
+    unsigned int res;
+
+    v = (int64_t)a;
+    if (v < 0) {
+        res = 0;
+    } else if (v > 0xffffffff) {
+        res = 0xffffffff;
+    } else {
+        res = v;
+    }
+    return res;
+}
+
 /*----------------------------------------------------------------------------
 | Software IEC/IEEE single-precision operations.
 *----------------------------------------------------------------------------*/
@@ -218,6 +258,63 @@ float128 float64_to_float128( float64 a 
 }
 #endif
 
+unsigned int float64_to_uint32( float64 a STATUS_PARAM)
+{
+    int64_t v;
+    unsigned int res;
+
+    v = llrint(a);
+    if (v < 0) {
+        res = 0;
+    } else if (v > 0xffffffff) {
+        res = 0xffffffff;
+    } else {
+        res = v;
+    }
+    return res;
+}
+unsigned int float64_to_uint32_round_to_zero( float64 a STATUS_PARAM)
+{
+    int64_t v;
+    unsigned int res;
+
+    v = (int64_t)a;
+    if (v < 0) {
+        res = 0;
+    } else if (v > 0xffffffff) {
+        res = 0xffffffff;
+    } else {
+        res = v;
+    }
+    return res;
+}
+uint64_t float64_to_uint64 (float64 a STATUS_PARAM)
+{
+    uint64_t res;
+    int64_t v;
+
+    v = llrint(a);
+    if (v < 0) {
+        res = 0;
+    } else {
+        res = v;
+    }
+    return res;
+}
+uint64_t float64_to_uint64_round_to_zero (float64 a STATUS_PARAM)
+{
+    uint64_t res;
+    int64_t v;
+
+    v = (int64_t)a;
+    if (v < 0) {
+        res = 0;
+    } else {
+        res = v;
+    }
+    return res;
+}
+
 /*----------------------------------------------------------------------------
 | Software IEC/IEEE double-precision operations.
 *----------------------------------------------------------------------------*/

Index: fpu/softfloat-native.h
===================================================================
RCS file: /sources/qemu/qemu/fpu/softfloat-native.h,v retrieving revision 1.8

softfloat-native.h +8 -0
diff -u -d -d -p -r1.8 softfloat-native.h
@@ -99,6 +99,7 @@ void set_floatx80_rounding_precision(int
 | Software IEC/IEEE integer-to-floating-point conversion routines.
 *----------------------------------------------------------------------------*/
 float32 int32_to_float32( int STATUS_PARAM);
+float32 uint32_to_float32( unsigned int STATUS_PARAM);
 float64 int32_to_float64( int STATUS_PARAM);
 #ifdef FLOATX80
 floatx80 int32_to_floatx80( int STATUS_PARAM);
@@ -108,6 +109,7 @@ float128 int32_to_float128( int STATUS_P
 #endif
 float32 int64_to_float32( int64_t STATUS_PARAM);
 float64 int64_to_float64( int64_t STATUS_PARAM);
+float64 uint64_to_float64( uint64_t v STATUS_PARAM);
 #ifdef FLOATX80
 floatx80 int64_to_floatx80( int64_t STATUS_PARAM);
 #endif
@@ -120,6 +122,8 @@ float128 int64_to_float128( int64_t STAT
 *----------------------------------------------------------------------------*/
 int float32_to_int32( float32  STATUS_PARAM);
 int float32_to_int32_round_to_zero( float32  STATUS_PARAM);
+unsigned int float32_to_uint32( float32 a STATUS_PARAM);
+unsigned int float32_to_uint32_round_to_zero( float32 a STATUS_PARAM);
 int64_t float32_to_int64( float32  STATUS_PARAM);
 int64_t float32_to_int64_round_to_zero( float32  STATUS_PARAM);
 float64 float32_to_float64( float32  STATUS_PARAM);
@@ -200,8 +204,12 @@ INLINE float32 float32_chs(float32 a)
 *----------------------------------------------------------------------------*/
 int float64_to_int32( float64 STATUS_PARAM );
 int float64_to_int32_round_to_zero( float64 STATUS_PARAM );
+unsigned int float64_to_uint32( float64 STATUS_PARAM );
+unsigned int float64_to_uint32_round_to_zero( float64 STATUS_PARAM );
 int64_t float64_to_int64( float64 STATUS_PARAM );
 int64_t float64_to_int64_round_to_zero( float64 STATUS_PARAM );
+uint64_t float64_to_uint64( float64 STATUS_PARAM );
+uint64_t float64_to_uint64_round_to_zero( float64 STATUS_PARAM );
 float32 float64_to_float32( float64 STATUS_PARAM );
 #ifdef FLOATX80
 floatx80 float64_to_floatx80( float64 STATUS_PARAM );

Index: fpu/softfloat.c
===================================================================
RCS file: /sources/qemu/qemu/fpu/softfloat.c,v
retrieving revision 1.4

softfloat.c +37 -0
diff -u -d -d -p -r1.4 softfloat.c
@@ -1183,6 +1183,13 @@ float64 int64_to_float64( int64 a STATUS
 
 }
 
+float64 uint64_to_float64( uint64 a STATUS_PARAM )
+{
+    if ( a == 0 ) return 0;
+    return normalizeRoundAndPackFloat64( 0, 0x43C, a STATUS_VAR );
+
+}
+
 #ifdef FLOATX80
 
 /*----------------------------------------------------------------------------
@@ -5282,6 +5289,36 @@ unsigned int float64_to_uint32_round_to_
     return res;
 }
 
+uint64_t float64_to_uint64 (float64 a STATUS_PARAM)
+{
+    uint64_t res;
+    int64_t v;
+
+    v = float64_to_int64(a STATUS_VAR);
+    if (v < 0) {
+        res = 0;
+        float_raise( float_flag_invalid STATUS_VAR);
+    } else {
+        res = v;
+    }
+    return res;
+}
+
+uint64_t float64_to_uint64_round_to_zero (float64 a STATUS_PARAM)
+{
+    uint64_t res;
+    int64_t v;
+
+    v = float64_to_int64_round_to_zero(a STATUS_VAR);
+    if (v < 0) {
+        res = 0;
+        float_raise( float_flag_invalid STATUS_VAR);
+    } else {
+        res = v;
+    }
+    return res;
+}
+
 #define COMPARE(s, nan_exp)                                                  \
 INLINE int float ## s ## _compare_internal( float ## s a, float ## s b,      \
                                       int is_quiet STATUS_PARAM )            \

Index: fpu/softfloat.h
===================================================================
RCS file: /sources/qemu/qemu/fpu/softfloat.h,v
retrieving revision 1.5

softfloat.h +3 -0
diff -u -d -d -p -r1.5 softfloat.h
@@ -194,6 +194,7 @@ float128 int32_to_float128( int STATUS_P
 #endif
 float32 int64_to_float32( int64_t STATUS_PARAM );
 float64 int64_to_float64( int64_t STATUS_PARAM );
+float64 uint64_to_float64( uint64_t STATUS_PARAM );
 #ifdef FLOATX80
 floatx80 int64_to_floatx80( int64_t STATUS_PARAM );
 #endif
@@ -258,6 +259,8 @@ unsigned int float64_to_uint32( float64 
 unsigned int float64_to_uint32_round_to_zero( float64 STATUS_PARAM );
 int64_t float64_to_int64( float64 STATUS_PARAM );
 int64_t float64_to_int64_round_to_zero( float64 STATUS_PARAM );
+uint64_t float64_to_uint64 (float64 a STATUS_PARAM);
+uint64_t float64_to_uint64_round_to_zero (float64 a STATUS_PARAM);
 float32 float64_to_float32( float64 STATUS_PARAM );
 #ifdef FLOATX80
 floatx80 float64_to_floatx80( float64 STATUS_PARAM );
#1 lore raw
Quoted 4 lines

Note that float64_to_uint64 functions are not correct, as they won't return results between INT64_MAX and UINT64_MAX. Hope someone may know the proper solution for this.

How about this?

J

uint64_t float64_to_uint64 (float64 a STATUS_PARAM)

{
    uint64_t res;
    int64_t v;

    if (isinf(a) || isnan(a)) {
       return special value (  maybe 1<<63 ?)
    }
    else
    if (a < 0.0 || a > (float64)UINT64_MAX) {
       return out-of-range value, whatever that is
    } else {

       a += (float64) INT64_MIN;  // move a downwards 
       v = llrint(a);             // convert
       v -= INT64_MIN;            // move v back up

       return v;
    }
}
author
#2 lore raw
Quoted 5 lines · Julian Seward
Quoted 3 lines

Note that float64_to_uint64 functions are not correct, as they won't return results between INT64_MAX and UINT64_MAX. Hope someone may know the proper solution for this.

How about this?

Yes, it seems to be the correct way, but thinking more about the problem, it appeared to me that the implementation could be even easier than yours. It seems to me that this may be sufficient:
uint64_t float64_to_uint64 (float64 a STATUS_PARAM)

{
    int64_t v;

    v = llrint(a + (float64)INT64_MIN);

    return v - INT64_MIN;
}
uint64_t float64_to_uint64_round_to_zero (float64 a STATUS_PARAM)
{
    int64_t v;

    v = (int64_t)(a + (float64)INT64_MIN);

    return v - INT64_MIN;
}

For "not-native" softfloat, this gives:
uint64_t float64_to_uint64 (float64 a STATUS_PARAM)

{
    int64_t v;

    v = int64_to_float64(INT64_MIN STATUS_VAR);
    v = float64_to_int64((a + v) STATUS_VAR);

    return v - INT64_MIN;
}

uint64_t float64_to_uint64_round_to_zero (float64 a STATUS_PARAM)

{
    int64_t v;

    v = int64_to_float64(INT64_MIN STATUS_VAR);
    v = float64_to_int64_round_to_zero((a + v) STATUS_VAR);

    return v - INT64_MIN;
}

This should also give the correct result for NaN and overflows, if we rely to the fact float64_to_int64 is correct. Please tell me if I'm wrong !

Signature · 2 lines
J. Mayer <[email protected]>
Never organized
#3 lore raw

Thinking about this more, you ask "is this correct", but that is only meaningful if you say what the specification is. Correct relative to what?

Quoted 11 lines

Yes, it seems to be the correct way, but thinking more about the problem, it appeared to me that the implementation could be even easier than yours. It seems to me that this may be sufficient:
uint64_t float64_to_uint64 (float64 a STATUS_PARAM)

{
    int64_t v;

    v = llrint(a + (float64)INT64_MIN);

    return v - INT64_MIN;
}

If a is NaN then so is the argument to llrint. 'man llrint' says:

If x is infinite or NaN, or if the rounded value is outside the range of the return type, the numeric result is unspecified.

So then float64_to_uint64 produces an unspecified result.

It seems to me much safer to test and handle NaN, Inf and out-of-range values specially. However, even that does not help unless you say what the specification is.

J

author
#4 lore raw
Quoted 22 lines · Julian Seward

Thinking about this more, you ask "is this correct", but that is only meaningful if you say what the specification is. Correct relative to what?

Quoted 11 lines

Yes, it seems to be the correct way, but thinking more about the problem, it appeared to me that the implementation could be even easier than yours. It seems to me that this may be sufficient:
uint64_t float64_to_uint64 (float64 a STATUS_PARAM)

{
    int64_t v;

    v = llrint(a + (float64)INT64_MIN);

    return v - INT64_MIN;
}

If a is NaN then so is the argument to llrint. 'man llrint' says:

If x is infinite or NaN, or if the rounded value is outside the range of the return type, the numeric result is unspecified.

Quoted 5 lines

So then float64_to_uint64 produces an unspecified result.

It seems to me much safer to test and handle NaN, Inf and out-of-range values specially. However, even that does not help unless you say what the specification is.

Well, you are right, but the function float64_to_int64 acts the same way in that code.
If we want to follow IEEE compliance, we have to use the softfloat functions instead, not the softfloat-native ones. Or we should sanitize the whole softfloat.c code, to be consistent, it seems.

Here's an updated patch, with two more functions. The added functions are:
float32 uint32_to_float32(unsigned int v STATUS_PARAM) float64 uint32_to_float64(unsigned int v STATUS_PARAM) float32 uint64_to_float32( uint64_t v STATUS_PARAM) float64 uint64_to_float64( uint64_t v STATUS_PARAM) unsigned int float32_to_uint32( float32 a STATUS_PARAM) unsigned int float32_to_uint32_round_to_zero( float32 a STATUS_PARAM) unsigned int float64_to_uint32( float64 a STATUS_PARAM) unsigned int float64_to_uint32_round_to_zero( float64 a STATUS_PARAM) uint64_t float64_to_uint64 (float64 a STATUS_PARAM) uint64_t float64_to_uint64_round_to_zero (float64 a STATUS_PARAM)

Note that some of those functions already exist in softfloat.c but not in softfloat-native.c.

Signature · 2 lines
J. Mayer <[email protected]>
Never organized

Index: fpu/softfloat-native.c
===================================================================
RCS file: /sources/qemu/qemu/fpu/softfloat-native.c,v retrieving revision 1.6

softfloat-native.c +96 -0
diff -u -d -d -p -r1.6 softfloat-native.c
@@ -59,11 +59,21 @@ float32 int32_to_float32(int v STATUS_PA
     return (float32)v;
 }
 
+float32 uint32_to_float32(unsigned int v STATUS_PARAM)
+{
+    return (float32)v;
+}
+
 float64 int32_to_float64(int v STATUS_PARAM)
 {
     return (float64)v;
 }
 
+float64 uint32_to_float64(unsigned int v STATUS_PARAM)
+{
+    return (float64)v;
+}
+
 #ifdef FLOATX80
 floatx80 int32_to_floatx80(int v STATUS_PARAM)
 {
@@ -74,10 +84,18 @@ float32 int64_to_float32( int64_t v STAT
 {
     return (float32)v;
 }
+float32 uint64_to_float32( uint64_t v STATUS_PARAM)
+{
+    return (float32)v;
+}
 float64 int64_to_float64( int64_t v STATUS_PARAM)
 {
     return (float64)v;
 }
+float64 uint64_to_float64( uint64_t v STATUS_PARAM)
+{
+    return (float64)v;
+}
 #ifdef FLOATX80
 floatx80 int64_to_floatx80( int64_t v STATUS_PARAM)
 {
@@ -132,6 +150,37 @@ floatx80 float32_to_floatx80( float32 a 
 }
 #endif
 
+unsigned int float32_to_uint32( float32 a STATUS_PARAM)
+{
+    int64_t v;
+    unsigned int res;
+
+    v = llrintf(a);
+    if (v < 0) {
+        res = 0;
+    } else if (v > 0xffffffff) {
+        res = 0xffffffff;
+    } else {
+        res = v;
+    }
+    return res;
+}
+unsigned int float32_to_uint32_round_to_zero( float32 a STATUS_PARAM)
+{
+    int64_t v;
+    unsigned int res;
+
+    v = (int64_t)a;
+    if (v < 0) {
+        res = 0;
+    } else if (v > 0xffffffff) {
+        res = 0xffffffff;
+    } else {
+        res = v;
+    }
+    return res;
+}
+
 /*----------------------------------------------------------------------------
 | Software IEC/IEEE single-precision operations.
 *----------------------------------------------------------------------------*/
@@ -218,6 +267,53 @@ float128 float64_to_float128( float64 a 
 }
 #endif
 
+unsigned int float64_to_uint32( float64 a STATUS_PARAM)
+{
+    int64_t v;
+    unsigned int res;
+
+    v = llrint(a);
+    if (v < 0) {
+        res = 0;
+    } else if (v > 0xffffffff) {
+        res = 0xffffffff;
+    } else {
+        res = v;
+    }
+    return res;
+}
+unsigned int float64_to_uint32_round_to_zero( float64 a STATUS_PARAM)
+{
+    int64_t v;
+    unsigned int res;
+
+    v = (int64_t)a;
+    if (v < 0) {
+        res = 0;
+    } else if (v > 0xffffffff) {
+        res = 0xffffffff;
+    } else {
+        res = v;
+    }
+    return res;
+}
+uint64_t float64_to_uint64 (float64 a STATUS_PARAM)
+{
+    int64_t v;
+
+    v = llrint(a + (float64)INT64_MIN);
+
+    return v - INT64_MIN;
+}
+uint64_t float64_to_uint64_round_to_zero (float64 a STATUS_PARAM)
+{
+    int64_t v;
+
+    v = (int64_t)(a + (float64)INT64_MIN);
+
+    return v - INT64_MIN;
+}
+
 /*----------------------------------------------------------------------------
 | Software IEC/IEEE double-precision operations.
 *----------------------------------------------------------------------------*/

Index: fpu/softfloat-native.h
===================================================================
RCS file: /sources/qemu/qemu/fpu/softfloat-native.h,v retrieving revision 1.8

softfloat-native.h +10 -0
diff -u -d -d -p -r1.8 softfloat-native.h
@@ -99,7 +99,9 @@ void set_floatx80_rounding_precision(int
 | Software IEC/IEEE integer-to-floating-point conversion routines.
 *----------------------------------------------------------------------------*/
 float32 int32_to_float32( int STATUS_PARAM);
+float32 uint32_to_float32( unsigned int STATUS_PARAM);
 float64 int32_to_float64( int STATUS_PARAM);
+float64 uint32_to_float64( unsigned int STATUS_PARAM);
 #ifdef FLOATX80
 floatx80 int32_to_floatx80( int STATUS_PARAM);
 #endif
@@ -107,7 +109,9 @@ floatx80 int32_to_floatx80( int STATUS_P
 float128 int32_to_float128( int STATUS_PARAM);
 #endif
 float32 int64_to_float32( int64_t STATUS_PARAM);
+float32 uint64_to_float32( uint64_t STATUS_PARAM);
 float64 int64_to_float64( int64_t STATUS_PARAM);
+float64 uint64_to_float64( uint64_t v STATUS_PARAM);
 #ifdef FLOATX80
 floatx80 int64_to_floatx80( int64_t STATUS_PARAM);
 #endif
@@ -120,6 +124,8 @@ float128 int64_to_float128( int64_t STAT
 *----------------------------------------------------------------------------*/
 int float32_to_int32( float32  STATUS_PARAM);
 int float32_to_int32_round_to_zero( float32  STATUS_PARAM);
+unsigned int float32_to_uint32( float32 a STATUS_PARAM);
+unsigned int float32_to_uint32_round_to_zero( float32 a STATUS_PARAM);
 int64_t float32_to_int64( float32  STATUS_PARAM);
 int64_t float32_to_int64_round_to_zero( float32  STATUS_PARAM);
 float64 float32_to_float64( float32  STATUS_PARAM);
@@ -200,8 +206,12 @@ INLINE float32 float32_chs(float32 a)
 *----------------------------------------------------------------------------*/
 int float64_to_int32( float64 STATUS_PARAM );
 int float64_to_int32_round_to_zero( float64 STATUS_PARAM );
+unsigned int float64_to_uint32( float64 STATUS_PARAM );
+unsigned int float64_to_uint32_round_to_zero( float64 STATUS_PARAM );
 int64_t float64_to_int64( float64 STATUS_PARAM );
 int64_t float64_to_int64_round_to_zero( float64 STATUS_PARAM );
+uint64_t float64_to_uint64( float64 STATUS_PARAM );
+uint64_t float64_to_uint64_round_to_zero( float64 STATUS_PARAM );
 float32 float64_to_float32( float64 STATUS_PARAM );
 #ifdef FLOATX80
 floatx80 float64_to_floatx80( float64 STATUS_PARAM );

Index: fpu/softfloat.c
===================================================================
RCS file: /sources/qemu/qemu/fpu/softfloat.c,v
retrieving revision 1.4

softfloat.c +48 -0

66 lines not on this page · raw message

Index: fpu/softfloat.h
===================================================================
RCS file: /sources/qemu/qemu/fpu/softfloat.h,v
retrieving revision 1.5

softfloat.h +4 -0
diff -u -d -d -p -r1.5 softfloat.h
@@ -193,7 +193,9 @@ floatx80 int32_to_floatx80( int STATUS_P
 float128 int32_to_float128( int STATUS_PARAM );
 #endif
 float32 int64_to_float32( int64_t STATUS_PARAM );
+float32 uint64_to_float32( uint64_t STATUS_PARAM );
 float64 int64_to_float64( int64_t STATUS_PARAM );
+float64 uint64_to_float64( uint64_t STATUS_PARAM );
 #ifdef FLOATX80
 floatx80 int64_to_floatx80( int64_t STATUS_PARAM );
 #endif
@@ -258,6 +260,8 @@ unsigned int float64_to_uint32( float64 
 unsigned int float64_to_uint32_round_to_zero( float64 STATUS_PARAM );
 int64_t float64_to_int64( float64 STATUS_PARAM );
 int64_t float64_to_int64_round_to_zero( float64 STATUS_PARAM );
+uint64_t float64_to_uint64 (float64 a STATUS_PARAM);
+uint64_t float64_to_uint64_round_to_zero (float64 a STATUS_PARAM);
 float32 float64_to_float32( float64 STATUS_PARAM );
 #ifdef FLOATX80
 floatx80 float64_to_floatx80( float64 STATUS_PARAM );