/* A memset for CRIS.1Copyright (C) 1999-2005 Axis Communications.2All rights reserved.34Redistribution and use in source and binary forms, with or without5modification, are permitted provided that the following conditions6are met:781. Redistributions of source code must retain the above copyright9notice, this list of conditions and the following disclaimer.10112. Neither the name of Axis Communications nor the names of its12contributors may be used to endorse or promote products derived13from this software without specific prior written permission.1415THIS SOFTWARE IS PROVIDED BY AXIS COMMUNICATIONS AND ITS CONTRIBUTORS16``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT17LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR18A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL AXIS19COMMUNICATIONS OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT,20INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES21(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR22SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)23HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,24STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING25IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE26POSSIBILITY OF SUCH DAMAGE. */2728/* FIXME: This file should really only be used for reference, as the29result is somewhat depending on gcc generating what we expect rather30than what we describe. An assembly file should be used instead. */3132/* Note the multiple occurrence of the expression "12*4", including the33asm. It is hard to get it into the asm in a good way. Thus better to34expose the problem everywhere: no macro. */3536/* Assuming one cycle per dword written or read (ok, not really true; the37world is not ideal), and one cycle per instruction, then 43+3*(n/48-1)38<= 24+24*(n/48-1) so n >= 45.7; n >= 0.9; we win on the first full3948-byte block to set. */4041#define MEMSET_BY_BLOCK_THRESHOLD (1 * 48)4243/* No name ambiguities in this file. */44__asm__ (".syntax no_register_prefix");4546void *memset(void *pdst, int c, unsigned int plen)47{48/* Now we want the parameters in special registers. Make sure the49compiler does something usable with this. */5051register char *return_dst __asm__ ("r10") = pdst;52register int n __asm__ ("r12") = plen;53register int lc __asm__ ("r11") = c;5455/* Most apps use memset sanely. Memsetting about 3..4 bytes or less get56penalized here compared to the generic implementation. */5758/* This is fragile performancewise at best. Check with newer GCC59releases, if they compile cascaded "x |= x << 8" to sane code. */60__asm__("movu.b %0,r13 \n\61lslq 8,r13 \n\62move.b %0,r13 \n\63move.d r13,%0 \n\64lslq 16,r13 \n\65or.d r13,%0"66: "=r" (lc) /* Inputs. */67: "0" (lc) /* Outputs. */68: "r13"); /* Trash. */6970{71register char *dst __asm__ ("r13") = pdst;7273if (((unsigned long) pdst & 3) != 074/* Oops! n = 0 must be a valid call, regardless of alignment. */75&& n >= 3)76{77if ((unsigned long) dst & 1)78{79*dst = (char) lc;80n--;81dst++;82}8384if ((unsigned long) dst & 2)85{86*(short *) dst = lc;87n -= 2;88dst += 2;89}90}9192/* Decide which setting method to use. */93if (n >= MEMSET_BY_BLOCK_THRESHOLD)94{95/* It is not optimal to tell the compiler about clobbering any96registers; that will move the saving/restoring of those registers97to the function prologue/epilogue, and make non-block sizes98suboptimal. */99__asm__ volatile100("\101;; GCC does promise correct register allocations, but let's \n\102;; make sure it keeps its promises. \n\103.ifnc %0-%1-%4,$r13-$r12-$r11 \n\104.error \"GCC reg alloc bug: %0-%1-%4 != $r13-$r12-$r11\" \n\105.endif \n\106\n\107;; Save the registers we'll clobber in the movem process \n\108;; on the stack. Don't mention them to gcc, it will only be \n\109;; upset. \n\110subq 11*4,sp \n\111movem r10,[sp] \n\112\n\113move.d r11,r0 \n\114move.d r11,r1 \n\115move.d r11,r2 \n\116move.d r11,r3 \n\117move.d r11,r4 \n\118move.d r11,r5 \n\119move.d r11,r6 \n\120move.d r11,r7 \n\121move.d r11,r8 \n\122move.d r11,r9 \n\123move.d r11,r10 \n\124\n\125;; Now we've got this: \n\126;; r13 - dst \n\127;; r12 - n \n\128\n\129;; Update n for the first loop \n\130subq 12*4,r12 \n\1310: \n\132"133#ifdef __arch_common_v10_v32134/* Cater to branch offset difference between v32 and v10. We135assume the branch below has an 8-bit offset. */136" setf\n"137#endif138" subq 12*4,r12 \n\139bge 0b \n\140movem r11,[r13+] \n\141\n\142;; Compensate for last loop underflowing n. \n\143addq 12*4,r12 \n\144\n\145;; Restore registers from stack. \n\146movem [sp+],r10"147148/* Outputs. */149: "=r" (dst), "=r" (n)150151/* Inputs. */152: "0" (dst), "1" (n), "r" (lc));153}154155/* An ad-hoc unroll, used for 4*12-1..16 bytes. */156while (n >= 16)157{158*(long *) dst = lc; dst += 4;159*(long *) dst = lc; dst += 4;160*(long *) dst = lc; dst += 4;161*(long *) dst = lc; dst += 4;162n -= 16;163}164165switch (n)166{167case 0:168break;169170case 1:171*dst = (char) lc;172break;173174case 2:175*(short *) dst = (short) lc;176break;177178case 3:179*(short *) dst = (short) lc; dst += 2;180*dst = (char) lc;181break;182183case 4:184*(long *) dst = lc;185break;186187case 5:188*(long *) dst = lc; dst += 4;189*dst = (char) lc;190break;191192case 6:193*(long *) dst = lc; dst += 4;194*(short *) dst = (short) lc;195break;196197case 7:198*(long *) dst = lc; dst += 4;199*(short *) dst = (short) lc; dst += 2;200*dst = (char) lc;201break;202203case 8:204*(long *) dst = lc; dst += 4;205*(long *) dst = lc;206break;207208case 9:209*(long *) dst = lc; dst += 4;210*(long *) dst = lc; dst += 4;211*dst = (char) lc;212break;213214case 10:215*(long *) dst = lc; dst += 4;216*(long *) dst = lc; dst += 4;217*(short *) dst = (short) lc;218break;219220case 11:221*(long *) dst = lc; dst += 4;222*(long *) dst = lc; dst += 4;223*(short *) dst = (short) lc; dst += 2;224*dst = (char) lc;225break;226227case 12:228*(long *) dst = lc; dst += 4;229*(long *) dst = lc; dst += 4;230*(long *) dst = lc;231break;232233case 13:234*(long *) dst = lc; dst += 4;235*(long *) dst = lc; dst += 4;236*(long *) dst = lc; dst += 4;237*dst = (char) lc;238break;239240case 14:241*(long *) dst = lc; dst += 4;242*(long *) dst = lc; dst += 4;243*(long *) dst = lc; dst += 4;244*(short *) dst = (short) lc;245break;246247case 15:248*(long *) dst = lc; dst += 4;249*(long *) dst = lc; dst += 4;250*(long *) dst = lc; dst += 4;251*(short *) dst = (short) lc; dst += 2;252*dst = (char) lc;253break;254}255}256257return return_dst;258}259260261