Loading...
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 | /* SPDX-License-Identifier: GPL-2.0-only */ /* * Copyright (c) 2010-2011, The Linux Foundation. All rights reserved. */ /* * Description * * library function for memcpy where length bytes are copied from * ptr_in to ptr_out. ptr_out is returned unchanged. * Allows any combination of alignment on input and output pointers * and length from 0 to 2^32-1 * * Restrictions * The arrays should not overlap, the program will produce undefined output * if they do. * For blocks less than 16 bytes a byte by byte copy is performed. For * 8byte alignments, and length multiples, a dword copy is performed up to * 96bytes * History * * DJH 5/15/09 Initial version 1.0 * DJH 6/ 1/09 Version 1.1 modified ABI to inlcude R16-R19 * DJH 7/12/09 Version 1.2 optimized codesize down to 760 was 840 * DJH 10/14/09 Version 1.3 added special loop for aligned case, was * overreading bloated codesize back up to 892 * DJH 4/20/10 Version 1.4 fixed Ldword_loop_epilog loop to prevent loads * occurring if only 1 left outstanding, fixes bug * # 3888, corrected for all alignments. Peeled off * 1 32byte chunk from kernel loop and extended 8byte * loop at end to solve all combinations and prevent * over read. Fixed Ldword_loop_prolog to prevent * overread for blocks less than 48bytes. Reduced * codesize to 752 bytes * DJH 4/21/10 version 1.5 1.4 fix broke code for input block ends not * aligned to dword boundaries,underwriting by 1 * byte, added detection for this and fixed. A * little bloat. * DJH 4/23/10 version 1.6 corrected stack error, R20 was not being restored * always, fixed the error of R20 being modified * before it was being saved * Natural c model * =============== * void * memcpy(char * ptr_out, char * ptr_in, int length) { * int i; * if(length) for(i=0; i < length; i++) { ptr_out[i] = ptr_in[i]; } * return(ptr_out); * } * * Optimized memcpy function * ========================= * void * memcpy(char * ptr_out, char * ptr_in, int len) { * int i, prolog, kernel, epilog, mask; * u8 offset; * s64 data0, dataF8, data70; * * s64 * ptr8_in; * s64 * ptr8_out; * s32 * ptr4; * s16 * ptr2; * * offset = ((int) ptr_in) & 7; * ptr8_in = (s64 *) &ptr_in[-offset]; //read in the aligned pointers * * data70 = *ptr8_in++; * dataF8 = *ptr8_in++; * * data0 = HEXAGON_P_valignb_PPp(dataF8, data70, offset); * * prolog = 32 - ((int) ptr_out); * mask = 0x7fffffff >> HEXAGON_R_cl0_R(len); * prolog = prolog & mask; * kernel = len - prolog; * epilog = kernel & 0x1F; * kernel = kernel>>5; * * if (prolog & 1) { ptr_out[0] = (u8) data0; data0 >>= 8; ptr_out += 1;} * ptr2 = (s16 *) &ptr_out[0]; * if (prolog & 2) { ptr2[0] = (u16) data0; data0 >>= 16; ptr_out += 2;} * ptr4 = (s32 *) &ptr_out[0]; * if (prolog & 4) { ptr4[0] = (u32) data0; data0 >>= 32; ptr_out += 4;} * * offset = offset + (prolog & 7); * if (offset >= 8) { * data70 = dataF8; * dataF8 = *ptr8_in++; * } * offset = offset & 0x7; * * prolog = prolog >> 3; * if (prolog) for (i=0; i < prolog; i++) { * data0 = HEXAGON_P_valignb_PPp(dataF8, data70, offset); * ptr8_out = (s64 *) &ptr_out[0]; *ptr8_out = data0; ptr_out += 8; * data70 = dataF8; * dataF8 = *ptr8_in++; * } * if(kernel) { kernel -= 1; epilog += 32; } * if(kernel) for(i=0; i < kernel; i++) { * data0 = HEXAGON_P_valignb_PPp(dataF8, data70, offset); * ptr8_out = (s64 *) &ptr_out[0]; *ptr8_out = data0; ptr_out += 8; * data70 = *ptr8_in++; * * data0 = HEXAGON_P_valignb_PPp(data70, dataF8, offset); * ptr8_out = (s64 *) &ptr_out[0]; *ptr8_out = data0; ptr_out += 8; * dataF8 = *ptr8_in++; * * data0 = HEXAGON_P_valignb_PPp(dataF8, data70, offset); * ptr8_out = (s64 *) &ptr_out[0]; *ptr8_out = data0; ptr_out += 8; * data70 = *ptr8_in++; * * data0 = HEXAGON_P_valignb_PPp(data70, dataF8, offset); * ptr8_out = (s64 *) &ptr_out[0]; *ptr8_out = data0; ptr_out += 8; * dataF8 = *ptr8_in++; * } * epilogdws = epilog >> 3; * if (epilogdws) for (i=0; i < epilogdws; i++) { * data0 = HEXAGON_P_valignb_PPp(dataF8, data70, offset); * ptr8_out = (s64 *) &ptr_out[0]; *ptr8_out = data0; ptr_out += 8; * data70 = dataF8; * dataF8 = *ptr8_in++; * } * data0 = HEXAGON_P_valignb_PPp(dataF8, data70, offset); * * ptr4 = (s32 *) &ptr_out[0]; * if (epilog & 4) { ptr4[0] = (u32) data0; data0 >>= 32; ptr_out += 4;} * ptr2 = (s16 *) &ptr_out[0]; * if (epilog & 2) { ptr2[0] = (u16) data0; data0 >>= 16; ptr_out += 2;} * if (epilog & 1) { *ptr_out++ = (u8) data0; } * * return(ptr_out - length); * } * * Codesize : 784 bytes */ #define ptr_out R0 /* destination pounter */ #define ptr_in R1 /* source pointer */ #define len R2 /* length of copy in bytes */ #define data70 R13:12 /* lo 8 bytes of non-aligned transfer */ #define dataF8 R11:10 /* hi 8 bytes of non-aligned transfer */ #define ldata0 R7:6 /* even 8 bytes chunks */ #define ldata1 R25:24 /* odd 8 bytes chunks */ #define data1 R7 /* lower 8 bytes of ldata1 */ #define data0 R6 /* lower 8 bytes of ldata0 */ #define ifbyte p0 /* if transfer has bytes in epilog/prolog */ #define ifhword p0 /* if transfer has shorts in epilog/prolog */ #define ifword p0 /* if transfer has words in epilog/prolog */ #define noprolog p0 /* no prolog, xfer starts at 32byte */ #define nokernel p1 /* no 32byte multiple block in the transfer */ #define noepilog p0 /* no epilog, xfer ends on 32byte boundary */ #define align p2 /* alignment of input rel to 8byte boundary */ #define kernel1 p0 /* kernel count == 1 */ #define dalign R25 /* rel alignment of input to output data */ #define star3 R16 /* number bytes in prolog - dwords */ #define rest R8 /* length - prolog bytes */ #define back R7 /* nr bytes > dword boundary in src block */ #define epilog R3 /* bytes in epilog */ #define inc R15:14 /* inc kernel by -1 and defetch ptr by 32 */ #define kernel R4 /* number of 32byte chunks in kernel */ #define ptr_in_p_128 R5 /* pointer for prefetch of input data */ #define mask R8 /* mask used to determine prolog size */ #define shift R8 /* used to work a shifter to extract bytes */ #define shift2 R5 /* in epilog to workshifter to extract bytes */ #define prolog R15 /* bytes in prolog */ #define epilogdws R15 /* number dwords in epilog */ #define shiftb R14 /* used to extract bytes */ #define offset R9 /* same as align in reg */ #define ptr_out_p_32 R17 /* pointer to output dczero */ #define align888 R14 /* if simple dword loop can be used */ #define len8 R9 /* number of dwords in length */ #define over R20 /* nr of bytes > last inp buf dword boundary */ #define ptr_in_p_128kernel R5:4 /* packed fetch pointer & kernel cnt */ .section .text .p2align 4 .global memcpy .type memcpy, @function memcpy: { p2 = cmp.eq(len, #0); /* =0 */ align888 = or(ptr_in, ptr_out); /* %8 < 97 */ p0 = cmp.gtu(len, #23); /* %1, <24 */ p1 = cmp.eq(ptr_in, ptr_out); /* attempt to overwrite self */ } { p1 = or(p2, p1); p3 = cmp.gtu(len, #95); /* %8 < 97 */ align888 = or(align888, len); /* %8 < 97 */ len8 = lsr(len, #3); /* %8 < 97 */ } { dcfetch(ptr_in); /* zero/ptrin=ptrout causes fetch */ p2 = bitsclr(align888, #7); /* %8 < 97 */ if(p1) jumpr r31; /* =0 */ } { p2 = and(p2,!p3); /* %8 < 97 */ if (p2.new) len = add(len, #-8); /* %8 < 97 */ if (p2.new) jump:NT .Ldwordaligned; /* %8 < 97 */ } { if(!p0) jump .Lbytes23orless; /* %1, <24 */ mask.l = #LO(0x7fffffff); /* all bytes before line multiples of data */ prolog = sub(#0, ptr_out); } { /* save r31 on stack, decrement sp by 16 */ allocframe(#24); mask.h = #HI(0x7fffffff); ptr_in_p_128 = add(ptr_in, #32); back = cl0(len); } { memd(sp+#0) = R17:16; /* save r16,r17 on stack6 */ r31.l = #LO(.Lmemcpy_return); /* set up final return pointer */ prolog &= lsr(mask, back); offset = and(ptr_in, #7); } { memd(sp+#8) = R25:24; /* save r25,r24 on stack */ dalign = sub(ptr_out, ptr_in); r31.h = #HI(.Lmemcpy_return); /* set up final return pointer */ } { /* see if there if input buffer end if aligned */ over = add(len, ptr_in); back = add(len, offset); memd(sp+#16) = R21:20; /* save r20,r21 on stack */ } { noprolog = bitsclr(prolog, #7); prolog = and(prolog, #31); dcfetch(ptr_in_p_128); ptr_in_p_128 = add(ptr_in_p_128, #32); } { kernel = sub(len, prolog); shift = asl(prolog, #3); star3 = and(prolog, #7); ptr_in = and(ptr_in, #-8); } { prolog = lsr(prolog, #3); epilog = and(kernel, #31); ptr_out_p_32 = add(ptr_out, prolog); over = and(over, #7); } { p3 = cmp.gtu(back, #8); kernel = lsr(kernel, #5); dcfetch(ptr_in_p_128); ptr_in_p_128 = add(ptr_in_p_128, #32); } { p1 = cmp.eq(prolog, #0); if(!p1.new) prolog = add(prolog, #1); dcfetch(ptr_in_p_128); /* reserve the line 64bytes on */ ptr_in_p_128 = add(ptr_in_p_128, #32); } { nokernel = cmp.eq(kernel,#0); dcfetch(ptr_in_p_128); /* reserve the line 64bytes on */ ptr_in_p_128 = add(ptr_in_p_128, #32); shiftb = and(shift, #8); } { dcfetch(ptr_in_p_128); /* reserve the line 64bytes on */ ptr_in_p_128 = add(ptr_in_p_128, #32); if(nokernel) jump .Lskip64; p2 = cmp.eq(kernel, #1); /* skip ovr if kernel == 0 */ } { dczeroa(ptr_out_p_32); /* don't advance pointer */ if(!p2) ptr_out_p_32 = add(ptr_out_p_32, #32); } { dalign = and(dalign, #31); dczeroa(ptr_out_p_32); } .Lskip64: { data70 = memd(ptr_in++#16); if(p3) dataF8 = memd(ptr_in+#8); if(noprolog) jump .Lnoprolog32; align = offset; } /* upto initial 7 bytes */ { ldata0 = valignb(dataF8, data70, align); ifbyte = tstbit(shift,#3); offset = add(offset, star3); } { if(ifbyte) memb(ptr_out++#1) = data0; ldata0 = lsr(ldata0, shiftb); shiftb = and(shift, #16); ifhword = tstbit(shift,#4); } { if(ifhword) memh(ptr_out++#2) = data0; ldata0 = lsr(ldata0, shiftb); ifword = tstbit(shift,#5); p2 = cmp.gtu(offset, #7); } { if(ifword) memw(ptr_out++#4) = data0; if(p2) data70 = dataF8; if(p2) dataF8 = memd(ptr_in++#8); /* another 8 bytes */ align = offset; } .Lnoprolog32: { p3 = sp1loop0(.Ldword_loop_prolog, prolog) rest = sub(len, star3); /* whats left after the loop */ p0 = cmp.gt(over, #0); } if(p0) rest = add(rest, #16); .Ldword_loop_prolog: { if(p3) memd(ptr_out++#8) = ldata0; ldata0 = valignb(dataF8, data70, align); p0 = cmp.gt(rest, #16); } { data70 = dataF8; if(p0) dataF8 = memd(ptr_in++#8); rest = add(rest, #-8); }:endloop0 .Lkernel: { /* kernel is at least 32bytes */ p3 = cmp.gtu(kernel, #0); /* last itn. remove edge effects */ if(p3.new) kernel = add(kernel, #-1); /* dealt with in last dword loop */ if(p3.new) epilog = add(epilog, #32); } { nokernel = cmp.eq(kernel, #0); /* after adjustment, recheck */ if(nokernel.new) jump:NT .Lepilog; /* likely not taken */ inc = combine(#32, #-1); p3 = cmp.gtu(dalign, #24); } { if(p3) jump .Lodd_alignment; } { loop0(.Loword_loop_25to31, kernel); kernel1 = cmp.gtu(kernel, #1); rest = kernel; } .falign .Loword_loop_25to31: { dcfetch(ptr_in_p_128); /* prefetch 4 lines ahead */ if(kernel1) ptr_out_p_32 = add(ptr_out_p_32, #32); } { dczeroa(ptr_out_p_32); /* reserve the next 32bytes in cache */ p3 = cmp.eq(kernel, rest); } { /* kernel -= 1 */ ptr_in_p_128kernel = vaddw(ptr_in_p_128kernel, inc); /* kill write on first iteration */ if(!p3) memd(ptr_out++#8) = ldata1; ldata1 = valignb(dataF8, data70, align); data70 = memd(ptr_in++#8); } { memd(ptr_out++#8) = ldata0; ldata0 = valignb(data70, dataF8, align); dataF8 = memd(ptr_in++#8); } { memd(ptr_out++#8) = ldata1; ldata1 = valignb(dataF8, data70, align); data70 = memd(ptr_in++#8); } { memd(ptr_out++#8) = ldata0; ldata0 = valignb(data70, dataF8, align); dataF8 = memd(ptr_in++#8); kernel1 = cmp.gtu(kernel, #1); }:endloop0 { memd(ptr_out++#8) = ldata1; jump .Lepilog; } .Lodd_alignment: { loop0(.Loword_loop_00to24, kernel); kernel1 = cmp.gtu(kernel, #1); rest = add(kernel, #-1); } .falign .Loword_loop_00to24: { dcfetch(ptr_in_p_128); /* prefetch 4 lines ahead */ ptr_in_p_128kernel = vaddw(ptr_in_p_128kernel, inc); if(kernel1) ptr_out_p_32 = add(ptr_out_p_32, #32); } { dczeroa(ptr_out_p_32); /* reserve the next 32bytes in cache */ } { memd(ptr_out++#8) = ldata0; ldata0 = valignb(dataF8, data70, align); data70 = memd(ptr_in++#8); } { memd(ptr_out++#8) = ldata0; ldata0 = valignb(data70, dataF8, align); dataF8 = memd(ptr_in++#8); } { memd(ptr_out++#8) = ldata0; ldata0 = valignb(dataF8, data70, align); data70 = memd(ptr_in++#8); } { memd(ptr_out++#8) = ldata0; ldata0 = valignb(data70, dataF8, align); dataF8 = memd(ptr_in++#8); kernel1 = cmp.gtu(kernel, #1); }:endloop0 .Lepilog: { noepilog = cmp.eq(epilog,#0); epilogdws = lsr(epilog, #3); kernel = and(epilog, #7); } { if(noepilog) jumpr r31; if(noepilog) ptr_out = sub(ptr_out, len); p3 = cmp.eq(epilogdws, #0); shift2 = asl(epilog, #3); } { shiftb = and(shift2, #32); ifword = tstbit(epilog,#2); if(p3) jump .Lepilog60; if(!p3) epilog = add(epilog, #-16); } { loop0(.Ldword_loop_epilog, epilogdws); /* stop criteria is lsbs unless = 0 then its 8 */ p3 = cmp.eq(kernel, #0); if(p3.new) kernel= #8; p1 = cmp.gt(over, #0); } /* if not aligned to end of buffer execute 1 more iteration */ if(p1) kernel= #0; .Ldword_loop_epilog: { memd(ptr_out++#8) = ldata0; ldata0 = valignb(dataF8, data70, align); p3 = cmp.gt(epilog, kernel); } { data70 = dataF8; if(p3) dataF8 = memd(ptr_in++#8); epilog = add(epilog, #-8); }:endloop0 /* copy last 7 bytes */ .Lepilog60: { if(ifword) memw(ptr_out++#4) = data0; ldata0 = lsr(ldata0, shiftb); ifhword = tstbit(epilog,#1); shiftb = and(shift2, #16); } { if(ifhword) memh(ptr_out++#2) = data0; ldata0 = lsr(ldata0, shiftb); ifbyte = tstbit(epilog,#0); if(ifbyte.new) len = add(len, #-1); } { if(ifbyte) memb(ptr_out) = data0; ptr_out = sub(ptr_out, len); /* return dest pointer */ jumpr r31; } /* do byte copy for small n */ .Lbytes23orless: { p3 = sp1loop0(.Lbyte_copy, len); len = add(len, #-1); } .Lbyte_copy: { data0 = memb(ptr_in++#1); if(p3) memb(ptr_out++#1) = data0; }:endloop0 { memb(ptr_out) = data0; ptr_out = sub(ptr_out, len); jumpr r31; } /* do dword copies for aligned in, out and length */ .Ldwordaligned: { p3 = sp1loop0(.Ldword_copy, len8); } .Ldword_copy: { if(p3) memd(ptr_out++#8) = ldata0; ldata0 = memd(ptr_in++#8); }:endloop0 { memd(ptr_out) = ldata0; ptr_out = sub(ptr_out, len); jumpr r31; /* return to function caller */ } .Lmemcpy_return: r21:20 = memd(sp+#16); /* restore r20+r21 */ { r25:24 = memd(sp+#8); /* restore r24+r25 */ r17:16 = memd(sp+#0); /* restore r16+r17 */ } deallocframe; /* restore r31 and incrment stack by 16 */ jumpr r31 |