123{
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) {
138 free(left_slices);
139 free(right_slices);
140 return scheme_ii_nan();
141 }
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;
150 double a_max = 0.0;
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;
155 }
156 int a_exponent = 0;
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;
162 double b_max = 0.0;
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;
167 }
168 int b_exponent = 0;
169 if (b_max != 0.0) scheme_ii_frexp(b_max, &b_exponent);
170 const int b_unit = b_exponent - bits;
171 double dot = 0.0;
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);
175 }
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);
181 }
182 }
183 }
184 free(left_slices);
185 free(right_slices);
186 if (left_count < 0 || right_count < 0) return scheme_ii_nan();
187 return total.value();
188}
SCHEME_I_HD int scheme_i_slice_bits(int length)
SCHEME_I_HD int scheme_i_split(const double *values, int length, int bits, double *slices, int cap)
SCHEME_I_HD int scheme_i_slice_cap(const double *values, int length, int bits)