2016-01-25 16:56:00 +01:00
|
|
|
/****************************************************************************
|
2018-05-29 21:21:26 +02:00
|
|
|
* libs/libc/misc/lib_umul64.c
|
2016-01-25 16:56:00 +01:00
|
|
|
*
|
2021-04-02 09:22:52 +02: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
|
2016-01-25 16:56:00 +01:00
|
|
|
*
|
2021-04-02 09:22:52 +02:00
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
2016-01-25 16:56:00 +01:00
|
|
|
*
|
2021-04-02 09:22:52 +02: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.
|
2016-01-25 16:56:00 +01:00
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Included Files
|
|
|
|
****************************************************************************/
|
|
|
|
|
2016-07-21 22:00:15 +02:00
|
|
|
#include <nuttx/lib/math32.h>
|
2016-01-25 16:56:00 +01:00
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* 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) +
|
2016-01-25 18:19:25 +01:00
|
|
|
* factor2->ls * (factor1->ms << 32) +
|
2016-01-25 16:56:00 +01:00
|
|
|
* factor1->ls * factor2->ls
|
|
|
|
*
|
|
|
|
* Truncated, 64-bit product:
|
|
|
|
* factor1 * factor2 = (factor1->ls * factor2->ms +
|
2016-01-25 18:19:25 +01:00
|
|
|
* factor2->ls * factor1->ms) << 32) +
|
2016-01-25 16:56:00 +01:00
|
|
|
* 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);
|
|
|
|
}
|