Add some utilities to support 64-bit math operations on platforms that do not support long long types. Not yet used anywhere

This commit is contained in:
Gregory Nutt 2016-01-25 09:56:00 -06:00
parent 0e3cd129ae
commit 047ed9e543
8 changed files with 628 additions and 1 deletions

2
arch

@ -1 +1 @@
Subproject commit 7843ff7609a657430a4ba0b86eb3267682d73a30
Subproject commit e6307a4fa94d09845fee43de7c7fce0d379d9207

180
include/nuttx/math32.h Normal file
View File

@ -0,0 +1,180 @@
/****************************************************************************
* include/nuttx/math32.h
*
* Copyright (C) 2016 Gregory Nutt. All rights reserved.
* Author: Gregory Nutt <gnutt@nuttx.org>
*
* 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 documentation and/or other materials provided with the
* distribution.
* 3. Neither the name NuttX nor the names of its contributors may be
* used to endorse or promote products derived from this software
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS 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
* COPYRIGHT OWNER 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.
*
****************************************************************************/
#ifndef __INCLUDE_NUTTX_MATH32_H
#define __INCLUDE_NUTTX_MATH32_H
/****************************************************************************
* Included Files
****************************************************************************/
#include <nuttx/config.h>
#include <stdint.h>
/****************************************************************************
* Public Types
****************************************************************************/
/* These types are useful on platforms that do not support 64-bit types. */
struct int64_s
{
#ifdef CONFIG_ENDIAN_BIG
int32_t ms;
uint32_t ls;
#else
uint32_t ls;
int32_t ms;
#endif
};
struct uint64_s
{
#ifdef CONFIG_ENDIAN_BIG
uint32_t ms;
uint32_t ls;
#else
uint32_t ls;
uint32_t ms;
#endif
};
/****************************************************************************
* Public Data
****************************************************************************/
#ifdef __cplusplus
#define EXTERN extern "C"
extern "C"
{
#else
#define EXTERN extern
#endif
/****************************************************************************
* Public Function Prototypes
****************************************************************************/
/****************************************************************************
* Name: umul32
*
* Description:
* Multiply two 32-bit values, factor1 and factor2, and return the
* full 64-bit product.
*
* Input Parameters:
* factor1 and factor2 - The values to be multiplied
* product - The location to return the product of the two values.
*
****************************************************************************/
void umul32(uint32_t factor1, uint32_t factor2, FAR struct uint64_s *product);
/****************************************************************************
* Name: umul32x64
*
* Description:
* Multiply one 32-bit and one 64-bit values, factor1 and factor2,
* respectively, and return the truncated 64-bit product.
*
* Input Parameters:
* factor1 and factor2 - The values to be multiplied
* product - The location to return the product of the two values.
*
****************************************************************************/
void umul32x64(uint32_t factor1, FAR const struct uint64_s *factor2,
FAR struct uint64_s *product);
/****************************************************************************
* Name: umul64
*
* Description:
* Multiply two 64-bit values, factor1 and factor2, and return the
* truncated 64-bit product.
*
* Input Parameters:
* factor1 and factor2 - The values to be multiplied
* product - The location to return the product of the two values.
*
****************************************************************************/
void umul64(FAR const struct uint64_s *factor1,
FAR const struct uint64_s *factor2,
FAR struct uint64_s *product);
/****************************************************************************
* Name: uadd64
*
* Description:
* Add two 64-bit values and return a 64-bit sum.
*
* Input Parameters:
* term1 and term2 - The values to be added
* sum - The location to return the product of the two values. sum may
* be one of term1 or term2
*
****************************************************************************/
void uadd64(FAR const struct uint64_s *term1,
FAR const struct uint64_s *term2,
FAR struct uint64_s *sum);
/****************************************************************************
* Name: usub64
*
* Description:
* Subtract two 64-bit values and return the 64-bit difference.
*
* Input Parameters:
* minuend - The number from which another number (the Subtrahend) is
* to be subtracted.
* subtrahend - The number that is to be subtracted.
* difference - The location to return the difference of the two values.
* difference may the same as one of minuend or subtrahend.
*
****************************************************************************/
void usub64(FAR const struct uint64_s *minuend,
FAR const struct uint64_s *subtrahend,
FAR struct uint64_s *difference);
#undef EXTERN
#ifdef __cplusplus
}
#endif
#endif /* __INCLUDE_NUTTX_MATH32_H */

View File

@ -38,6 +38,11 @@
CSRCS += lib_stream.c lib_filesem.c lib_utsname.c
CSRCS += lib_tea_encrypt.c lib_tea_decrypt.c
# Support for platforms that do not have long long types
CSRCS += lib_umul32.c lib_umul64.c lib_umul32x64.c
CSRCS += lib_uadd64.c lib_usub64.c
# Add C files that depend on file OR socket descriptors
ifneq ($(CONFIG_NFILE_DESCRIPTORS),0)

80
libc/misc/lib_uadd64.c Normal file
View File

@ -0,0 +1,80 @@
/****************************************************************************
* libc/fixedmath/lib_uadd64.c
*
* Copyright (C) 2016 Gregory Nutt. All rights reserved.
* Author: Gregory Nutt <gnutt@nuttx.org>
*
* 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 documentation and/or other materials provided with the
* distribution.
* 3. Neither the name NuttX nor the names of its contributors may be
* used to endorse or promote products derived from this software
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS 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
* COPYRIGHT OWNER 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.
*
****************************************************************************/
/****************************************************************************
* Included Files
****************************************************************************/
#include <nuttx/math32.h>
/****************************************************************************
* Public Functions
****************************************************************************/
/****************************************************************************
* Name: uadd64
*
* Description:
* Add two 64-bit values and return a 64-bit sum.
*
* Input Parameters:
* term1 and term2 - The values to be added
* sum - The location to return the product of the two values. sum may
* be one of term1 or term2
*
****************************************************************************/
void uadd64(FAR const struct uint64_s *term1,
FAR const struct uint64_s *term2,
FAR struct uint64_s *sum)
{
/* Get the MS part of the sum */
sum->ms = term1->ms + term2->ms;
/* Check for carry, i.e., that is when:
*
* term1->ls + term2->ls > UINT32_MAX
*/
if ((UINT32_MAX - term1->ls) > term2->ls)
{
sum->ms++;
}
/* Get the LS part of the sum */
sum->ls = term1->ls + term2->ls;
}

93
libc/misc/lib_umul32.c Normal file
View File

@ -0,0 +1,93 @@
/****************************************************************************
* libc/fixedmath/lib_umul32.c
*
* Copyright (C) 2016 Gregory Nutt. All rights reserved.
* Author: Gregory Nutt <gnutt@nuttx.org>
*
* 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 documentation and/or other materials provided with the
* distribution.
* 3. Neither the name NuttX nor the names of its contributors may be
* used to endorse or promote products derived from this software
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS 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
* COPYRIGHT OWNER 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.
*
****************************************************************************/
/****************************************************************************
* Included Files
****************************************************************************/
#include <nuttx/math32.h>
/****************************************************************************
* Public Functions
****************************************************************************/
/****************************************************************************
* Name: umul32
*
* Description:
* Multiply two 32-bit values, factor1 and factor2, and return the
* full 64-bit product.
*
* Input Parameters:
* factor1 and factor2 - The values to be multiplied
* product - The location to return the product of the two values.
*
****************************************************************************/
void umul32(uint32_t factor1, uint32_t factor2, FAR struct uint64_s *product)
{
struct uint64_s part2;
uint32_t ms1;
uint32_t ls1;
uint32_t ms2;
uint32_t ls2;
uint32_t tmp;
/* factor1 = ms1 << 16 + ls1
* factor2 = ms2 << 16 + ls2
*/
ms1 = factor1 >> 16;
ls1 = factor1 & 0x0000ffff;
ms2 = factor2 >> 16;
ls2 = factor2 & 0x0000ffff;
/* factor1 * factor2 = (ms1 * ms2 << 32) +
* ls1 * (ms2 << 16) + ls2 * (ms1 << 16) +
* ls1 * ls2
* part1 = (ms1 * ms2 << 32) + ls1 * ls2
* part2 = ls1 * (ms2 << 16) + ls2 * (ms1 << 16)
* factor1 * factor2 = part1 + part2
*/
product->ms = ms1 * ms2;
product->ls = ls1 * ls2;
tmp = ls1 * ms2 + ls2 * ms1;
part2.ms = tmp >> 16;
part2.ls = tmp << 16;
uadd64(product, &part2, product);
}

86
libc/misc/lib_umul32x64.c Normal file
View File

@ -0,0 +1,86 @@
/****************************************************************************
* libc/fixedmath/lib_umul32x64.c
*
* Copyright (C) 2016 Gregory Nutt. All rights reserved.
* Author: Gregory Nutt <gnutt@nuttx.org>
*
* 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 documentation and/or other materials provided with the
* distribution.
* 3. Neither the name NuttX nor the names of its contributors may be
* used to endorse or promote products derived from this software
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS 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
* COPYRIGHT OWNER 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.
*
****************************************************************************/
/****************************************************************************
* Included Files
****************************************************************************/
#include <nuttx/math32.h>
/****************************************************************************
* Public Functions
****************************************************************************/
/****************************************************************************
* Name: umul32x64
*
* Description:
* Multiply one 32-bit and one 64-bit values, factor1 and factor2,
* respectively, and return the truncated 64-bit product.
*
* Input Parameters:
* factor1 and factor2 - The values to be multiplied
* product - The location to return the product of the two values.
*
****************************************************************************/
void umul32x64(uint32_t factor1, FAR const struct uint64_s *factor2,
FAR struct uint64_s *product)
{
struct uint64_s part1;
struct uint64_s part2;
/* factor2 = factor2->ms << 32 + factor2->ls
*
* Full 128-bit product:
* factor1 * factor2 = factor1 * (factor2->ms << 32) +
* factor1 * factor2->ls
*/
/* Get part1 = factor1 * factor2->ms, shifting left by 32-bits
* (truncating to 64-bits)
*/
part1.ms = factor1 * factor2->ms;
part1.ls = 0;
/* Get the full 64-bit part2 = factor1 * factor2->ls */
umul32(factor1, factor2->ls, &part2);
/* The product is then the sum */
uadd64(&part1, &part2, product);
}

101
libc/misc/lib_umul64.c Normal file
View File

@ -0,0 +1,101 @@
/****************************************************************************
* libc/fixedmath/lib_umul64.c
*
* Copyright (C) 2016 Gregory Nutt. All rights reserved.
* Author: Gregory Nutt <gnutt@nuttx.org>
*
* 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 documentation and/or other materials provided with the
* distribution.
* 3. Neither the name NuttX nor the names of its contributors may be
* used to endorse or promote products derived from this software
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS 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
* COPYRIGHT OWNER 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.
*
****************************************************************************/
/****************************************************************************
* Included Files
****************************************************************************/
#include <nuttx/math32.h>
/****************************************************************************
* Public Functions
****************************************************************************/
/****************************************************************************
* Name: umul64
*
* Description:
* Multiply two 64-bit values, factor1 and factor2, and return the
* truncated 64-bit product.
*
* Input Parameters:
* factor1 and factor2 - The values to be multiplied
* product - The location to return the product of the two values.
*
****************************************************************************/
void umul64(FAR const struct uint64_s *factor1,
FAR const struct uint64_s *factor2,
FAR struct uint64_s *product)
{
struct uint64_s part1;
struct uint64_s part2;
/* factor1 = factor1->ms << 32 + factor1->ls
* factor2 = factor2->ms << 32 + factor2->ls
*
* Full 128-bit product:
* factor1 * factor2 = (factor1->ms * factor2->ms << 64) +
* factor1->ls * (factor2->ms << 32) +
* factor2->ls * (factor1->ms << 32) +
* factor1->ls * factor2->ls
*
* Truncated, 64-bit product:
* factor1 * factor2 = (factor1->ls * factor2->ms +
* factor2->ls * factor1->ms) << 32) +
* factor1->ls * factor2->ls
*
* part1 = (factor1->ls * factor2->ms +
* factor2->ls * factor1->ms) << 32)
* part2 = factor1->ls * factor2->ls
* factor1 * factor2 = part1 + part2
*/
/* Get part1 = factor1->ls * factor2->ms + factor2->ls * factor1->ms,
* shifting left by 32-bits (truncating to 64-bits)
*/
part1.ms = factor1->ls * factor2->ms +
factor2->ls * factor2->ms;
part1.ls = 0;
/* Get the full 64-bit part2 = factor1->ls * factor2->ls */
umul32(factor1->ls, factor2->ls, &part2);
/* The product is then the sum */
uadd64(&part1, &part2, product);
}

82
libc/misc/lib_usub64.c Normal file
View File

@ -0,0 +1,82 @@
/****************************************************************************
* libc/fixedmath/lib_usub64.c
*
* Copyright (C) 2016 Gregory Nutt. All rights reserved.
* Author: Gregory Nutt <gnutt@nuttx.org>
*
* 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 documentation and/or other materials provided with the
* distribution.
* 3. Neither the name NuttX nor the names of its contributors may be
* used to endorse or promote products derived from this software
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS 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
* COPYRIGHT OWNER 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.
*
****************************************************************************/
/****************************************************************************
* Included Files
****************************************************************************/
#include <nuttx/math32.h>
/****************************************************************************
* Public Functions
****************************************************************************/
/****************************************************************************
* Name: usub64
*
* Description:
* Subtract two 64-bit values and return the 64-bit difference.
*
* Input Parameters:
* minuend - The number from which another number (the Subtrahend) is
* to be subtracted.
* subtrahend - The number that is to be subtracted.
* difference - The location to return the difference of the two values.
* difference may the same as one of minuend or subtrahend.
*
****************************************************************************/
void usub64(FAR const struct uint64_s *minuend,
FAR const struct uint64_s *subtrahend,
FAR struct uint64_s *difference)
{
/* Get the MS part of the difference */
difference->ms = minuend->ms - subtrahend->ms;
/* Check for a borrow, i.e., that is when:
*
* subtrahend->ls > minuend->ls
*/
if (subtrahend->ls > minuend->ls)
{
difference->ms--;
}
/* Get the LS part of the difference */
difference->ls = minuend->ls - subtrahend->ls;
}