Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Visualize custom floating point class in debugger

I already did some fiddling in autoexp.dat to ease the inspection of custom string and other simple classes in the Visual Studio debugger (using vs2005). I'd really love to directly see the value (or approximation) of our custom floating point class. The internal representation is a quad-integer (int mantissa[4], 128bit on x86) that will be divided by 10 to the power of our exponent. So it basically looks like this:

class FloatingPoint
{
   private:
      char exponent;
      int mantissa[4]
};

The following statement would convert it to double, given fp is an object of type FloatingPoint:

(mantissa[0] + 
   * ((double)mantissa[1] * 32 * 2) 
   * ((double)mantissa[2] * 64 * 2) 
   * ((double)mantissa[3] * 96 * 2))
   /  std::pow(10, fp.exponent)

Is it possible to somehow get the Visual Studio debugger show objects of type FloatingPoint using this calculation? The call to pow is an extra problem, because this function has no external linking and can not be called by the debugger... maybe there's a way around this?

like image 628
Daniel Albuschat Avatar asked Nov 24 '25 16:11

Daniel Albuschat


1 Answers

Since the range of possible exponents is so small, add a (debug-only) lookup table double power10[256];. The debugger will happily use that. Only downside: debugging constructors of global objects may happen before the array is initialized, in which case those values will still be 0. You will need to call the initializer function yourself, from the Immediate Window, if this happens.

On a slightly-related note, you'll want to choose either signed char or unsigned char for the exponent. (Probably signed, else there's no point to float). Plain char has an implementation-defined sign, and here that sign is quite relevant.

Quick attempt (not tested) :

FloatingPoint {
  preview (
    #([($c.mantissa[0]  + $c.mantissa[1] * 64.0 + $c.mantissa[2] * 128.0 + $c.mantissa[3] * 192.0) / power10[$c.exponent], f])
  )
  stringview (
    #([($c.mantissa[0]  + $c.mantissa[1] * 64.0 + $c.mantissa[2] * 128.0 + $c.mantissa[3] * 192.0) / power10[$c.exponent], f])
  )
  children([$c,!])
}
like image 149
MSalters Avatar answered Nov 27 '25 05:11

MSalters



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!