cake
/
libg1m
Archived
1
0
Fork 0
This repository has been archived on 2024-03-16. You can view files and clone it, but cannot push or open issues or pull requests.
libg1m/src/bcd/cas.c

97 lines
3.1 KiB
C

/* *****************************************************************************
* bcd/cas.c -- make a libg1m BCD out of an CAS 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)] & 0xf0) >> 4)
#define lwr(N) (bytes[(N)] & 0xf)
/**
* g1m_bcd_fromcas:
* Make a libg1m BCD out of an CAS BCD number.
*
* @arg raw the raw BCD.
* @arg dest the not raw BCD.
* @return the special bit, for simplicity.
*/
int g1m_bcd_fromcas(const cas_bcd_t *raw, g1m_bcd_t *bcd)
{
const unsigned char *bytes = raw->mantissa;
/* get the exponent and sign */
bcd->exp = (raw->exponent >> 4) * 10 + (raw->exponent & 0x0F);
if (raw->signinfo & g1m_casbcd_pow_neg)
bcd->exp = -bcd->exp;
/* get the mantissa */
bcd->mant[0] = lwr(0);
bcd->mant[1] = upr(1);
bcd->mant[2] = lwr(1);
bcd->mant[3] = upr(2);
bcd->mant[4] = lwr(2);
bcd->mant[5] = upr(3);
bcd->mant[6] = lwr(3);
bcd->mant[7] = upr(4);
bcd->mant[8] = lwr(4);
bcd->mant[9] = upr(5);
bcd->mant[10] = lwr(5);
bcd->mant[11] = upr(6);
bcd->mant[12] = lwr(6);
bcd->mant[13] = upr(7);
bcd->mant[14] = lwr(7);
/* set the special things and return */
bcd->flags = g1m_make_bcdflags(!!(raw->signinfo & g1m_casbcd_special),
(raw->signinfo & g1m_casbcd_negative) == g1m_casbcd_negative, 15);
return (g1m_bcd_has_special(bcd));
}
/**
* g1m_bcd_tocas:
* Make an CAS BCD number out of a libg1m BCD number.
*
* @arg bcd the not raw BCD.
* @arg raw the raw BCD.
*/
void g1m_bcd_tocas(const g1m_bcd_t *bcd, cas_bcd_t *raw)
{
/* put the exponent */
div_t d = div(abs(bcd->exp), 10);
raw->exponent = (d.quot << 4) | d.rem;
/* put the mantissa */
raw->mantissa[0] = bcd->mant[0];
raw->mantissa[1] = (bcd->mant[1] << 4) | bcd->mant[2];
raw->mantissa[2] = (bcd->mant[3] << 4) | bcd->mant[4];
raw->mantissa[3] = (bcd->mant[5] << 4) | bcd->mant[6];
raw->mantissa[4] = (bcd->mant[7] << 4) | bcd->mant[8];
raw->mantissa[5] = (bcd->mant[9] << 4) | bcd->mant[10];
raw->mantissa[6] = (bcd->mant[11] << 4) | bcd->mant[12];
raw->mantissa[7] = (bcd->mant[13] << 4) | bcd->mant[14];
/* put the flags */
raw->signinfo = 0;
if (bcd->exp < 0)
raw->signinfo |= g1m_casbcd_pow_neg;
if (g1m_bcd_is_negative(bcd))
raw->signinfo |= g1m_casbcd_negative;
if (g1m_bcd_has_special(bcd))
raw->signinfo |= g1m_casbcd_special;
}