/*1* Copyright © 2018 Intel Corporation2*3* Permission is hereby granted, free of charge, to any person obtaining a4* copy of this software and associated documentation files (the "Software"),5* to deal in the Software without restriction, including without limitation6* the rights to use, copy, modify, merge, publish, distribute, sublicense,7* and/or sell copies of the Software, and to permit persons to whom the8* Software is furnished to do so, subject to the following conditions:9*10* The above copyright notice and this permission notice (including the next11* paragraph) shall be included in all copies or substantial portions of the12* Software.13*14* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR15* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,16* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL17* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER18* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING19* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS20* IN THE SOFTWARE.21*/2223#ifndef UTIL_BIGMATH_H24#define UTIL_BIGMATH_H2526#include "macros.h"2728#include <assert.h>29#include <stdint.h>30#include <string.h>3132static inline bool33_ubm_add_u32arr(uint32_t *dst, unsigned dst_len,34uint32_t *a, unsigned a_len,35uint32_t *b, unsigned b_len)36{37uint32_t carry = 0;38for (unsigned i = 0; i < dst_len; i++) {39uint64_t sum = carry;40if (i < a_len)41sum += a[i];42if (i < b_len)43sum += b[i];44dst[i] = sum;45carry = sum >> 32;46}4748/* Now compute overflow */4950for (unsigned i = dst_len; i < a_len; i++) {51if (a[i])52return true;53}5455for (unsigned i = dst_len; i < b_len; i++) {56if (b[i])57return true;58}5960return carry;61}62#define ubm_add_u32arr(dst, a, b) \63_ubm_add_u32arr(dst, ARRAY_SIZE(dst), a, ARRAY_SIZE(a), b, ARRAY_SIZE(b))6465static inline bool66_ubm_mul_u32arr(uint32_t *dst, unsigned dst_len,67uint32_t *a, unsigned a_len,68uint32_t *b, unsigned b_len)69{70memset(dst, 0, dst_len * sizeof(*dst));7172bool overflow = false;7374for (unsigned i = 0; i < a_len; i++) {75uint32_t carry = 0;76for (unsigned j = 0; j < b_len; j++) {77/* The maximum values of a[i] and b[i] are UINT32_MAX so the maximum78* value of tmp is UINT32_MAX * UINT32_MAX. The maximum value that79* will fit in tmp is80*81* UINT64_MAX = UINT32_MAX << 32 + UINT32_MAX82* = UINT32_MAX * (UINT32_MAX + 1) + UINT32_MAX83* = UINT32_MAX * UINT32_MAX + 2 * UINT32_MAX84*85* so we're guaranteed that we can add in two more 32-bit values86* without overflowing tmp.87*/88uint64_t tmp = (uint64_t)a[i] * (uint64_t)b[j];89tmp += carry;90if (i + j < dst_len) {91tmp += dst[i + j];92dst[i + j] = tmp;93carry = tmp >> 32;94} else {95/* We're trying to write a value that doesn't fit */96overflow = overflow || tmp > 0;97break;98}99}100if (i + b_len < dst_len)101dst[i + b_len] = carry;102else103overflow = overflow || carry > 0;104}105106return overflow;107}108#define ubm_mul_u32arr(dst, a, b) \109_ubm_mul_u32arr(dst, ARRAY_SIZE(dst), a, ARRAY_SIZE(a), b, ARRAY_SIZE(b))110111#endif /* UTIL_BIGMATH_H */112113114