/*
* m a t h 6 4 . c
* Forth Inspired Command Language - 64 bit math support routines
* Authors: Michael A. Gauland (gaulandm@mdhost.cse.tek.com)
* Larry Hastings (larry@hastings.org)
* John Sadler (john_sadler@alum.mit.edu)
* Created: 25 January 1998
* Rev 2.03: Support for 128 bit DP math. This file really ouught to
* be renamed!
* $Id: double.c,v 1.2 2010/09/12 15:18:07 asau Exp $
*/
/*
* Copyright (c) 1997-2001 John Sadler (john_sadler@alum.mit.edu)
* All rights reserved.
*
* Get the latest Ficl release at http://ficl.sourceforge.net
*
* I am interested in hearing from anyone who uses Ficl. If you have
* a problem, a success story, a defect, an enhancement request, or
* if you would like to contribute to the Ficl release, please
* contact me by email at the address above.
*
* L I C E N S E and D I S C L A I M E R
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
*
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*/
#include "ficl.h"
{
/*
* Once we have the quotient, it's cheaper to calculate the
* remainder this way than with % (mod). --lch
*/
return (result);
}
#else /* FICL_PLATFORM_HAS_2INTEGER */
/*
* ficl2IntegerIsNegative
* Returns TRUE if the specified ficl2Unsigned has its sign bit set.
*/
int
{
return (x.high < 0);
}
/*
* ficl2IntegerNegate
* Negates an ficl2Unsigned by complementing and incrementing.
*/
{
x.low ++;
if (x.low == 0)
x.high++;
return (x);
}
/*
* ficl2UnsignedMultiplyAccumulate
* Mixed precision multiply and accumulate primitive for number building.
* Multiplies ficl2Unsigned u by ficlUnsigned mul and adds ficlUnsigned add.
* Mul is typically the numeric base, and add represents a digit to be
* appended to the growing number.
* Returns the result of the operation
*/
{
return (resultLo);
}
/*
* ficl2IntegerMultiply
* Multiplies a pair of ficlIntegers and returns an ficl2Integer result.
*/
{
if (x < 0) {
x = -x;
}
if (y < 0) {
y = -y;
}
prod = ficl2UnsignedMultiply(x, y);
if (sign > 0)
return (result);
else
return (ficl2IntegerNegate(result));
}
{
if (x.low == INTMAX_MIN)
x.high--;
x.low--;
return (x);
}
{
return (result);
}
/*
* ficl2UnsignedMultiply
* Contributed by:
* Michael A. Gauland gaulandm@mdhost.cse.tek.com
*/
{
while (x != 0) {
if (x & 1) {
}
x >>= 1;
}
return (result);
}
/*
* ficl2UnsignedSubtract
*/
{
}
return (result);
}
/*
* ficl2UnsignedArithmeticShiftLeft
* 64 bit left shift
*/
{
if (x.low & FICL_CELL_HIGH_BIT) {
}
return (result);
}
/*
* ficl2UnsignedArithmeticShiftRight
* 64 bit right shift (unsigned - no sign extend)
*/
{
if (x.high & 1) {
}
return (result);
}
/*
* ficl2UnsignedOr
* 64 bit bitwise OR
*/
{
return (result);
}
/*
* ficl2UnsignedCompare
* Return -1 if x < y; 0 if x==y, and 1 if x > y.
*/
int
{
return (1);
return (-1);
/* High parts are equal */
return (1);
return (-1);
return (0);
}
/*
* ficl2UnsignedDivide
* Portable versions of ficl2Multiply and ficl2Divide in C
* Contributed by:
* Michael A. Gauland gaulandm@mdhost.cse.tek.com
*/
{
subtrahend.low = y;
subtrahend.high = 0;
while ((ficl2UnsignedCompare(subtrahend, q) < 0) &&
}
if (ficl2UnsignedCompare(subtrahend, q) <= 0) {
q = ficl2UnsignedSubtract(q, subtrahend);
}
}
return (result);
}
#endif /* !FICL_PLATFORM_HAS_2INTEGER */
/*
* ficl2IntegerDivideFloored
*
* FROM THE FORTH ANS...
* Floored division is integer division in which the remainder carries
* the sign of the divisor or is zero, and the quotient is rounded to
* its arithmetic floor. Symmetric division is integer division in which
* the remainder carries the sign of the dividend or is zero and the
* quotient is the mathematical quotient rounded towards zero or
* truncated. Examples of each are shown in tables 3.3 and 3.4.
*
* Table 3.3 - Floored Division Example
* Dividend Divisor Remainder Quotient
* -------- ------- --------- --------
* 10 7 3 1
* -10 7 4 -2
* 10 -7 -4 -2
* -10 -7 -3 1
*
*
* Table 3.4 - Symmetric Division Example
* Dividend Divisor Remainder Quotient
* -------- ------- --------- --------
* 10 7 3 1
* -10 7 -3 -1
* 10 -7 3 -1
* -10 -7 -3 1
*/
{
if (ficl2IntegerIsNegative(num)) {
}
if (den < 0) {
}
FICL_2UNSIGNED_GET_LOW(num), u);
if (signQuot < 0) {
}
}
if (signRem < 0)
return (qr);
}
/*
* ficl2IntegerDivideSymmetric
* Divide an ficl2Unsigned by a ficlInteger and return a ficlInteger quotient
* and a ficlInteger remainder. The absolute values of quotient and remainder
* are not affected by the signs of the numerator and denominator
* (the operation is symmetric on the number line)
*/
{
if (ficl2IntegerIsNegative(num)) {
}
if (den < 0) {
}
FICL_2UNSIGNED_GET_LOW(num), u);
if (signRem < 0)
if (signQuot < 0)
return (qr);
}