Skip to content

Commit 5b6543a

Browse files
kbowers-jumpReisen
authored andcommitted
More minor op count tweaks
1 parent b3925a7 commit 5b6543a

File tree

1 file changed

+16
-18
lines changed

1 file changed

+16
-18
lines changed

program/src/oracle/sort/tmpl/sort_stable.c

Lines changed: 16 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -163,13 +163,23 @@ SORT_IMPL(stable_node)( SORT_KEY_T * x,
163163
/* Note that nl and nr are both at least one at this point so at least
164164
one interation of the loop body is necessary. */
165165

166-
do {
166+
for(;;) { /* Minimal C language operations */
167+
if( SORT_BEFORE( yr[k], yl[j] ) ) {
168+
x[i++] = yr[k++];
169+
if( k>=nr ) { /* append left stragglers (at least one) */ do x[i++] = yl[j++]; while( j<nl ); break; }
170+
} else {
171+
x[i++] = yl[j++];
172+
if( j>=nl ) { /* append right stragglers (at least one) */ do x[i++] = yr[k++]; while( k<nr ); break; }
173+
}
174+
}
175+
176+
# if 0 /* These variants of the above don't seem to produce much better code */
177+
do { /* Single loop exit */
167178
if( SORT_BEFORE( yr[k], yl[j] ) ) x[i++] = yr[k++];
168179
else x[i++] = yl[j++];
169180
} while( (j<nl) && (k<nr) );
170181

171-
# if 0 /* These variants of the above don't seem to produce much better code */
172-
do { /* Minimal mem ops and branching */
182+
do { /* Single loop exit, minimal mem ops and branching */
173183
SORT_KEY_T yj = yl[j];
174184
SORT_KEY_T yk = yr[k];
175185
int c = SORT_BEFORE( yk, yj );
@@ -178,34 +188,22 @@ SORT_IMPL(stable_node)( SORT_KEY_T * x,
178188
k += (SORT_IDX_T) c;
179189
} while( (j<nl) & (k<nr) );
180190

181-
do { /* Minimal mem ops */
191+
do { /* Single loop exit, Minimal mem ops */
182192
SORT_KEY_T yj = yl[j];
183193
SORT_KEY_T yk = yr[k];
184194
if( SORT_BEFORE( yk, yj ) ) x[i++] = yk, k++;
185195
else x[i++] = yj, j++;
186196
} while( (j<nl) && (k<nr) );
187197

188-
do { /* Trinary variant of above */
198+
do { /* Single loop exit, trinary variant of above */
189199
x[i++] = SORT_BEFORE( yr[k], yl[j] ) ? yr[k++] : yl[j++];
190200
} while( (j<nl) && (k<nr) );
191201

192-
for(;;) { /* Minimal mem ops and exit condition tests */
193-
SORT_KEY_T yj = yl[j];
194-
SORT_KEY_T yk = yr[k];
195-
if( SORT_BEFORE( yk, yj ) ) { x[i++] = yk; k++; if( k>=nr ) break; }
196-
else { x[i++] = yj; j++; if( j>=nl ) break; }
197-
}
198-
199-
for(;;) { /* Minimal exit condition tests */
200-
if( SORT_BEFORE( yr[k], yl[j] ) ) { x[i++] = yr[k]; k++; if( k>=nr ) break; }
201-
else { x[i++] = yl[j]; j++; if( j>=nl ) break; }
202-
}
203-
# endif
204-
205202
/* Append any stragglers */
206203

207204
if( j<nl ) do x[i++] = yl[j++]; while( j<nl );
208205
else do x[i++] = yr[k++]; while( k<nr );
206+
# endif
209207

210208
return x;
211209
}

0 commit comments

Comments
 (0)