Background
Is there anybody here who is an EE with programming experience who has worked with the Texas Instruments TMS320 C3X-series DSP? I am currently doing some work with the C3X for a project. My goal here is to convert C3X-formatted floating point hexadecimal values into IEEE 754 format. The code that I have works properly for:
Where x is the input single floating-point C3X hex number (represented as a UInt32).
The issue, is when I attempt to use a number such as 0.123. It seems that I always get a NaN value as a result. I am currently using this code snippet from the University of Florida that I have ported over to C#.
Procedure
- Swap the sign and exponent fields to go from C3X to IEEE. I will not show this code (unless asked) as I believe it is very straightforward for anyone who has worked with this DSP.
- Perform the University of Florida algorithm (linked above), that I believe I do not fully understand.
- Combine the sign, exponent, and mantissa into single-precision IEEE 754 floating point.
C# Implementation
Here is the code used to implement the algorithm for when the C3X float-number happens to be between 0 and 1 (where the issue is happening). The c30
object, represents the C3X floating point number. The ieee
object represents the IEEE 754 floating point number.
EDIT: Upon popular request, here is a code sample that isn't awful. This sample is the properly working version where we dump in 15,913.35... (as C3X hexadecimal).
C30Float c30 = null;
UInt32 bias = 127;
Int32 mantissa = 0;
Int32 digit = 0;
double fraction = 0;
double exponent = 0;
double fpValue = 0;
double sign = 0;
// 1. Declare the new C3X floating point value.
c30 = new C30Float(0x0D78A56B);
// 2. Get the sign bit value.
sign = c30.GetSign();
// 3. Decide whether or not to do the flip.
if (sign == 1)
{
// flip
mantissa = c30.GetMantissa();
mantissa = mantissa + 1;
mantissa = (~mantissa) & 0x007FFFFF; // the first 9 bits have nothing to do with the mantissa
}
else
{
mantissa = c30.GetMantissa();
}
// 4. Obtain part of the fraction (do the swim!).
for (int i = -1; i > -24; i--)
{
// mantissa index
int currentDigit = i + 23; // n = -23 + 23 => n = 0
// shift forward (get the digit)
digit = mantissa;
digit = digit & (1 << currentDigit);
// shift back (get the digit into the lowest place)
digit = digit >> currentDigit;
// apply the digit to the fraction
fraction += digit * Math.Pow(2, i);
}
// 5. Obtain the exponential part of the number.
exponent = c30.GetExponent() + 0x7F;
// 6. Get the floating point number.
fpValue = Math.Pow(-1, sign);
fpValue *= (1 + fraction) * Math.Pow(2, exponent - bias);
I know that I have not provided the implementation details for the following:
GetMantissa()
GetExponent()
GetSign()
or...
SetMantissa()
SetExponent()
SetSign()
The reason for this is that all these are doing is entering/pulling the numbers to/from their respective fields as specified in the TMS320 C3X User's Guide. Who knows, maybe that is where I'm going wrong, but for now I'd like to start at where I think the scope of the problem begins. If any of you believe that the issue is not here then feel free to ask about this implementation and I will provide more detail as warranted.
Question
My issue is apparently with numbers between 0 and 1. So if anyone has known C3X hex values and their known counter-part floats/reals that I can plug-and-chug that would be great so I can troubleshoot this algorithm.
Note: We don't have the DSP in front of us for what we're doing and can't just get the numbers out.