2012-11-10 17:06:01 +01:00
|
|
|
/****************************************************************************
|
2018-05-29 21:21:26 +02:00
|
|
|
* libs/libc/math/lib_fixedmath.c
|
2012-11-10 17:06:01 +01:00
|
|
|
*
|
2021-03-02 15:54:21 +01:00
|
|
|
* Licensed to the Apache Software Foundation (ASF) under one or more
|
|
|
|
* contributor license agreements. See the NOTICE file distributed with
|
|
|
|
* this work for additional information regarding copyright ownership. The
|
|
|
|
* ASF licenses this file to you under the Apache License, Version 2.0 (the
|
|
|
|
* "License"); you may not use this file except in compliance with the
|
|
|
|
* License. You may obtain a copy of the License at
|
2012-11-10 17:06:01 +01:00
|
|
|
*
|
2021-03-02 15:54:21 +01:00
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
2012-11-10 17:06:01 +01:00
|
|
|
*
|
2021-03-02 15:54:21 +01:00
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
|
|
|
|
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
|
|
|
|
* License for the specific language governing permissions and limitations
|
|
|
|
* under the License.
|
2012-11-10 17:06:01 +01:00
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Included Files
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
#include <nuttx/config.h>
|
|
|
|
|
|
|
|
#include <stdint.h>
|
|
|
|
#include <stdbool.h>
|
|
|
|
#include <fixedmath.h>
|
|
|
|
|
|
|
|
#ifndef CONFIG_HAVE_LONG_LONG
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Pre-processor Definitions
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Private Type Declarations
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Private Function Prototypes
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Public Data
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Private Data
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: fixsign
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
static void fixsign(b16_t *parg1, b16_t *parg2, bool *pnegate)
|
|
|
|
{
|
|
|
|
bool negate = false;
|
|
|
|
b16_t arg;
|
|
|
|
|
|
|
|
arg = *parg1;
|
|
|
|
if (arg < 0)
|
|
|
|
{
|
|
|
|
*parg1 = -arg;
|
|
|
|
negate = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
arg = *parg2;
|
|
|
|
if (arg < 0)
|
|
|
|
{
|
|
|
|
*parg2 = -arg;
|
|
|
|
negate ^= true;
|
|
|
|
}
|
|
|
|
|
|
|
|
*pnegate = negate;
|
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: adjustsign
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
static b16_t adjustsign(b16_t result, bool negate)
|
|
|
|
{
|
|
|
|
/* If the product is negative, then we overflowed */
|
|
|
|
|
|
|
|
if (result < 0)
|
|
|
|
{
|
|
|
|
if (result)
|
|
|
|
{
|
|
|
|
return b16MIN;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
return b16MAX;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* correct the sign of the result */
|
|
|
|
|
|
|
|
if (negate)
|
|
|
|
{
|
|
|
|
return -result;
|
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Public Functions
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: b16mulb16
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
b16_t b16mulb16(b16_t m1, b16_t m2)
|
|
|
|
{
|
|
|
|
bool negate;
|
|
|
|
b16_t product;
|
|
|
|
|
|
|
|
fixsign(&m1, &m2, &negate);
|
|
|
|
product = (b16_t)ub16mulub16((ub16_t)m1, (ub16_t)m2);
|
|
|
|
return adjustsign(product, negate);
|
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: ub16mulub16
|
2015-10-03 01:43:18 +02:00
|
|
|
****************************************************************************/
|
2012-11-10 17:06:01 +01:00
|
|
|
|
|
|
|
ub16_t ub16mulub16(ub16_t m1, ub16_t m2)
|
|
|
|
{
|
2015-10-04 23:04:00 +02:00
|
|
|
/* Let:
|
|
|
|
*
|
|
|
|
* m1 = m1i*2**16 + m1f (b16)
|
|
|
|
* m2 = m2i*2**16 + m2f (b16)
|
|
|
|
*
|
|
|
|
* Then:
|
|
|
|
*
|
|
|
|
* m1*m2 = (m1i*m2i)*2**32 + (m1i*m2f + m2i*m1f)*2**16 + m1f*m2f (b32)
|
|
|
|
* = (m1i*m2i)*2**16 + (m1i*m2f + m2i*m1f) + m1f*m2f*2**-16 (b16)
|
|
|
|
* = a*2**16 + b + c*2**-16
|
|
|
|
*/
|
2012-11-10 17:06:01 +01:00
|
|
|
|
|
|
|
uint32_t m1i = ((uint32_t)m1 >> 16);
|
|
|
|
uint32_t m2i = ((uint32_t)m1 >> 16);
|
|
|
|
uint32_t m1f = ((uint32_t)m1 & 0x0000ffff);
|
|
|
|
uint32_t m2f = ((uint32_t)m2 & 0x0000ffff);
|
|
|
|
|
|
|
|
return (m1i*m2i << 16) + m1i*m2f + m2i*m1f + (((m1f*m2f) + b16HALF) >> 16);
|
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: b16sqr
|
2015-10-03 01:43:18 +02:00
|
|
|
****************************************************************************/
|
2012-11-10 17:06:01 +01:00
|
|
|
|
|
|
|
b16_t b16sqr(b16_t a)
|
|
|
|
{
|
|
|
|
b16_t sq;
|
|
|
|
|
|
|
|
/* The result is always positive. Just take the absolute value */
|
|
|
|
|
|
|
|
if (a < 0)
|
|
|
|
{
|
|
|
|
a = -a;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Overflow occurred if the result is negative */
|
|
|
|
|
|
|
|
sq = (b16_t)ub16sqr(a);
|
|
|
|
if (sq < 0)
|
|
|
|
{
|
|
|
|
sq = b16MAX;
|
|
|
|
}
|
|
|
|
return sq;
|
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: b16divb16
|
2015-10-03 01:43:18 +02:00
|
|
|
****************************************************************************/
|
2012-11-10 17:06:01 +01:00
|
|
|
|
|
|
|
ub16_t ub16sqr(ub16_t a)
|
|
|
|
{
|
2015-10-04 23:04:00 +02:00
|
|
|
/* Let:
|
|
|
|
*
|
|
|
|
* m = mi*2**16 + mf (b16)
|
|
|
|
*
|
|
|
|
* Then:
|
|
|
|
*
|
|
|
|
* m*m = (mi*mi)*2**32 + 2*(m1*m2)*2**16 + mf*mf (b32)
|
|
|
|
* = (mi*mi)*2**16 + 2*(mi*mf) + mf*mf*2**-16 (b16)
|
|
|
|
*/
|
2012-11-10 17:06:01 +01:00
|
|
|
|
|
|
|
uint32_t mi = ((uint32_t)a >> 16);
|
|
|
|
uint32_t mf = ((uint32_t)a & 0x0000ffff);
|
|
|
|
|
|
|
|
return (mi*mi << 16) + (mi*mf << 1) + ((mf*mf + b16HALF) >> 16);
|
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: b16divb16
|
2015-10-03 01:43:18 +02:00
|
|
|
****************************************************************************/
|
2012-11-10 17:06:01 +01:00
|
|
|
|
|
|
|
b16_t b16divb16(b16_t num, b16_t denom)
|
|
|
|
{
|
|
|
|
bool negate;
|
|
|
|
b16_t quotient;
|
|
|
|
|
|
|
|
fixsign(&num, &denom, &negate);
|
|
|
|
quotient = (b16_t)ub16divub16((ub16_t)num, (ub16_t)denom);
|
|
|
|
return adjustsign(quotient, negate);
|
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: ub16divub16
|
2015-10-03 01:43:18 +02:00
|
|
|
****************************************************************************/
|
2012-11-10 17:06:01 +01:00
|
|
|
|
|
|
|
ub16_t ub16divub16(ub16_t num, ub16_t denom)
|
|
|
|
{
|
|
|
|
uint32_t term1;
|
|
|
|
uint32_t numf;
|
|
|
|
uint32_t product;
|
|
|
|
|
2015-10-04 23:04:00 +02:00
|
|
|
/* Let:
|
|
|
|
*
|
|
|
|
* num = numi*2**16 + numf (b16)
|
|
|
|
* den = deni*2**16 + denf (b16)
|
|
|
|
*
|
|
|
|
* Then:
|
|
|
|
*
|
|
|
|
* num/den = numi*2**16 / den + numf / den (b0)
|
|
|
|
* = numi*2**32 / den + numf*2**16 /den (b16)
|
|
|
|
*/
|
2012-11-10 17:06:01 +01:00
|
|
|
|
|
|
|
/* Check for overflow in the first part of the quotient */
|
|
|
|
|
|
|
|
term1 = ((uint32_t)num & 0xffff0000) / denom;
|
|
|
|
if (term1 >= 0x00010000)
|
|
|
|
{
|
|
|
|
return ub16MAX; /* Will overflow */
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Finish the division */
|
|
|
|
|
|
|
|
numf = num - term1 * denom;
|
|
|
|
term1 <<= 16;
|
|
|
|
product = term1 + (numf + (denom >> 1)) / denom;
|
|
|
|
|
|
|
|
/* Check for overflow */
|
|
|
|
|
|
|
|
if (product < term1)
|
|
|
|
{
|
|
|
|
return ub16MAX; /* Overflowed */
|
|
|
|
}
|
|
|
|
return product;
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif
|