1 /*
2     Copyright (C) 2021 Fredrik Johansson
3 
4     This file is part of Arb.
5 
6     Arb is free software: you can redistribute it and/or modify it under
7     the terms of the GNU Lesser General Public License (LGPL) as published
8     by the Free Software Foundation; either version 2.1 of the License, or
9     (at your option) any later version.  See <http://www.gnu.org/licenses/>.
10 */
11 
12 #include "arb.h"
13 
14 static void
arf_shallow_set_uiui(arf_t res,ulong vhi,ulong vlo)15 arf_shallow_set_uiui(arf_t res, ulong vhi, ulong vlo)
16 {
17     unsigned int bc;
18 
19     if (vhi == 0)
20     {
21         if (vlo == 0)
22         {
23             ARF_XSIZE(res) = 0;
24             ARF_EXP(res) = ARF_EXP_ZERO;
25         }
26         else
27         {
28             count_leading_zeros(bc, vlo);
29             ARF_EXP(res) = FLINT_BITS - bc;
30             ARF_NOPTR_D(res)[0] = vlo << bc;
31             ARF_XSIZE(res) = ARF_MAKE_XSIZE(1, 0);
32         }
33     }
34     else if (vlo == 0)
35     {
36         count_leading_zeros(bc, vhi);
37         ARF_EXP(res) = 2 * FLINT_BITS - bc;
38         ARF_NOPTR_D(res)[0] = vhi << bc;
39         ARF_XSIZE(res) = ARF_MAKE_XSIZE(1, 0);
40     }
41     else
42     {
43         count_leading_zeros(bc, vhi);
44         ARF_EXP(res) = 2 * FLINT_BITS - bc;
45         ARF_NOPTR_D(res)[0] = vlo << bc;
46         if (bc == 0)
47             ARF_NOPTR_D(res)[1] = vhi;
48         else
49             ARF_NOPTR_D(res)[1] = (vhi << bc) | (vlo >> (FLINT_BITS - bc));
50         ARF_XSIZE(res) = ARF_MAKE_XSIZE(2, 0);
51     }
52 }
53 
54 void
arb_dot_uiui(arb_t res,const arb_t initial,int subtract,arb_srcptr x,slong xstep,const ulong * y,slong ystep,slong len,slong prec)55 arb_dot_uiui(arb_t res, const arb_t initial, int subtract, arb_srcptr x, slong xstep, const ulong * y, slong ystep, slong len, slong prec)
56 {
57     arb_ptr t;
58     slong i;
59     ulong vhi, vlo;
60     TMP_INIT;
61 
62     /* todo: fast fma and fmma (len=2) code */
63     if (len <= 1)
64     {
65         if (initial == NULL)
66         {
67             if (len <= 0)
68                 arb_zero(res);
69             else
70             {
71                 arf_t t;
72                 arf_shallow_set_uiui(t, y[1], y[0]);
73                 arb_mul_arf(res, x, t, prec);
74                 if (subtract)
75                     arb_neg(res, res);
76             }
77             return;
78         }
79         else if (len <= 0)
80         {
81             arb_set_round(res, initial, prec);
82             return;
83         }
84     }
85 
86     TMP_START;
87     t = TMP_ALLOC(sizeof(arb_struct) * len);
88 
89     for (i = 0; i < len; i++)
90     {
91         vlo = y[2 * i * ystep];
92         vhi = y[2 * i * ystep + 1];
93 
94         arf_shallow_set_uiui(arb_midref(t + i), vhi, vlo);
95 
96         MAG_EXP(arb_radref(t + i)) = 0;
97         MAG_MAN(arb_radref(t + i)) = 0;
98     }
99 
100     arb_dot(res, initial, subtract, x, xstep, t, 1, len, prec);
101 
102     TMP_END;
103 }
104 
105