/* ***************************************************************************** * bcd/cas.c -- make a libg1m BCD out of an CAS BCD number. * Copyright (C) 2017 Thomas "Cakeisalie5" Touhey * * 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 . * ************************************************************************** */ #include #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 g1m_casbcd_t *raw, g1m_bcd_t *bcd) { const unsigned char *bytes = raw->g1m_casbcd_mant; /* get the exponent and sign */ bcd->g1m_bcd_exp = (raw->g1m_casbcd_exp >> 4) * 10 + (raw->g1m_casbcd_exp & 0x0F); if (raw->g1m_casbcd_flags & g1m_casbcdflag_pow_neg) bcd->g1m_bcd_exp = -bcd->g1m_bcd_exp; /* get the mantissa */ bcd->g1m_bcd_mant[0] = lwr(0); bcd->g1m_bcd_mant[1] = upr(1); bcd->g1m_bcd_mant[2] = lwr(1); bcd->g1m_bcd_mant[3] = upr(2); bcd->g1m_bcd_mant[4] = lwr(2); bcd->g1m_bcd_mant[5] = upr(3); bcd->g1m_bcd_mant[6] = lwr(3); bcd->g1m_bcd_mant[7] = upr(4); bcd->g1m_bcd_mant[8] = lwr(4); bcd->g1m_bcd_mant[9] = upr(5); bcd->g1m_bcd_mant[10] = lwr(5); bcd->g1m_bcd_mant[11] = upr(6); bcd->g1m_bcd_mant[12] = lwr(6); bcd->g1m_bcd_mant[13] = upr(7); bcd->g1m_bcd_mant[14] = lwr(7); /* set the special things and return */ bcd->g1m_bcd_flags = g1m_make_bcdflags( !!(raw->g1m_casbcd_flags & g1m_casbcdflag_special), (raw->g1m_casbcd_flags & g1m_casbcdflag_negative) == g1m_casbcdflag_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. */ # define g1m_bcd_m g1m_bcd_mant void g1m_bcd_tocas(const g1m_bcd_t *bcd, g1m_casbcd_t *raw) { /* put the exponent */ div_t d = div(abs(bcd->g1m_bcd_exp), 10); raw->g1m_casbcd_exp = (d.quot << 4) | d.rem; /* put the mantissa */ raw->g1m_casbcd_mant[0] = bcd->g1m_bcd_m[0]; raw->g1m_casbcd_mant[1] = (bcd->g1m_bcd_m[1] << 4) | bcd->g1m_bcd_m[2]; raw->g1m_casbcd_mant[2] = (bcd->g1m_bcd_m[3] << 4) | bcd->g1m_bcd_m[4]; raw->g1m_casbcd_mant[3] = (bcd->g1m_bcd_m[5] << 4) | bcd->g1m_bcd_m[6]; raw->g1m_casbcd_mant[4] = (bcd->g1m_bcd_m[7] << 4) | bcd->g1m_bcd_m[8]; raw->g1m_casbcd_mant[5] = (bcd->g1m_bcd_m[9] << 4) | bcd->g1m_bcd_m[10]; raw->g1m_casbcd_mant[6] = (bcd->g1m_bcd_m[11] << 4) | bcd->g1m_bcd_m[12]; raw->g1m_casbcd_mant[7] = (bcd->g1m_bcd_m[13] << 4) | bcd->g1m_bcd_m[14]; /* put the flags */ raw->g1m_casbcd_flags = 0; if (bcd->g1m_bcd_exp < 0) raw->g1m_casbcd_flags |= g1m_casbcdflag_pow_neg; if (g1m_bcd_is_negative(bcd)) raw->g1m_casbcd_flags |= g1m_casbcdflag_negative; if (g1m_bcd_has_special(bcd)) raw->g1m_casbcd_flags |= g1m_casbcdflag_special; }