1bf215546Sopenharmony_ci/*
2bf215546Sopenharmony_ci * Copyright © 2018 Intel Corporation
3bf215546Sopenharmony_ci *
4bf215546Sopenharmony_ci * Permission is hereby granted, free of charge, to any person obtaining a
5bf215546Sopenharmony_ci * copy of this software and associated documentation files (the "Software"),
6bf215546Sopenharmony_ci * to deal in the Software without restriction, including without limitation
7bf215546Sopenharmony_ci * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8bf215546Sopenharmony_ci * and/or sell copies of the Software, and to permit persons to whom the
9bf215546Sopenharmony_ci * Software is furnished to do so, subject to the following conditions:
10bf215546Sopenharmony_ci *
11bf215546Sopenharmony_ci * The above copyright notice and this permission notice (including the next
12bf215546Sopenharmony_ci * paragraph) shall be included in all copies or substantial portions of the
13bf215546Sopenharmony_ci * Software.
14bf215546Sopenharmony_ci *
15bf215546Sopenharmony_ci * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16bf215546Sopenharmony_ci * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17bf215546Sopenharmony_ci * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
18bf215546Sopenharmony_ci * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19bf215546Sopenharmony_ci * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20bf215546Sopenharmony_ci * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
21bf215546Sopenharmony_ci * IN THE SOFTWARE.
22bf215546Sopenharmony_ci */
23bf215546Sopenharmony_ci
24bf215546Sopenharmony_ci#ifndef UTIL_BIGMATH_H
25bf215546Sopenharmony_ci#define UTIL_BIGMATH_H
26bf215546Sopenharmony_ci
27bf215546Sopenharmony_ci#include "macros.h"
28bf215546Sopenharmony_ci
29bf215546Sopenharmony_ci#include <assert.h>
30bf215546Sopenharmony_ci#include <stdint.h>
31bf215546Sopenharmony_ci#include <string.h>
32bf215546Sopenharmony_ci
33bf215546Sopenharmony_cistatic inline bool
34bf215546Sopenharmony_ci_ubm_add_u32arr(uint32_t *dst, unsigned dst_len,
35bf215546Sopenharmony_ci                uint32_t *a, unsigned a_len,
36bf215546Sopenharmony_ci                uint32_t *b, unsigned b_len)
37bf215546Sopenharmony_ci{
38bf215546Sopenharmony_ci   uint32_t carry = 0;
39bf215546Sopenharmony_ci   for (unsigned i = 0; i < dst_len; i++) {
40bf215546Sopenharmony_ci      uint64_t sum = carry;
41bf215546Sopenharmony_ci      if (i < a_len)
42bf215546Sopenharmony_ci         sum += a[i];
43bf215546Sopenharmony_ci      if (i < b_len)
44bf215546Sopenharmony_ci         sum += b[i];
45bf215546Sopenharmony_ci      dst[i] = sum;
46bf215546Sopenharmony_ci      carry = sum >> 32;
47bf215546Sopenharmony_ci   }
48bf215546Sopenharmony_ci
49bf215546Sopenharmony_ci   /* Now compute overflow */
50bf215546Sopenharmony_ci
51bf215546Sopenharmony_ci   for (unsigned i = dst_len; i < a_len; i++) {
52bf215546Sopenharmony_ci      if (a[i])
53bf215546Sopenharmony_ci         return true;
54bf215546Sopenharmony_ci   }
55bf215546Sopenharmony_ci
56bf215546Sopenharmony_ci   for (unsigned i = dst_len; i < b_len; i++) {
57bf215546Sopenharmony_ci      if (b[i])
58bf215546Sopenharmony_ci         return true;
59bf215546Sopenharmony_ci   }
60bf215546Sopenharmony_ci
61bf215546Sopenharmony_ci   return carry;
62bf215546Sopenharmony_ci}
63bf215546Sopenharmony_ci#define ubm_add_u32arr(dst, a, b) \
64bf215546Sopenharmony_ci   _ubm_add_u32arr(dst, ARRAY_SIZE(dst), a, ARRAY_SIZE(a), b, ARRAY_SIZE(b))
65bf215546Sopenharmony_ci
66bf215546Sopenharmony_cistatic inline bool
67bf215546Sopenharmony_ci_ubm_mul_u32arr(uint32_t *dst, unsigned dst_len,
68bf215546Sopenharmony_ci                uint32_t *a, unsigned a_len,
69bf215546Sopenharmony_ci                uint32_t *b, unsigned b_len)
70bf215546Sopenharmony_ci{
71bf215546Sopenharmony_ci   memset(dst, 0, dst_len * sizeof(*dst));
72bf215546Sopenharmony_ci
73bf215546Sopenharmony_ci   bool overflow = false;
74bf215546Sopenharmony_ci
75bf215546Sopenharmony_ci   for (unsigned i = 0; i < a_len; i++) {
76bf215546Sopenharmony_ci      uint32_t carry = 0;
77bf215546Sopenharmony_ci      for (unsigned j = 0; j < b_len; j++) {
78bf215546Sopenharmony_ci         /* The maximum values of a[i] and b[i] are UINT32_MAX so the maximum
79bf215546Sopenharmony_ci          * value of tmp is UINT32_MAX * UINT32_MAX.  The maximum value that
80bf215546Sopenharmony_ci          * will fit in tmp is
81bf215546Sopenharmony_ci          *
82bf215546Sopenharmony_ci          *    UINT64_MAX = UINT32_MAX << 32 + UINT32_MAX
83bf215546Sopenharmony_ci          *               = UINT32_MAX * (UINT32_MAX + 1) + UINT32_MAX
84bf215546Sopenharmony_ci          *               = UINT32_MAX * UINT32_MAX + 2 * UINT32_MAX
85bf215546Sopenharmony_ci          *
86bf215546Sopenharmony_ci          * so we're guaranteed that we can add in two more 32-bit values
87bf215546Sopenharmony_ci          * without overflowing tmp.
88bf215546Sopenharmony_ci          */
89bf215546Sopenharmony_ci         uint64_t tmp = (uint64_t)a[i] * (uint64_t)b[j];
90bf215546Sopenharmony_ci         tmp += carry;
91bf215546Sopenharmony_ci         if (i + j < dst_len) {
92bf215546Sopenharmony_ci            tmp += dst[i + j];
93bf215546Sopenharmony_ci            dst[i + j] = tmp;
94bf215546Sopenharmony_ci            carry = tmp >> 32;
95bf215546Sopenharmony_ci         } else {
96bf215546Sopenharmony_ci            /* We're trying to write a value that doesn't fit */
97bf215546Sopenharmony_ci            overflow = overflow || tmp > 0;
98bf215546Sopenharmony_ci            break;
99bf215546Sopenharmony_ci         }
100bf215546Sopenharmony_ci      }
101bf215546Sopenharmony_ci      if (i + b_len < dst_len)
102bf215546Sopenharmony_ci         dst[i + b_len] = carry;
103bf215546Sopenharmony_ci      else
104bf215546Sopenharmony_ci         overflow = overflow || carry > 0;
105bf215546Sopenharmony_ci   }
106bf215546Sopenharmony_ci
107bf215546Sopenharmony_ci   return overflow;
108bf215546Sopenharmony_ci}
109bf215546Sopenharmony_ci#define ubm_mul_u32arr(dst, a, b) \
110bf215546Sopenharmony_ci   _ubm_mul_u32arr(dst, ARRAY_SIZE(dst), a, ARRAY_SIZE(a), b, ARRAY_SIZE(b))
111bf215546Sopenharmony_ci
112bf215546Sopenharmony_ci#endif /* UTIL_BIGMATH_H */
113