bcd Type (Structure)

args.h, estack.h, math.h, timath.h, values.h

typedef struct {
unsigned short exponent;
unsigned long long mantissa;
} bcd __attribute__((__may_alias__));

Represents the internal organization of floating point numbers in the format recognized by the TIOS.

The bcd type represents the internal organization of floating point numbers in the format recognized by the TIOS (the so-called SMAP II BCD format). Note that long long is not a typing error: it is a GNU C extension for representing very long integers (8-byte integers in this implementation).

Here will be given the exact internal organization of floating point numbers. Magnitude of every real number (except zero) can be represented as m*10^e, where e (so-called exponent) is an integer, and m (so-called mantissa) is a real number which satisfies condition 1 <= m < 10 (this is somewhat different convention than used in frexp10 function which is derived from ANSI standard). e is stored in the exponent field, and m in mantissa field of the bcd structure. Details of storing format are given below. You don't need to know these details, but they are given here for anybody who needs to know more about floats on TI.

Field exponent of the bcd structure contains e+0x4000 if the number is positive, or e+0xC000 if the number is negative. So, the most significant bit of exponent is the sign of the number, but the format is not 2-complement code (more precise, it is sign_and_magnitude_0x4000_biased code). The exponent is NOT bcd-coded (unlike the mantissa). Legal range for the e is from -16383 to +16382 (values -16384 and +16383 are reserved for some special values), although many math functions are not very happy with extremely small or extremely big exponents. Keep your exponents in the range from -999 to +999.

The mantissa is stored in BCD code. As the mantissa satisfies the condition 1 <= m < 10, it can be represented as m1.m2m3m4... where m1, m2 etc. are digits (0-9). TIOS first truncates the mantissa up to 16 digits, or adds trailing zeros on the end of the mantissa up to 16 digits if it is shorter than 16 digits. Then, it stores the integer number m1m2m3...m16 in mantissa field of the bcd structure using packed BCD code (each digit in 4 bits).

Everything will be more clear on a concrete example. Look the number 379.25. It can be written as 3.7925*10^2. So, e is 2, and m is 3.7925. As the number is positive, exponent will contain 0x4000+2 = 0x4002. As the mantissa has less than 16 digits, it must be padded to 3.792500000000000. So, mantissa will contain the integer 3792500000000000, i.e. it will contain 0x3792500000000000 (note the strong correspodence between hex numbers and bcd coded numbers: they have the same digits). So, to assign the value 379.25 to the variable a of type bcd, you can use a.exponent = 0x4002 and a.mantissa = 0x3792500000000000. Standard ANSI types float, double and long double (all of them are the same in TIGCC) are internally organized exactly at the same way in TIGCC, except that from the aspect of the compiler, they are scalars, and bcd is a structure, so you can not simply cast float to bcd and vice versa. For this purpose, use functions (more precise macros) float_to_bcd, bcd_to_float and bcd_var.

Note that due to the condition 1 <= m < 10, the mantissa must be normalized (which means that the first digit of the mantissa must not be zero). The consequence is that mantissa field must always be greater or equal to 0x1000000000000000. You can construct (artifically) structures in which this condition is not satisfied. Some functions will work well with such (unnormalized) numbers, but many of them will not work correctly. So, avoid creating of such illegal values: any unnormalized number may be represented in normalized format. Anyway, don't worry about it: all numbers written using "normal" methods are always normalized: you can create unnormalized numbers only intentionally by direct accessing to mantissa part of a bcd structure.