Convert decimal to its binary fixed-length representation two representations of the same length can be compared with memcmp with the correct -1/0/+1 result SYNOPSIS decimal2bin() from - value to convert to - points to buffer where string representation should be stored precision/scale - see decimal_bin_size() below NOTE the buffer is assumed to be of th
| 1263 | 7E F2 04 C7 2D FB 2D |
| 1264 | */ |
| 1265 | int decimal2bin(decimal_t *from, uchar *to, int precision, int frac) |
| 1266 | { |
| 1267 | dec1 mask=from->sign ? -1 : 0, *buf1=from->buf, *stop1; |
| 1268 | int error=E_DEC_OK, intg=precision-frac, |
| 1269 | isize1, intg1, intg1x, from_intg, |
| 1270 | intg0=intg/DIG_PER_DEC1, |
| 1271 | frac0=frac/DIG_PER_DEC1, |
| 1272 | intg0x=intg-intg0*DIG_PER_DEC1, |
| 1273 | frac0x=frac-frac0*DIG_PER_DEC1, |
| 1274 | frac1=from->frac/DIG_PER_DEC1, |
| 1275 | frac1x=from->frac-frac1*DIG_PER_DEC1, |
| 1276 | isize0=intg0*sizeof(dec1)+dig2bytes[intg0x], |
| 1277 | fsize0=frac0*sizeof(dec1)+dig2bytes[frac0x], |
| 1278 | fsize1=frac1*sizeof(dec1)+dig2bytes[frac1x]; |
| 1279 | const int orig_isize0= isize0; |
| 1280 | const int orig_fsize0= fsize0; |
| 1281 | uchar *orig_to= to; |
| 1282 | |
| 1283 | buf1= remove_leading_zeroes(from, &from_intg); |
| 1284 | |
| 1285 | if (unlikely(from_intg+fsize1==0)) |
| 1286 | { |
| 1287 | mask=0; /* just in case */ |
| 1288 | intg=1; |
| 1289 | buf1=&mask; |
| 1290 | } |
| 1291 | |
| 1292 | intg1=from_intg/DIG_PER_DEC1; |
| 1293 | intg1x=from_intg-intg1*DIG_PER_DEC1; |
| 1294 | isize1=intg1*sizeof(dec1)+dig2bytes[intg1x]; |
| 1295 | |
| 1296 | if (intg < from_intg) |
| 1297 | { |
| 1298 | buf1+=intg1-intg0+(intg1x>0)-(intg0x>0); |
| 1299 | intg1=intg0; intg1x=intg0x; |
| 1300 | error=E_DEC_OVERFLOW; |
| 1301 | } |
| 1302 | else if (isize0 > isize1) |
| 1303 | { |
| 1304 | while (isize0-- > isize1) |
| 1305 | *to++= (char)mask; |
| 1306 | } |
| 1307 | if (fsize0 < fsize1) |
| 1308 | { |
| 1309 | frac1=frac0; frac1x=frac0x; |
| 1310 | error=E_DEC_TRUNCATED; |
| 1311 | } |
| 1312 | else if (fsize0 > fsize1 && frac1x) |
| 1313 | { |
| 1314 | if (frac0 == frac1) |
| 1315 | { |
| 1316 | frac1x=frac0x; |
| 1317 | fsize0= fsize1; |
| 1318 | } |
| 1319 | else |
| 1320 | { |
| 1321 | frac1++; |
| 1322 | frac1x=0; |
no test coverage detected