2828#include " beam_jit_common.hpp"
2929#include " beam_jit_bs.hpp"
3030
31- #include < iterator>
32- #include < numeric>
33-
3431extern " C"
3532{
3633#include " beam_file.h"
@@ -89,41 +86,36 @@ std::vector<BscSegment> beam_jit_bsc_init(const Span<ArgVal> &args) {
8986 return segments;
9087}
9188
92- template < typename It>
93- static auto fold_group ( std::vector<BscSegment> &segs, It first, It last ) {
94- auto &back = segs. emplace_back (*first) ;
89+ static void push_group (std::vector<BscSegment> &segs,
90+ std::vector<BscSegment> &group ) {
91+ BscSegment seg ;
9592
96- back.action = BscSegment::action::ACCUMULATE_FIRST;
93+ if (group.empty ()) {
94+ return ;
95+ }
9796
98- return std::accumulate (std::next (first),
99- last,
100- back.effectiveSize ,
101- [&segs](Sint acc, const BscSegment &seg) {
102- auto &back = segs.emplace_back (seg);
97+ if ((group.back ().flags & BSF_LITTLE) != 0 ) {
98+ std::reverse (group.begin (), group.end ());
99+ }
103100
104- back.action = BscSegment::action::ACCUMULATE;
101+ Sint groupSize = 0 ;
102+ for (size_t i = 0 ; i < group.size (); i++) {
103+ seg = group[i];
104+ groupSize += seg.effectiveSize ;
105+ if (i == 0 ) {
106+ seg.action = BscSegment::action::ACCUMULATE_FIRST;
107+ } else {
108+ seg.action = BscSegment::action::ACCUMULATE;
109+ }
110+ segs.push_back (seg);
111+ }
105112
106- return acc + back.effectiveSize ;
107- });
108- }
113+ seg.type = am_integer;
114+ seg.action = BscSegment::action::STORE;
115+ seg.effectiveSize = groupSize;
116+ segs.push_back (seg);
109117
110- static void push_group (std::vector<BscSegment> &segs,
111- std::vector<BscSegment>::const_iterator start,
112- std::vector<BscSegment>::const_iterator end) {
113- if (start < end) {
114- auto groupSize = ((start->flags & BSF_LITTLE) != 0 )
115- ? fold_group (segs,
116- std::make_reverse_iterator (end),
117- std::make_reverse_iterator (start))
118- : fold_group (segs, start, end);
119-
120- auto &seg = segs.emplace_back ();
121-
122- seg.type = am_integer;
123- seg.action = BscSegment::action::STORE;
124- seg.effectiveSize = groupSize;
125- seg.flags = start->flags ;
126- }
118+ group.clear ();
127119}
128120
129121/*
@@ -166,30 +158,25 @@ static void push_group(std::vector<BscSegment> &segs,
166158std::vector<BscSegment> beam_jit_bsc_combine_segments (
167159 const std::vector<BscSegment> segments) {
168160 std::vector<BscSegment> segs;
169-
170- auto group = segments.cend ();
161+ std::vector<BscSegment> group;
171162 Sint combinedSize = 0 ;
172163
173- for (auto it = segments.cbegin (); it != segments.cend (); it++) {
174- auto &seg = *it;
175-
164+ for (auto seg : segments) {
176165 switch (seg.type ) {
177166 case am_integer: {
178167 if (!(0 < seg.effectiveSize && seg.effectiveSize <= 64 )) {
179168 /* Unknown or too large size. Handle using the default
180169 * DIRECT action. */
181- push_group (segs, group, it);
182- group = segments.cend ();
183-
170+ push_group (segs, group);
184171 segs.push_back (seg);
185172 continue ;
186173 }
187174
188175 /* The current segment has a known size not exceeding 64
189176 * bits. Try to add it to the current group. */
190- if (group == segments.cend ()) {
191- group = it;
192177
178+ if (group.empty ()) {
179+ group.push_back (seg);
193180 combinedSize = seg.effectiveSize ;
194181 continue ;
195182 }
@@ -198,8 +185,9 @@ std::vector<BscSegment> beam_jit_bsc_combine_segments(
198185 * Append the current segment to the group only if it is
199186 * compatible and will fit. */
200187
188+ auto &prev = group.back ();
201189 bool sameEndian =
202- (seg.flags & BSF_LITTLE) == (group-> flags & BSF_LITTLE);
190+ (seg.flags & BSF_LITTLE) == (prev. flags & BSF_LITTLE);
203191
204192 /* Big-endian segments can always be grouped (if the size
205193 * does not exceed 64 bits). Little-endian segments can
@@ -211,31 +199,30 @@ std::vector<BscSegment> beam_jit_bsc_combine_segments(
211199 if (sameEndian && combinedSize + seg.effectiveSize <= 64 &&
212200 suitableSizes) {
213201 combinedSize += seg.effectiveSize ;
202+ group.push_back (seg);
214203 continue ;
215204 }
216205
217206 /*
218207 * Not possible to fit anything more into the group.
219208 * Flush the group and start a new group.
220209 */
221- push_group (segs, group, it);
222- group = it;
210+ push_group (segs, group);
223211
212+ group.push_back (seg);
224213 combinedSize = seg.effectiveSize ;
225214 break ;
226215 }
227216 default :
228- push_group (segs, group, it);
229- group = segments.cend ();
230-
217+ push_group (segs, group);
231218 segs.push_back (seg);
232219 break ;
233220 }
234221 }
235222
236- push_group (segs, group, segments. cend () );
223+ push_group (segs, group);
237224
238- /* Calculate bit offsets for ACCUMULATE and STORE segments . */
225+ /* Calculate bit offsets for each ACCUMULATE segment . */
239226
240227 Uint offset = 0 ;
241228 for (int i = segs.size () - 1 ; i >= 0 ; i--) {
0 commit comments