|
| 1 | +/* |
| 2 | +Copyright (c) 2013, Raspberry Pi Foundation |
| 3 | +Copyright (c) 2013, RISC OS Open Ltd |
| 4 | +All rights reserved. |
| 5 | +
|
| 6 | +Redistribution and use in source and binary forms, with or without |
| 7 | +modification, are permitted provided that the following conditions are met: |
| 8 | + * Redistributions of source code must retain the above copyright |
| 9 | + notice, this list of conditions and the following disclaimer. |
| 10 | + * Redistributions in binary form must reproduce the above copyright |
| 11 | + notice, this list of conditions and the following disclaimer in the |
| 12 | + documentation and/or other materials provided with the distribution. |
| 13 | + * Neither the name of the copyright holder nor the |
| 14 | + names of its contributors may be used to endorse or promote products |
| 15 | + derived from this software without specific prior written permission. |
| 16 | +
|
| 17 | +THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND |
| 18 | +ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED |
| 19 | +WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE |
| 20 | +DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY |
| 21 | +DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES |
| 22 | +(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; |
| 23 | +LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND |
| 24 | +ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 25 | +(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS |
| 26 | +SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 27 | +*/ |
| 28 | + |
| 29 | +.macro myfunc fname |
| 30 | + .func fname |
| 31 | + .global fname |
| 32 | +fname: |
| 33 | +.endm |
| 34 | + |
| 35 | +.macro preload_leading_step1 backwards, ptr, base |
| 36 | +/* If the destination is already 16-byte aligned, then we need to preload |
| 37 | + * between 0 and prefetch_distance (inclusive) cache lines ahead so there |
| 38 | + * are no gaps when the inner loop starts. |
| 39 | + */ |
| 40 | + .if backwards |
| 41 | + sub ptr, base, #1 |
| 42 | + bic ptr, ptr, #31 |
| 43 | + .else |
| 44 | + bic ptr, base, #31 |
| 45 | + .endif |
| 46 | + .set OFFSET, 0 |
| 47 | + .rept prefetch_distance+1 |
| 48 | + pld [ptr, #OFFSET] |
| 49 | + .if backwards |
| 50 | + .set OFFSET, OFFSET-32 |
| 51 | + .else |
| 52 | + .set OFFSET, OFFSET+32 |
| 53 | + .endif |
| 54 | + .endr |
| 55 | +.endm |
| 56 | + |
| 57 | +.macro preload_leading_step2 backwards, ptr, base, leading_bytes, tmp |
| 58 | +/* However, if the destination is not 16-byte aligned, we may need to |
| 59 | + * preload one more cache line than that. The question we need to ask is: |
| 60 | + * are the leading bytes more than the amount by which the source |
| 61 | + * pointer will be rounded down for preloading, and if so, by how many |
| 62 | + * cache lines? |
| 63 | + */ |
| 64 | + .if backwards |
| 65 | +/* Here we compare against how many bytes we are into the |
| 66 | + * cache line, counting down from the highest such address. |
| 67 | + * Effectively, we want to calculate |
| 68 | + * leading_bytes = dst&15 |
| 69 | + * cacheline_offset = 31-((src-leading_bytes-1)&31) |
| 70 | + * extra_needed = leading_bytes - cacheline_offset |
| 71 | + * and test if extra_needed is <= 0, or rearranging: |
| 72 | + * leading_bytes + (src-leading_bytes-1)&31 <= 31 |
| 73 | + */ |
| 74 | + mov tmp, base, lsl #32-5 |
| 75 | + sbc tmp, tmp, leading_bytes, lsl #32-5 |
| 76 | + adds tmp, tmp, leading_bytes, lsl #32-5 |
| 77 | + bcc 61f |
| 78 | + pld [ptr, #-32*(prefetch_distance+1)] |
| 79 | + .else |
| 80 | +/* Effectively, we want to calculate |
| 81 | + * leading_bytes = (-dst)&15 |
| 82 | + * cacheline_offset = (src+leading_bytes)&31 |
| 83 | + * extra_needed = leading_bytes - cacheline_offset |
| 84 | + * and test if extra_needed is <= 0. |
| 85 | + */ |
| 86 | + mov tmp, base, lsl #32-5 |
| 87 | + add tmp, tmp, leading_bytes, lsl #32-5 |
| 88 | + rsbs tmp, tmp, leading_bytes, lsl #32-5 |
| 89 | + bls 61f |
| 90 | + pld [ptr, #32*(prefetch_distance+1)] |
| 91 | + .endif |
| 92 | +61: |
| 93 | +.endm |
| 94 | + |
| 95 | +.macro preload_trailing backwards, base, remain, tmp |
| 96 | + /* We need either 0, 1 or 2 extra preloads */ |
| 97 | + .if backwards |
| 98 | + rsb tmp, base, #0 |
| 99 | + mov tmp, tmp, lsl #32-5 |
| 100 | + .else |
| 101 | + mov tmp, base, lsl #32-5 |
| 102 | + .endif |
| 103 | + adds tmp, tmp, remain, lsl #32-5 |
| 104 | + adceqs tmp, tmp, #0 |
| 105 | + /* The instruction above has two effects: ensures Z is only |
| 106 | + * set if C was clear (so Z indicates that both shifted quantities |
| 107 | + * were 0), and clears C if Z was set (so C indicates that the sum |
| 108 | + * of the shifted quantities was greater and not equal to 32) */ |
| 109 | + beq 82f |
| 110 | + .if backwards |
| 111 | + sub tmp, base, #1 |
| 112 | + bic tmp, tmp, #31 |
| 113 | + .else |
| 114 | + bic tmp, base, #31 |
| 115 | + .endif |
| 116 | + bcc 81f |
| 117 | + .if backwards |
| 118 | + pld [tmp, #-32*(prefetch_distance+1)] |
| 119 | +81: |
| 120 | + pld [tmp, #-32*prefetch_distance] |
| 121 | + .else |
| 122 | + pld [tmp, #32*(prefetch_distance+2)] |
| 123 | +81: |
| 124 | + pld [tmp, #32*(prefetch_distance+1)] |
| 125 | + .endif |
| 126 | +82: |
| 127 | +.endm |
| 128 | + |
| 129 | +.macro preload_all backwards, narrow_case, shift, base, remain, tmp0, tmp1 |
| 130 | + .if backwards |
| 131 | + sub tmp0, base, #1 |
| 132 | + bic tmp0, tmp0, #31 |
| 133 | + pld [tmp0] |
| 134 | + sub tmp1, base, remain, lsl #shift |
| 135 | + .else |
| 136 | + bic tmp0, base, #31 |
| 137 | + pld [tmp0] |
| 138 | + add tmp1, base, remain, lsl #shift |
| 139 | + sub tmp1, tmp1, #1 |
| 140 | + .endif |
| 141 | + bic tmp1, tmp1, #31 |
| 142 | + cmp tmp1, tmp0 |
| 143 | + beq 92f |
| 144 | + .if narrow_case |
| 145 | + /* In this case, all the data fits in either 1 or 2 cache lines */ |
| 146 | + pld [tmp1] |
| 147 | + .else |
| 148 | +91: |
| 149 | + .if backwards |
| 150 | + sub tmp0, tmp0, #32 |
| 151 | + .else |
| 152 | + add tmp0, tmp0, #32 |
| 153 | + .endif |
| 154 | + cmp tmp0, tmp1 |
| 155 | + pld [tmp0] |
| 156 | + bne 91b |
| 157 | + .endif |
| 158 | +92: |
| 159 | +.endm |
0 commit comments