SPRUI04F july 2015 – april 2023
Specify Trip Count Values
label .trip minimum value [,maximum value[, factor]]
The .trip directive specifies the value of the trip count. The trip count indicates how many times a loop iterates. The .trip directive is valid within procedures only. Following are descriptions of the .trip directive parameters:
label | The label represents the beginning of the loop. This is a required parameter. |
minimum value | The minimum number of times that the loop can iterate. This is a required parameter. The default is 1. |
maximum value | The maximum number of times that the loop can iterate. The maximum value is an optional parameter. |
factor | The factor used,
along with minimum value and maximum value, to determine the number of
times the loop can iterate. A factor of 2 states that your loop always
executes an even number of times, allowing the compiler to unroll once; this can
improve performance. In this example, the loop executes some multiple of 8, between
8 and 48, times:
|
The factor is optional when the maximum value is specified. |
If the assembly optimizer cannot ensure that the trip count is large enough to pipeline a loop for maximum performance, a pipelined version and an unpipelined version of the same loop are generated. This makes one of the loops a redundant loop. The pipelined or unpipelined loop is executed based on a comparison of the trip count and the number of iterations of the loop that can execute in parallel. If the trip count is greater or equal to the number of parallel iterations, the pipelined loop is executed; otherwise, the unpipelined loop is executed. For more information about redundant loops, see Section 4.7.
You are not required to specify a .trip directive with every loop; however, you should use .trip if you know that a loop iterates some number of times. This generally means that redundant loops are not generated (unless the minimum value is really small) saving code size and execution time.
If you know that a loop always executes the same number of times whenever it is called, define maximum value (where maximum value equals minimum value) as well. The compiler may now be able to unroll your loop thereby increasing performance.
When you are compiling with the interrupt flexibility option (--interrupt_threshold=n), using a .trip maximum value allows the compiler to determine the maximum number of cycles that the loop can execute. Then, the compiler compares that value to the threshold value given by the --interrupt_threshold option. See Section 3.12 for more information.
The .trip directive states that the loop will execute 16, 24, 32, 40 or 48 times when the w_vecsum routine is called.
w_vecsum: .cproc ptr_a, ptr_b, ptr_c, weight, cnt
.reg ai, bi, prod, scaled_prod, ci
.no_mdep
loop: .trip 16, 48, 8
ldh *ptr_a++, ai
ldh *ptr_b++, bi
mpy weight, ai, prod
shr prod, 15, scaled_prod
add scaled_prod, bi, ci
sth ci, *ptr_c++
[cnt] sub cnt, 1, cnt
[cnt] b loop
.endproc