Subversion Repositories HelenOS-historic

Rev

Rev 532 | Go to most recent revision | Blame | Compare with Previous | Last modification | View Log | Download | RSS feed

  1. /*
  2.  * Copyright (C) 2005 Sergey Bondari
  3.  * All rights reserved.
  4.  *
  5.  * Redistribution and use in source and binary forms, with or without
  6.  * modification, are permitted provided that the following conditions
  7.  * are met:
  8.  *
  9.  * - Redistributions of source code must retain the above copyright
  10.  *   notice, this list of conditions and the following disclaimer.
  11.  * - Redistributions in binary form must reproduce the above copyright
  12.  *   notice, this list of conditions and the following disclaimer in the
  13.  *   documentation and/or other materials provided with the distribution.
  14.  * - The name of the author may not 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 AUTHOR ``AS IS'' AND ANY EXPRESS OR
  18.  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
  19.  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
  20.  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
  21.  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  22.  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  23.  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  24.  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  25.  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
  26.  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  27.  */
  28.  
  29. #ifndef __amd64_MEMSTR_H__
  30. #define __amd64_MEMSTR_H__
  31.  
  32. /** Copy memory
  33.  *
  34.  * Copy a given number of bytes (3rd argument)
  35.  * from the memory location defined by 2nd argument
  36.  * to the memory location defined by 1st argument.
  37.  * The memory areas cannot overlap.
  38.  *
  39.  * @param dst Destination
  40.  * @param src Source
  41.  * @param cnt Number of bytes
  42.  * @return Destination
  43.  */
  44. static inline void * memcpy(void * dst, const void * src, size_t cnt)
  45. {
  46.         __native d0, d1, d2;
  47.  
  48.         __asm__ __volatile__(
  49.                 "rep movsq\n\t"
  50.                 "movq %4, %%rcx\n\t"
  51.                 "andq $7, %%rcx\n\t"
  52.                 "jz 1f\n\t"
  53.                 "rep movsb\n\t"
  54.                 "1:\n"
  55.                 : "=&c" (d0), "=&D" (d1), "=&S" (d2)
  56.                 : "0" ((__native)(cnt / 8)), "g" ((__native)cnt), "1" ((__native) dst), "2" ((__native) src)
  57.                 : "memory");
  58.  
  59.         return dst;
  60. }
  61.  
  62.  
  63. /** Compare memory regions for equality
  64.  *
  65.  * Compare a given number of bytes (3rd argument)
  66.  * at memory locations defined by 1st and 2nd argument
  67.  * for equality. If bytes are equal function returns 0.
  68.  *
  69.  * @param src Region 1
  70.  * @param dst Region 2
  71.  * @param cnt Number of bytes
  72.  * @return Zero if bytes are equal, non-zero otherwise
  73.  */
  74. static inline int memcmp(const void * src, const void * dst, size_t cnt)
  75. {
  76.     __native d0, d1, d2;
  77.     __native ret;
  78.    
  79.     __asm__ (
  80.         "repe cmpsb\n\t"
  81.         "je 1f\n\t"
  82.         "movq %3, %0\n\t"
  83.         "addq $1, %0\n\t"
  84.         "1:\n"
  85.         : "=a" (ret), "=%S" (d0), "=&D" (d1), "=&c" (d2)
  86.         : "0" (0), "1" (src), "2" (dst), "3" ((__native)cnt)
  87.     );
  88.    
  89.     return ret;
  90. }
  91.  
  92. /** Fill memory with words
  93.  * Fill a given number of words (2nd argument)
  94.  * at memory defined by 1st argument with the
  95.  * word value defined by 3rd argument.
  96.  *
  97.  * @param dst Destination
  98.  * @param cnt Number of words
  99.  * @param x Value to fill
  100.  */
  101. static inline void memsetw(__address dst, size_t cnt, __u16 x)
  102. {
  103.     __native d0, d1;
  104.    
  105.     __asm__ __volatile__ (
  106.         "rep stosw\n\t"
  107.         : "=&D" (d0), "=&c" (d1), "=a" (x)
  108.         : "0" (dst), "1" ((__native)cnt), "2" (x)
  109.         : "memory"
  110.     );
  111.  
  112. }
  113.  
  114. /** Fill memory with bytes
  115.  * Fill a given number of bytes (2nd argument)
  116.  * at memory defined by 1st argument with the
  117.  * word value defined by 3rd argument.
  118.  *
  119.  * @param dst Destination
  120.  * @param cnt Number of bytes
  121.  * @param x Value to fill
  122.  */
  123. static inline void memsetb(__address dst, size_t cnt, __u8 x)
  124. {
  125.     __native d0, d1;
  126.    
  127.     __asm__ __volatile__ (
  128.         "rep stosb\n\t"
  129.         : "=&D" (d0), "=&c" (d1), "=a" (x)
  130.         : "0" (dst), "1" ((__native)cnt), "2" (x)
  131.         : "memory"
  132.     );
  133.  
  134. }
  135.  
  136. #endif
  137.