Loading...
Searching...
No Matches
SchemeIProduct.inl File Reference

Go to the source code of this file.

Functions

SCHEME_I_HD double scheme_i_trunc (double value)
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)
SCHEME_I_HD double schemeIProductRaw (const double *left, const double *right, int length)

Function Documentation

◆ scheme_i_slice_bits()

SCHEME_I_HD int scheme_i_slice_bits ( int length)

Definition at line 13 of file SchemeIProduct.inl.

14{
15 if (length < 1) length = 1;
16 int bits = 0;
17 unsigned long long rest = static_cast<unsigned long long>(length);
18 while (rest > 1) {
19 rest >>= 1;
20 ++bits;
21 }
22 if ((static_cast<unsigned long long>(length) &
23 (static_cast<unsigned long long>(length) - 1ULL)) != 0)
24 ++bits;
25 const int width = (52 - bits) / 2;
26 return width < 1 ? 1 : width;
27}

◆ scheme_i_slice_cap()

SCHEME_I_HD int scheme_i_slice_cap ( const double * values,
int length,
int bits )

Definition at line 100 of file SchemeIProduct.inl.

101{
102 int hi = 0;
103 int lo = 0;
104 bool any = false;
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;
108 int exponent = 0;
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;
113 any = true;
114 }
115 if (!any) return 1;
116 const int span = hi - lo;
117 const int cap = span / step + 4;
118 return cap < 1 ? 1 : cap;
119}

◆ scheme_i_split()

SCHEME_I_HD int scheme_i_split ( const double * values,
int length,
int bits,
double * slices,
int cap )

Definition at line 29 of file SchemeIProduct.inl.

31{
32 double max_abs = 0.0;
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;
37 }
38 if (max_abs == 0.0) {
39 for (int i = 0; i < length; ++i)
40 slices[i] = 0.0;
41 return 1;
42 }
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];
48 int count = 0;
49 while (count < cap) {
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;
55 }
56 if (residual_max == 0.0) break;
57 int exponent = 0;
58 scheme_ii_frexp(residual_max, &exponent);
59 const int shift = exponent - bits;
60 double* slice = slices + static_cast<size_t>(count) * length;
61 bool any = false;
62 for (int i = 0; i < length; ++i) {
63 const double value = residual[i];
64 if (value == 0.0) {
65 slice[i] = 0.0;
66 continue;
67 }
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) {
72 slice[i] = 0.0;
73 continue;
74 }
75 const double scaled = scheme_ii_ldexp(value, -shift);
76 const double digits = scheme_i_trunc(scaled);
77 const double piece = scheme_ii_ldexp(digits, shift);
78 slice[i] = piece;
79 residual[i] = value - piece;
80 if (piece != 0.0) any = true;
81 }
82 if (!any) break;
83 ++count;
84 }
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;
89 }
90 free(residual);
91 if (leftover != 0.0) return -1;
92 if (count == 0) {
93 for (int i = 0; i < length; ++i)
94 slices[i] = 0.0;
95 return 1;
96 }
97 return count;
98}
SCHEME_I_HD double scheme_i_trunc(double value)

◆ scheme_i_trunc()

SCHEME_I_HD double scheme_i_trunc ( double value)

Definition at line 4 of file SchemeIProduct.inl.

5{
6#if defined(__CUDA_ARCH__)
7 return trunc(value);
8#else
9 return std::trunc(value);
10#endif
11}

◆ schemeIProductRaw()

SCHEME_I_HD double schemeIProductRaw ( const double * left,
const double * right,
int length )

Definition at line 121 of file SchemeIProduct.inl.

123{
124 if (length < 0 || (length > 0 && (left == nullptr || right == nullptr)))
125 return scheme_ii_nan();
126 if (length == 0) return 0.0;
127 const int bits = scheme_i_slice_bits(length);
128 int cap = scheme_i_slice_cap(left, length, bits);
129 const int right_cap = scheme_i_slice_cap(right, length, bits);
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 =
144 scheme_i_split(right, length, bits, right_slices, cap);
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)