Subject: Using Floating Point in kernel... is bad, okay?
To: None <tls@netbsd.org>
From: Juan RP <juan@xtrarom.org>
List: tech-kern
Date: 06/14/2007 19:46:06
Hey, 

So I did convert adt7463.c to envsys2 blindly and now when we tried
it (Nicolas Joly and I) there was a floating point exception.

Not surprisingly the code uses floating point to convert the
voltage/temperature values and return them to userland.

This is code I'm talking about:

adt7463c_refresh_volt(struct adt7463c_softc *sc)
{
        int i;
        u_int8_t reg;
        int data;
        float mult[] = {ADT7463_2_5V_CONST,
                        ADT7463_VCC_CONST,
                        ADT7463_3_3V_CONST,
                        ADT7463_5V_CONST,
                        ADT7463_12V_CONST};

        reg = ADT7463_VOLT_REG_START;
        for (i = 0; i < ADT7463_VOLT_SENSORS_COUNT; i++) {
          adt7463c_send_1(sc,  reg++);
          data = adt7463c_receive_1(sc);

          /* envstat assumes that voltage is in uVDC */
          double val = (data * 1000000.0  * mult[i]);
          if (data > 0)
            sc->sc_sensor[i].cur.data_us = (u_int32_t)val;
          else
            sc->sc_sensor[i].cur.data_us = 0;
        }
}

And the constants are defined as:

/* currently we use only 8 bits and hence the multiplier */
#define ADT7463_12V_CONST           (0.0625)
#define ADT7463_5V_CONST            (0.0260)
#define ADT7463_3_3V_CONST          (0.0171)
#define ADT7463_2_5V_CONST          (0.0130)
#define ADT7463_VCC_CONST           (0.0117)

#define ADT7463_CEL_TO_KELVIN       273.15

Why did anyone saw it before? specially the developer that imported
the driver, (yes, Thor... you imported it).

I would like to know how this code works currently, because with envsys2
obviously catches the FP exception.

Just FYI, have a nice day!

-- 
Juan Romero Pardines	- The NetBSD Project
http://plog.xtrarom.org/	- NetBSD/pkgsrc news in Spanish