30 double* slices,
int cap)
33 for (
int i = 0; i < length; ++i) {
34 if (!scheme_ii_isfinite(values[i]))
return -1;
35 const double magnitude = values[i] < 0.0 ? -values[i] : values[i];
36 if (magnitude > max_abs) max_abs = magnitude;
39 for (
int i = 0; i < length; ++i)
43 double* residual =
static_cast<double*
>(
44 malloc(
sizeof(
double) *
static_cast<size_t>(length)));
45 if (residual ==
nullptr)
return -1;
46 for (
int i = 0; i < length; ++i)
47 residual[i] = values[i];
50 double residual_max = 0.0;
51 for (
int i = 0; i < length; ++i) {
52 const double magnitude =
53 residual[i] < 0.0 ? -residual[i] : residual[i];
54 if (magnitude > residual_max) residual_max = magnitude;
56 if (residual_max == 0.0)
break;
58 scheme_ii_frexp(residual_max, &exponent);
59 const int shift = exponent - bits;
60 double* slice = slices +
static_cast<size_t>(count) * length;
62 for (
int i = 0; i < length; ++i) {
63 const double value = residual[i];
68 int value_exponent = 0;
69 const double magnitude = value < 0.0 ? -value : value;
70 scheme_ii_frexp(magnitude, &value_exponent);
71 if (value_exponent <= shift) {
75 const double scaled = scheme_ii_ldexp(value, -shift);
77 const double piece = scheme_ii_ldexp(digits, shift);
79 residual[i] = value - piece;
80 if (piece != 0.0) any =
true;
85 double leftover = 0.0;
86 for (
int i = 0; i < length; ++i) {
87 const double magnitude = residual[i] < 0.0 ? -residual[i] : residual[i];
88 if (magnitude > leftover) leftover = magnitude;
91 if (leftover != 0.0)
return -1;
93 for (
int i = 0; i < length; ++i)
105 const int step = bits < 1 ? 1 : bits;
106 for (
int i = 0; i < length; ++i) {
107 if (values[i] == 0.0 || !scheme_ii_isfinite(values[i]))
continue;
109 const double magnitude = values[i] < 0.0 ? -values[i] : values[i];
110 scheme_ii_frexp(magnitude, &exponent);
111 if (!any || exponent > hi) hi = exponent;
112 if (!any || exponent < lo) lo = exponent;
116 const int span = hi - lo;
117 const int cap = span / step + 4;
118 return cap < 1 ? 1 : cap;
124 if (length < 0 || (length > 0 && (left ==
nullptr || right ==
nullptr)))
125 return scheme_ii_nan();
126 if (length == 0)
return 0.0;
130 if (right_cap > cap) cap = right_cap;
131 if ((
unsigned)cap > 65535u || (
unsigned)length > 16777216u)
132 return scheme_ii_nan();
133 double* left_slices =
static_cast<double*
>(
134 malloc(
sizeof(
double) *
static_cast<size_t>(cap) * length));
135 double* right_slices =
static_cast<double*
>(
136 malloc(
sizeof(
double) *
static_cast<size_t>(cap) * length));
137 if (left_slices ==
nullptr || right_slices ==
nullptr) {
140 return scheme_ii_nan();
142 const int left_count =
scheme_i_split(left, length, bits, left_slices, cap);
143 const int right_count =
145 ExactProductAccumulator total{};
146 if (left_count > 0 && right_count > 0) {
147 for (
int a = 0; a < left_count; ++a) {
148 const double* a_slice =
149 left_slices +
static_cast<size_t>(a) * length;
151 for (
int i = 0; i < length; ++i) {
152 const double magnitude =
153 a_slice[i] < 0.0 ? -a_slice[i] : a_slice[i];
154 if (magnitude > a_max) a_max = magnitude;
157 if (a_max != 0.0) scheme_ii_frexp(a_max, &a_exponent);
158 const int a_unit = a_exponent - bits;
159 for (
int b = 0; b < right_count; ++b) {
160 const double* b_slice =
161 right_slices +
static_cast<size_t>(b) * length;
163 for (
int i = 0; i < length; ++i) {
164 const double magnitude =
165 b_slice[i] < 0.0 ? -b_slice[i] : b_slice[i];
166 if (magnitude > b_max) b_max = magnitude;
169 if (b_max != 0.0) scheme_ii_frexp(b_max, &b_exponent);
170 const int b_unit = b_exponent - bits;
172 for (
int i = 0; i < length; ++i) {
173 dot += scheme_ii_ldexp(a_slice[i], -a_unit) *
174 scheme_ii_ldexp(b_slice[i], -b_unit);
176 SchemeIIWide coefficient{};
177 coefficient.negative = dot < 0.0;
178 const double magnitude = dot < 0.0 ? -dot : dot;
179 coefficient.limb[0] =
static_cast<uint64_t
>(magnitude);
180 total.add(coefficient, a_unit + b_unit);
186 if (left_count < 0 || right_count < 0)
return scheme_ii_nan();
187 return total.value();