/* * Copyright (C) 1998 by Southwest Research Institute (SwRI) * * All rights reserved under U.S. Copyright Law and International Conventions. * * The development of this Software was supported by contracts NAG5-3148, * NAG5-6855, NAS8-36840, NAG5-2323, and NAG5-7043 issued on behalf of * the United States Government by its National Aeronautics and Space * Administration. Southwest Research Institute grants to the Government, * and others acting on its behalf, a paid-up nonexclusive, irrevocable, * worldwide license to reproduce, prepare derivative works, and perform * publicly and display publicly, by or on behalf of the Government. * Other than those rights granted to the United States Government, no part * of this Software may be reproduced in any form or by any means, electronic * or mechanical, including photocopying, without permission in writing from * Southwest Research Institute. All inquiries should be addressed to: * * Director of Contracts * Southwest Research Institute * P. O. Drawer 28510 * San Antonio, Texas 78228-0510 * * * Use of this Software is governed by the terms of the end user license * agreement, if any, which accompanies or is included with the Software * (the "License Agreement"). An end user will be unable to install any * Software that is accompanied by or includes a License Agreement, unless * the end user first agrees to the terms of the License Agreement. Except * as set forth in the applicable License Agreement, any further copying, * reproduction or distribution of this Software is expressly prohibited. * Installation assistance, product support and maintenance, if any, of the * Software is available from SwRI and/or the Third Party Providers, as the * case may be. * * Disclaimer of Warranty * * SOFTWARE IS WARRANTED, IF AT ALL, IN ACCORDANCE WITH THESE TERMS OF THE * LICENSE AGREEMENT. UNLESS OTHERWISE EXPLICITLY STATED, THIS SOFTWARE IS * PROVIDED "AS IS", IS EXPERIMENTAL, AND IS FOR NON-COMMERCIAL USE ONLY, * AND ALL EXPRESS OR IMPLIED CONDITIONS, REPRESENTATIONS AND WARRANTIES, * INCLUDING ANY IMPLIED WARRANTY OF MERCHANTABILITY, FITNESS FOR A PARTICULAR * PURPOSE, OR NON-INFRINGEMENT, ARE DISCLAIMED, EXCEPT TO THE EXTENT THAT * SUCH DISCLAIMERS ARE HELD TO BE LEGALLY INVALID. * * Limitation of Liability * * SwRI SHALL NOT BE LIABLE FOR ANY DAMAGES SUFFERED AS A RESULT OF USING, * MODIFYING, CONTRIBUTING, COPYING, DISTRIBUTING, OR DOWNLOADING THIS * SOFTWARE. IN NO EVENT SHALL SwRI BE LIABLE FOR ANY INDIRECT, PUNITIVE, * SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGE (INCLUDING LOSS OF BUSINESS, * REVENUE, PROFITS, USE, DATA OR OTHER ECONOMIC ADVANTAGE) HOWEVER IT ARISES, * WHETHER FOR BREACH OF IN TORT, EVEN IF SwRI HAS BEEN PREVIOUSLY ADVISED OF * THE POSSIBILITY OF SUCH DAMAGE. YOU HAVE SOLE RESPONSIBILITY FOR ADEQUATE * PROTECTION AND BACKUP OF DATA AND/OR EQUIPMENT USED IN CONNECTION WITH THE * SOFTWARE AND WILL NOT MAKE A CLAIM AGAINST SwRI FOR LOST DATA, RE-RUN TIME, * INACCURATE OUTPUT, WORK DELAYS OR LOST PROFITS RESULTING FROM THE USE OF * THIS SOFTWARE. YOU AGREE TO HOLD SwRI HARMLESS FROM, AND YOU COVENANT NOT * TO SUE SwRI FOR, ANY CLAIMS BASED ON USING THE SOFTWARE. * * Local Laws: Export Control * * You acknowledge and agree this Software is subject to the U.S. Export * Administration Laws and Regulations. Diversion of such Software contrary * to U.S. law is prohibited. You agree that none of the Software, nor any * direct product therefrom, is being or will be acquired for, shipped, * transferred, or reexported, directly or indirectly, to proscribed or * embargoed countries or their nationals, nor be used for nuclear activities, * chemical biological weapons, or missile projects unless authorized by U.S. * Government. Proscribed countries are set forth in the U.S. Export * Administration Regulations. Countries subject to U.S embargo are: Cuba, * Iran, Iraq, Libya, North Korea, Syria, and the Sudan. This list is subject * to change without further notice from SwRI, and you must comply with the * list as it exists in fact. You certify that you are not on the U.S. * Department of Commerce's Denied Persons List or affiliated lists or on the * U.S. Department of Treasury's Specially Designated Nationals List. You agree * to comply strictly with all U.S. export laws and assume sole responsibilities * for obtaining licenses to export or reexport as may be required. * * General * * These Terms represent the entire understanding relating to the use of the * Software and prevail over any prior or contemporaneous, conflicting or * additional, communications. SwRI can revise these Terms at any time * without notice by updating this posting. * * Trademarks * * The SwRI logo is a trademark of SwRI in the United States and other countries. * */ #ident "@(#) intfp.c 1.18 05/08/19 SwRI" #include #include #include "libbase_idfs.h" /* integer floating point range: 10^-63 to 10^+63 precision: 7 digits (0-16777215) mantissa * 10^exponent +-+-----------------------------------------------+-+-----------+ |3|3|2|2|2|2|2|2|2|2|2|1|1|1|1|1|1|1|1|1|1|1| | | | | | | | | | | |1|0|9|8|7|6|5|4|3|2|1|0|9|8|7|6|5|4|3|2|1|0|9|8|7|6|5|4|3|2|1|0| +-+-----------------------------------------------+-+-----------+ |s| mantissa |s| exponent | +-+-----------------------------------------------+-+-----------+ Exponent: magnitude -> bits 0-5 sign -> bit 6 Mantissa: magnitude -> bits 7-30 sign -> bit 31 */ /******************************************************************************* * * * IR_INTFP2FP SUBROUTINE * * * * DESCRIPTION * * This routine is called to convert the stored integer value back into * * floating point values. Data is stored in integer form for IDFS files. * * * * INPUT VARIABLES * * SDDAS_ULONG intfp the integer value to convert to floating point * * * * USAGE * * x = ir_intfp2fp (intfp) * * * * NECESSARY SUBPROGRAMS * * exp10 () calculates 10 ** x * * ir_tento () calculates 10 ** x (can use this or exp10) * * * * EXTERNAL VARIABLES * * None * * * * INTERNAL VARIABLES * * SDDAS_ULONG m_sgn the sign of the mantissa * * SDDAS_ULONG m_mag the magnitude of the mantissa * * SDDAS_ULONG e_sgn the sign of the exponent * * SDDAS_ULONG e_mag the magnitude of the exponent * * * * SUBSYSTEM * * Display Level * * * ******************************************************************************/ SDDAS_FLOAT ir_intfp2fp (SDDAS_ULONG intfp, SDDAS_CHAR dtype) { SDDAS_DOUBLE sig, exp, mant, ebase; SDDAS_ULONG m_sgn, m_mag, e_sgn, e_mag; SDDAS_FLOAT fp, norm; SDDAS_UCHAR byte1, byte2; /**************************************************************************/ /* The following are the definitions of the universal conversion factors */ /* used: */ /* */ /* sig - the number of significant places in the mantissa. It is */ /* to shift the decimal place of the exponent */ /* ebase - the exponent base */ /* norm - any normalization factor supplied to the result */ /**************************************************************************/ /**************************************************************************/ /* Conversion of stored unsigned integer to floating point value */ /* according to the format. This follows the following flow */ /* */ /* 1. Get the sign of the mantissa */ /* 2. Get the mantissa itself */ /* 3. Get the sign of the exponent */ /* 4. Get the exponent itself */ /* 5. Set the number of significant digits in mantissa for this format */ /* 6. Set the exponent base */ /* */ /* The schemes are */ /* case 2: IDFS 32 bit floating point */ /* case 3: IDFS 64 bit floating point */ /* case 4: */ /* case 5: IDFS 16 bit floating point (PEACE MOMENTS TYPE) */ /* case 6: IDFS 16 bit floating point (SCA INTERBALL TYPE) */ /**************************************************************************/ switch (dtype) { case 2: /* single precision */ m_sgn = (intfp >> 31) & 0x00000001; /* extract mantissa */ m_mag = (intfp >> 7) & 0x00ffffff; e_sgn = (intfp >> 6) & 0x00000001; /* extract exponent */ e_mag = intfp & 0x0000003f; sig = 7.0; ebase = 10.0; norm = 1.0; break; case 3: /* double precision */ m_mag = 0; e_mag = 0; m_sgn = 1; e_sgn = 0; sig = 7; break; case 4: /* half precision 1 */ m_sgn = (intfp >> 15) & 0x00000001; /* extract mantissa */ m_mag = (intfp >> 7) & 0x000000ff; e_sgn = (intfp >> 6) & 0x00000001; /* extract exponent */ e_mag = intfp & 0x0000003f; sig = 2.0; ebase = 10.0; norm = 1.0; break; case 5: /* half precision 2 */ if (intfp != 0) m_mag = ((intfp >> 8) & 0x0000007f) + 128; else m_mag = 0; m_sgn = (intfp >> 15) & 0x00000001; /* extract mantissa */ e_sgn = 0; /* extract exponent */ e_mag = (intfp & 0x000000ff); sig = 128; ebase = 2.0; /* Although the diagram does not show this, MSSL verified that */ /* the mantissa must be divided by 256 since the mantissa is a */ /* fractional mantissa. */ norm = 256.0; break; case 6: /* half precision 2 */ byte1 = (intfp >> 8) & 0x000000ff; byte2 = intfp & 0x000000ff; m_mag = (byte1 == 0) ? byte2 : byte2 + 256; e_sgn = (byte1 >> 7) & 0x01; m_sgn = (byte1 >> 6) & 0x01; e_mag = byte1 & 0x3f; sig = 0.0; ebase = 2.0; norm = (byte1 == 0) ? 1 : 512.0; break; default: m_mag = 0; e_mag = 0; m_sgn = 1; e_sgn = 0; sig = 7; break; } /**************************************************************************/ /* Form the floating point value */ /* */ /* 1. If both mantissa and exponent are 0 then we have to check for */ /* special situations indicated by the sign bits */ /* 1a MS=0, ES=0: value is 0.0 */ /* 1b MS=1, ES=0: value is +infinity */ /* 1b MS=1, ES=1: value is -infinity */ /* 1c MS=0, ES=1: value is invalid */ /* 2. Otherwise */ /* 3. Correct the manitssa for the sign */ /* 4. Correct the exponent for the sign */ /* 5. Convert number to value. Mantissa is always assumed to have the */ /* decimal point to the left of the most significant digit. This is */ /* taken into account by shifting exponent to left by the maximum */ /* number of significant digits the format allows. */ /**************************************************************************/ if ((m_mag == 0) && (e_mag == 0)) /* Special Values */ { if ((m_sgn == 0) && (e_sgn == 0)) /* +0 or -0 */ fp = 0.0; else if ((m_sgn == 1) && (e_sgn == 0)) /* +Infinity */ fp = 3.402823e38; else if ((m_sgn == 1) && (e_sgn == 1)) /* -Infinity */ fp = -3.402823e38; else /* Not a Number */ fp = 0.0; } else { mant = m_mag; exp = e_mag; if (m_sgn == 1) mant = -mant; if (e_sgn == 1) exp = -exp; fp = mant * pow(ebase, -sig + exp) / norm; } return (fp); } /******************************************************************************* * * * IR_TENTO SUBROUTINE * * * * DESCRIPTION * * This routine returns the power of 10 value associated with the * * exponential value passed into this routine as a parameter. * * * * INPUT VARIABLES * * SDDAS_CHAR cvar the power of 10 desired (the exponent value) * * * * USAGE * * x = ir_tento (cvar) * * * * NECESSARY SUBPROGRAMS * * None * * * * EXTERNAL VARIABLES * * None * * * * INTERNAL VARIABLES * * SDDAS_FLOAT ten the value 10 * * SDDAS_FLOAT res the result (passed back to calling routine) * * * * SUBSYSTEM * * Display Level * * * ******************************************************************************/ SDDAS_FLOAT ir_tento (SDDAS_CHAR cvar) { SDDAS_FLOAT ten = 10.0; SDDAS_FLOAT res = 1.0; /**********************************************************************/ /* Negative exponent? Fractional value will result. */ /**********************************************************************/ if (cvar <= 0) { while (cvar++ < 0) res /= ten; } /**********************************************************************/ /* Positive exponent encountered. */ /**********************************************************************/ else { while (cvar-- > 0) res *= ten; } return (res); }