“2039070e9cb4dadf12332d16f74cf349fd980d73”上不存在“paddle/legacy/utils/PythonUtil.h”
memset.c 7.3 KB
Newer Older
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
/* A memset for CRIS.
   Copyright (C) 1999-2005 Axis Communications.
   All rights reserved.

   Redistribution and use in source and binary forms, with or without
   modification, are permitted provided that the following conditions
   are met:

   1. Redistributions of source code must retain the above copyright
      notice, this list of conditions and the following disclaimer.

   2. Neither the name of Axis Communications nor the names of its
      contributors may be used to endorse or promote products derived
      from this software without specific prior written permission.

   THIS SOFTWARE IS PROVIDED BY AXIS COMMUNICATIONS AND ITS CONTRIBUTORS
   ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
   LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
   A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL AXIS
   COMMUNICATIONS OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
   INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
   (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
   SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
   HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
   STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
   IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
   POSSIBILITY OF SUCH DAMAGE.  */

/* FIXME: This file should really only be used for reference, as the
   result is somewhat depending on gcc generating what we expect rather
   than what we describe.  An assembly file should be used instead.  */

/* Note the multiple occurrence of the expression "12*4", including the
   asm.  It is hard to get it into the asm in a good way.  Thus better to
   expose the problem everywhere: no macro.  */

/* Assuming one cycle per dword written or read (ok, not really true; the
   world is not ideal), and one cycle per instruction, then 43+3*(n/48-1)
   <= 24+24*(n/48-1) so n >= 45.7; n >= 0.9; we win on the first full
   48-byte block to set.  */

#define MEMSET_BY_BLOCK_THRESHOLD (1 * 48)

/* No name ambiguities in this file.  */
__asm__ (".syntax no_register_prefix");

void *memset(void *pdst, int c, unsigned int plen)
48
{
49 50
  /* Now we want the parameters in special registers.  Make sure the
     compiler does something usable with this.  */
51 52 53 54 55

  register char *return_dst __asm__ ("r10") = pdst;
  register int n __asm__ ("r12") = plen;
  register int lc __asm__ ("r11") = c;

56 57
  /* Most apps use memset sanely.  Memsetting about 3..4 bytes or less get
     penalized here compared to the generic implementation.  */
58

59 60 61 62 63 64 65 66 67 68 69
  /* This is fragile performancewise at best.  Check with newer GCC
     releases, if they compile cascaded "x |= x << 8" to sane code.  */
  __asm__("movu.b %0,r13						\n\
	   lslq 8,r13							\n\
	   move.b %0,r13						\n\
	   move.d r13,%0						\n\
	   lslq 16,r13							\n\
	   or.d r13,%0"
          : "=r" (lc)		/* Inputs.  */
	  : "0" (lc)		/* Outputs.  */
	  : "r13");		/* Trash.  */
70 71 72 73

  {
    register char *dst __asm__ ("r13") = pdst;

74 75 76 77 78 79 80 81 82 83
    if (((unsigned long) pdst & 3) != 0
	/* Oops! n = 0 must be a valid call, regardless of alignment.  */
	&& n >= 3)
      {
	if ((unsigned long) dst & 1)
	  {
	    *dst = (char) lc;
	    n--;
	    dst++;
	  }
84

85 86 87 88 89 90 91
	if ((unsigned long) dst & 2)
	  {
	    *(short *) dst = lc;
	    n -= 2;
	    dst += 2;
	  }
      }
92

93 94 95 96 97 98 99 100 101 102 103 104 105 106
    /* Decide which setting method to use.  */
    if (n >= MEMSET_BY_BLOCK_THRESHOLD)
      {
	/* It is not optimal to tell the compiler about clobbering any
	   registers; that will move the saving/restoring of those registers
	   to the function prologue/epilogue, and make non-block sizes
	   suboptimal.  */
	__asm__ volatile
	  ("\
	   ;; GCC does promise correct register allocations, but let's	\n\
	   ;; make sure it keeps its promises.				\n\
	   .ifnc %0-%1-%4,$r13-$r12-$r11				\n\
	   .error \"GCC reg alloc bug: %0-%1-%4 != $r13-$r12-$r11\"	\n\
	   .endif							\n\
107
									\n\
108 109 110 111 112
	   ;; Save the registers we'll clobber in the movem process	\n\
	   ;; on the stack.  Don't mention them to gcc, it will only be	\n\
	   ;; upset.							\n\
	   subq	   11*4,sp						\n\
	   movem   r10,[sp]						\n\
113
									\n\
114 115 116 117 118 119 120 121 122 123 124
	   move.d  r11,r0						\n\
	   move.d  r11,r1						\n\
	   move.d  r11,r2						\n\
	   move.d  r11,r3						\n\
	   move.d  r11,r4						\n\
	   move.d  r11,r5						\n\
	   move.d  r11,r6						\n\
	   move.d  r11,r7						\n\
	   move.d  r11,r8						\n\
	   move.d  r11,r9						\n\
	   move.d  r11,r10						\n\
125
									\n\
126 127 128
	   ;; Now we've got this:					\n\
	   ;; r13 - dst							\n\
	   ;; r12 - n							\n\
129
									\n\
130 131
	   ;; Update n for the first loop				\n\
	   subq	   12*4,r12						\n\
132
0:									\n\
133 134 135 136 137 138 139 140 141
"
#ifdef __arch_common_v10_v32
	   /* Cater to branch offset difference between v32 and v10.  We
	      assume the branch below has an 8-bit offset.  */
"	   setf\n"
#endif
"	   subq	  12*4,r12						\n\
	   bge	   0b							\n\
	   movem	r11,[r13+]					\n\
142
									\n\
143 144
	   ;; Compensate for last loop underflowing n.			\n\
	   addq	  12*4,r12						\n\
145
									\n\
146 147
	   ;; Restore registers from stack.				\n\
	   movem [sp+],r10"
148

149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164
	   /* Outputs.	*/
	   : "=r" (dst), "=r" (n)

	   /* Inputs.  */
	   : "0" (dst), "1" (n), "r" (lc));
      }

    /* An ad-hoc unroll, used for 4*12-1..16 bytes. */
    while (n >= 16)
      {
	*(long *) dst = lc; dst += 4;
	*(long *) dst = lc; dst += 4;
	*(long *) dst = lc; dst += 4;
	*(long *) dst = lc; dst += 4;
	n -= 16;
      }
165 166

    switch (n)
167
      {
168 169
      case 0:
        break;
170

171
      case 1:
172
        *dst = (char) lc;
173
        break;
174

175
      case 2:
176
        *(short *) dst = (short) lc;
177
        break;
178

179
      case 3:
180 181
        *(short *) dst = (short) lc; dst += 2;
        *dst = (char) lc;
182
        break;
183

184
      case 4:
185
        *(long *) dst = lc;
186
        break;
187

188
      case 5:
189 190
        *(long *) dst = lc; dst += 4;
        *dst = (char) lc;
191
        break;
192

193
      case 6:
194 195
        *(long *) dst = lc; dst += 4;
        *(short *) dst = (short) lc;
196
        break;
197

198
      case 7:
199 200 201
        *(long *) dst = lc; dst += 4;
        *(short *) dst = (short) lc; dst += 2;
        *dst = (char) lc;
202
        break;
203

204
      case 8:
205 206
        *(long *) dst = lc; dst += 4;
        *(long *) dst = lc;
207
        break;
208

209
      case 9:
210 211 212
        *(long *) dst = lc; dst += 4;
        *(long *) dst = lc; dst += 4;
        *dst = (char) lc;
213
        break;
214

215
      case 10:
216 217 218
        *(long *) dst = lc; dst += 4;
        *(long *) dst = lc; dst += 4;
        *(short *) dst = (short) lc;
219
        break;
220

221
      case 11:
222 223 224 225
        *(long *) dst = lc; dst += 4;
        *(long *) dst = lc; dst += 4;
        *(short *) dst = (short) lc; dst += 2;
        *dst = (char) lc;
226
        break;
227

228
      case 12:
229 230 231
        *(long *) dst = lc; dst += 4;
        *(long *) dst = lc; dst += 4;
        *(long *) dst = lc;
232
        break;
233

234
      case 13:
235 236 237 238
        *(long *) dst = lc; dst += 4;
        *(long *) dst = lc; dst += 4;
        *(long *) dst = lc; dst += 4;
        *dst = (char) lc;
239
        break;
240

241
      case 14:
242 243 244 245
        *(long *) dst = lc; dst += 4;
        *(long *) dst = lc; dst += 4;
        *(long *) dst = lc; dst += 4;
        *(short *) dst = (short) lc;
246
        break;
247

248
      case 15:
249 250 251 252 253
        *(long *) dst = lc; dst += 4;
        *(long *) dst = lc; dst += 4;
        *(long *) dst = lc; dst += 4;
        *(short *) dst = (short) lc; dst += 2;
        *dst = (char) lc;
254
        break;
255
      }
256 257
  }

258 259
  return return_dst;
}