98 lines
3.0 KiB
C
98 lines
3.0 KiB
C
/* *****************************************************************************
|
|
* bcd/mcs.c -- make a libg1m BCD out of an MCS BCD number.
|
|
* Copyright (C) 2017 Thomas "Cakeisalie5" Touhey <thomas@touhey.fr>
|
|
*
|
|
* This file is part of libg1m.
|
|
* libg1m is free software; you can redistribute it and/or modify it
|
|
* under the terms of the GNU Lesser General Public License as published by
|
|
* the Free Software Foundation; either version 3.0 of the License,
|
|
* or (at your option) any later version.
|
|
*
|
|
* libg1m is distributed in the hope that it will be useful,
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
|
|
* See the GNU Lesser General Public License for more details.
|
|
*
|
|
* You should have received a copy of the GNU Lesser General Public License
|
|
* along with libg1m; if not, see <http://www.gnu.org/licenses/>.
|
|
* ************************************************************************** */
|
|
#include <libg1m/internals.h>
|
|
#define upr(N) (bytes[(N)] >> 4)
|
|
#define lwr(N) (bytes[(N)] & 15)
|
|
|
|
/**
|
|
* g1m_bcd_frommcs:
|
|
* Make a libg1m BCD out of an MCS BCD number.
|
|
*
|
|
* @arg raw the raw BCD.
|
|
* @arg dest the not raw BCD.
|
|
* @return the special bit, for simplicity.
|
|
*/
|
|
|
|
int g1m_bcd_frommcs(const mcs_bcd_t *raw, g1m_bcd_t *bcd)
|
|
{
|
|
const unsigned char *bytes = raw->BCDval;
|
|
|
|
/* get the exponent and correct it */
|
|
int exp = (upr(0) & 7) * 100 + lwr(0) * 10 + upr(1), neg = 0;
|
|
if (exp >= 500) exp -= 500, neg = 1;
|
|
bcd->exp = (char)(exp - 99);
|
|
|
|
/* set the flags */
|
|
bcd->flags = g1m_make_bcdflags((bytes[0] & 0x80) >> 7, neg, 15);
|
|
|
|
/* get the mantissa */
|
|
bcd->mant[0] = lwr(1);
|
|
bcd->mant[1] = upr(2);
|
|
bcd->mant[2] = lwr(2);
|
|
bcd->mant[3] = upr(3);
|
|
bcd->mant[4] = lwr(3);
|
|
bcd->mant[5] = upr(4);
|
|
bcd->mant[6] = lwr(4);
|
|
bcd->mant[7] = upr(5);
|
|
bcd->mant[8] = lwr(5);
|
|
bcd->mant[9] = upr(6);
|
|
bcd->mant[10] = lwr(6);
|
|
bcd->mant[11] = upr(7);
|
|
bcd->mant[12] = lwr(7);
|
|
bcd->mant[13] = upr(8);
|
|
bcd->mant[14] = lwr(8);
|
|
|
|
/* return the special bit */
|
|
return (g1m_bcd_has_special(bcd));
|
|
}
|
|
|
|
/**
|
|
* g1m_bcd_tomcs:
|
|
* Make an MCS BCD number out of a libg1m BCD number.
|
|
*
|
|
* @arg bcd the not raw BCD.
|
|
* @arg raw the raw BCD.
|
|
*/
|
|
|
|
void g1m_bcd_tomcs(const g1m_bcd_t *bcd, mcs_bcd_t *raw)
|
|
{
|
|
int exp = bcd->exp + 99;
|
|
if (g1m_bcd_is_negative(bcd)) exp += 500;
|
|
|
|
/* put the exponent (and first digit of the mantissa) */
|
|
div_t c = div(exp, 100);
|
|
div_t d = div(c.rem, 10);
|
|
raw->BCDval[0] = (c.quot << 4) | d.quot | (!!g1m_bcd_has_special(bcd) << 7);
|
|
raw->BCDval[1] = (d.rem << 4) | bcd->mant[0];
|
|
|
|
/* put the mantissa */
|
|
raw->BCDval[2] = (bcd->mant[1] << 4) | bcd->mant[2];
|
|
raw->BCDval[3] = (bcd->mant[3] << 4) | bcd->mant[4];
|
|
raw->BCDval[4] = (bcd->mant[5] << 4) | bcd->mant[6];
|
|
raw->BCDval[5] = (bcd->mant[7] << 4) | bcd->mant[8];
|
|
raw->BCDval[6] = (bcd->mant[9] << 4) | bcd->mant[10];
|
|
raw->BCDval[7] = (bcd->mant[11] << 4) | bcd->mant[12];
|
|
raw->BCDval[8] = (bcd->mant[13] << 4) | bcd->mant[14];
|
|
|
|
/* set the align zone */
|
|
raw->_align[0] = 0;
|
|
raw->_align[1] = 0;
|
|
raw->_align[2] = 0;
|
|
}
|