xref: /freebsd/sys/contrib/ncsw/etc/memcpy.c (revision c2c014f24c10f90d85126ac5fbd4d8524de32b1c)
1852ba100SJustin Hibbits /*
2852ba100SJustin Hibbits  * Copyright 2008-2012 Freescale Semiconductor Inc.
30aeed3e9SJustin Hibbits  *
40aeed3e9SJustin Hibbits  * Redistribution and use in source and binary forms, with or without
50aeed3e9SJustin Hibbits  * modification, are permitted provided that the following conditions are met:
60aeed3e9SJustin Hibbits  *     * Redistributions of source code must retain the above copyright
70aeed3e9SJustin Hibbits  *       notice, this list of conditions and the following disclaimer.
80aeed3e9SJustin Hibbits  *     * Redistributions in binary form must reproduce the above copyright
90aeed3e9SJustin Hibbits  *       notice, this list of conditions and the following disclaimer in the
100aeed3e9SJustin Hibbits  *       documentation and/or other materials provided with the distribution.
110aeed3e9SJustin Hibbits  *     * Neither the name of Freescale Semiconductor nor the
120aeed3e9SJustin Hibbits  *       names of its contributors may be used to endorse or promote products
130aeed3e9SJustin Hibbits  *       derived from this software without specific prior written permission.
140aeed3e9SJustin Hibbits  *
150aeed3e9SJustin Hibbits  *
160aeed3e9SJustin Hibbits  * ALTERNATIVELY, this software may be distributed under the terms of the
170aeed3e9SJustin Hibbits  * GNU General Public License ("GPL") as published by the Free Software
180aeed3e9SJustin Hibbits  * Foundation, either version 2 of that License or (at your option) any
190aeed3e9SJustin Hibbits  * later version.
200aeed3e9SJustin Hibbits  *
210aeed3e9SJustin Hibbits  * THIS SOFTWARE IS PROVIDED BY Freescale Semiconductor ``AS IS'' AND ANY
220aeed3e9SJustin Hibbits  * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
230aeed3e9SJustin Hibbits  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
240aeed3e9SJustin Hibbits  * DISCLAIMED. IN NO EVENT SHALL Freescale Semiconductor BE LIABLE FOR ANY
250aeed3e9SJustin Hibbits  * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
260aeed3e9SJustin Hibbits  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
270aeed3e9SJustin Hibbits  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
280aeed3e9SJustin Hibbits  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
290aeed3e9SJustin Hibbits  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
300aeed3e9SJustin Hibbits  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
310aeed3e9SJustin Hibbits  */
320aeed3e9SJustin Hibbits 
330aeed3e9SJustin Hibbits 
34852ba100SJustin Hibbits 
350aeed3e9SJustin Hibbits #include "std_ext.h"
360aeed3e9SJustin Hibbits #include "xx_ext.h"
370aeed3e9SJustin Hibbits #include "memcpy_ext.h"
380aeed3e9SJustin Hibbits 
MemCpy8(void * pDst,void * pSrc,uint32_t size)39852ba100SJustin Hibbits void * MemCpy8(void* pDst, void* pSrc, uint32_t size)
40852ba100SJustin Hibbits {
41*a32b5435SJustin Hibbits     uint32_t i;
420aeed3e9SJustin Hibbits 
43852ba100SJustin Hibbits     for(i = 0; i < size; ++i)
44852ba100SJustin Hibbits         *(((uint8_t*)(pDst)) + i) = *(((uint8_t*)(pSrc)) + i);
45852ba100SJustin Hibbits 
46852ba100SJustin Hibbits     return pDst;
470aeed3e9SJustin Hibbits }
480aeed3e9SJustin Hibbits 
MemSet8(void * pDst,int c,uint32_t size)49852ba100SJustin Hibbits void * MemSet8(void* pDst, int c, uint32_t size)
500aeed3e9SJustin Hibbits {
51*a32b5435SJustin Hibbits     uint32_t i;
52852ba100SJustin Hibbits 
53852ba100SJustin Hibbits     for(i = 0; i < size; ++i)
54852ba100SJustin Hibbits         *(((uint8_t*)(pDst)) + i) = (uint8_t)(c);
55852ba100SJustin Hibbits 
56852ba100SJustin Hibbits     return pDst;
570aeed3e9SJustin Hibbits }
580aeed3e9SJustin Hibbits 
MemCpy32(void * pDst,void * pSrc,uint32_t size)590aeed3e9SJustin Hibbits void * MemCpy32(void* pDst,void* pSrc, uint32_t size)
600aeed3e9SJustin Hibbits {
610aeed3e9SJustin Hibbits     uint32_t leftAlign;
620aeed3e9SJustin Hibbits     uint32_t rightAlign;
630aeed3e9SJustin Hibbits     uint32_t lastWord;
640aeed3e9SJustin Hibbits     uint32_t currWord;
650aeed3e9SJustin Hibbits     uint32_t *p_Src32;
660aeed3e9SJustin Hibbits     uint32_t *p_Dst32;
670aeed3e9SJustin Hibbits     uint8_t  *p_Src8;
680aeed3e9SJustin Hibbits     uint8_t  *p_Dst8;
690aeed3e9SJustin Hibbits 
700aeed3e9SJustin Hibbits     p_Src8 = (uint8_t*)(pSrc);
710aeed3e9SJustin Hibbits     p_Dst8 = (uint8_t*)(pDst);
720aeed3e9SJustin Hibbits     /* first copy byte by byte till the source first alignment
730aeed3e9SJustin Hibbits      * this step is necessary to ensure we do not even try to access
740aeed3e9SJustin Hibbits      * data which is before the source buffer, hence it is not ours.
750aeed3e9SJustin Hibbits      */
760aeed3e9SJustin Hibbits     while((PTR_TO_UINT(p_Src8) & 3) && size) /* (pSrc mod 4) > 0 and size > 0 */
770aeed3e9SJustin Hibbits     {
780aeed3e9SJustin Hibbits         *p_Dst8++ = *p_Src8++;
790aeed3e9SJustin Hibbits         size--;
800aeed3e9SJustin Hibbits     }
810aeed3e9SJustin Hibbits 
820aeed3e9SJustin Hibbits     /* align destination (possibly disaligning source)*/
830aeed3e9SJustin Hibbits     while((PTR_TO_UINT(p_Dst8) & 3) && size) /* (pDst mod 4) > 0 and size > 0 */
840aeed3e9SJustin Hibbits     {
850aeed3e9SJustin Hibbits         *p_Dst8++ = *p_Src8++;
860aeed3e9SJustin Hibbits         size--;
870aeed3e9SJustin Hibbits     }
880aeed3e9SJustin Hibbits 
890aeed3e9SJustin Hibbits     /* dest is aligned and source is not necessarily aligned */
900aeed3e9SJustin Hibbits     leftAlign = (uint32_t)((PTR_TO_UINT(p_Src8) & 3) << 3); /* leftAlign = (pSrc mod 4)*8 */
910aeed3e9SJustin Hibbits     rightAlign = 32 - leftAlign;
920aeed3e9SJustin Hibbits 
930aeed3e9SJustin Hibbits 
940aeed3e9SJustin Hibbits     if (leftAlign == 0)
950aeed3e9SJustin Hibbits     {
960aeed3e9SJustin Hibbits         /* source is also aligned */
970aeed3e9SJustin Hibbits         p_Src32 = (uint32_t*)(p_Src8);
980aeed3e9SJustin Hibbits         p_Dst32 = (uint32_t*)(p_Dst8);
990aeed3e9SJustin Hibbits         while (size >> 2) /* size >= 4 */
1000aeed3e9SJustin Hibbits         {
1010aeed3e9SJustin Hibbits             *p_Dst32++ = *p_Src32++;
1020aeed3e9SJustin Hibbits             size -= 4;
1030aeed3e9SJustin Hibbits         }
1040aeed3e9SJustin Hibbits         p_Src8 = (uint8_t*)(p_Src32);
1050aeed3e9SJustin Hibbits         p_Dst8 = (uint8_t*)(p_Dst32);
1060aeed3e9SJustin Hibbits     }
1070aeed3e9SJustin Hibbits     else
1080aeed3e9SJustin Hibbits     {
1090aeed3e9SJustin Hibbits         /* source is not aligned (destination is aligned)*/
1100aeed3e9SJustin Hibbits         p_Src32 = (uint32_t*)(p_Src8 - (leftAlign >> 3));
1110aeed3e9SJustin Hibbits         p_Dst32 = (uint32_t*)(p_Dst8);
1120aeed3e9SJustin Hibbits         lastWord = *p_Src32++;
1130aeed3e9SJustin Hibbits         while(size >> 3) /* size >= 8 */
1140aeed3e9SJustin Hibbits         {
1150aeed3e9SJustin Hibbits             currWord = *p_Src32;
1160aeed3e9SJustin Hibbits             *p_Dst32 = (lastWord << leftAlign) | (currWord >> rightAlign);
1170aeed3e9SJustin Hibbits             lastWord = currWord;
1180aeed3e9SJustin Hibbits             p_Src32++;
1190aeed3e9SJustin Hibbits             p_Dst32++;
1200aeed3e9SJustin Hibbits             size -= 4;
1210aeed3e9SJustin Hibbits         }
1220aeed3e9SJustin Hibbits         p_Dst8 = (uint8_t*)(p_Dst32);
1230aeed3e9SJustin Hibbits         p_Src8 = (uint8_t*)(p_Src32) - 4 + (leftAlign >> 3);
1240aeed3e9SJustin Hibbits     }
1250aeed3e9SJustin Hibbits 
1260aeed3e9SJustin Hibbits     /* complete the left overs */
1270aeed3e9SJustin Hibbits     while (size--)
1280aeed3e9SJustin Hibbits         *p_Dst8++ = *p_Src8++;
1290aeed3e9SJustin Hibbits 
1300aeed3e9SJustin Hibbits     return pDst;
1310aeed3e9SJustin Hibbits }
1320aeed3e9SJustin Hibbits 
IO2IOCpy32(void * pDst,void * pSrc,uint32_t size)1330aeed3e9SJustin Hibbits void * IO2IOCpy32(void* pDst,void* pSrc, uint32_t size)
1340aeed3e9SJustin Hibbits {
1350aeed3e9SJustin Hibbits     uint32_t leftAlign;
1360aeed3e9SJustin Hibbits     uint32_t rightAlign;
1370aeed3e9SJustin Hibbits     uint32_t lastWord;
1380aeed3e9SJustin Hibbits     uint32_t currWord;
1390aeed3e9SJustin Hibbits     uint32_t *p_Src32;
1400aeed3e9SJustin Hibbits     uint32_t *p_Dst32;
1410aeed3e9SJustin Hibbits     uint8_t  *p_Src8;
1420aeed3e9SJustin Hibbits     uint8_t  *p_Dst8;
1430aeed3e9SJustin Hibbits 
1440aeed3e9SJustin Hibbits     p_Src8 = (uint8_t*)(pSrc);
1450aeed3e9SJustin Hibbits     p_Dst8 = (uint8_t*)(pDst);
1460aeed3e9SJustin Hibbits     /* first copy byte by byte till the source first alignment
1470aeed3e9SJustin Hibbits      * this step is necessary to ensure we do not even try to access
1480aeed3e9SJustin Hibbits      * data which is before the source buffer, hence it is not ours.
1490aeed3e9SJustin Hibbits      */
1500aeed3e9SJustin Hibbits     while((PTR_TO_UINT(p_Src8) & 3) && size) /* (pSrc mod 4) > 0 and size > 0 */
1510aeed3e9SJustin Hibbits     {
152852ba100SJustin Hibbits         WRITE_UINT8(*p_Dst8, GET_UINT8(*p_Src8));
1530aeed3e9SJustin Hibbits         p_Dst8++;p_Src8++;
1540aeed3e9SJustin Hibbits         size--;
1550aeed3e9SJustin Hibbits     }
1560aeed3e9SJustin Hibbits 
1570aeed3e9SJustin Hibbits     /* align destination (possibly disaligning source)*/
1580aeed3e9SJustin Hibbits     while((PTR_TO_UINT(p_Dst8) & 3) && size) /* (pDst mod 4) > 0 and size > 0 */
1590aeed3e9SJustin Hibbits     {
160852ba100SJustin Hibbits         WRITE_UINT8(*p_Dst8, GET_UINT8(*p_Src8));
1610aeed3e9SJustin Hibbits         p_Dst8++;p_Src8++;
1620aeed3e9SJustin Hibbits         size--;
1630aeed3e9SJustin Hibbits     }
1640aeed3e9SJustin Hibbits 
1650aeed3e9SJustin Hibbits     /* dest is aligned and source is not necessarily aligned */
1660aeed3e9SJustin Hibbits     leftAlign = (uint32_t)((PTR_TO_UINT(p_Src8) & 3) << 3); /* leftAlign = (pSrc mod 4)*8 */
1670aeed3e9SJustin Hibbits     rightAlign = 32 - leftAlign;
1680aeed3e9SJustin Hibbits 
1690aeed3e9SJustin Hibbits     if (leftAlign == 0)
1700aeed3e9SJustin Hibbits     {
1710aeed3e9SJustin Hibbits         /* source is also aligned */
1720aeed3e9SJustin Hibbits         p_Src32 = (uint32_t*)(p_Src8);
1730aeed3e9SJustin Hibbits         p_Dst32 = (uint32_t*)(p_Dst8);
1740aeed3e9SJustin Hibbits         while (size >> 2) /* size >= 4 */
1750aeed3e9SJustin Hibbits         {
1760aeed3e9SJustin Hibbits             WRITE_UINT32(*p_Dst32, GET_UINT32(*p_Src32));
1770aeed3e9SJustin Hibbits             p_Dst32++;p_Src32++;
1780aeed3e9SJustin Hibbits             size -= 4;
1790aeed3e9SJustin Hibbits         }
1800aeed3e9SJustin Hibbits         p_Src8 = (uint8_t*)(p_Src32);
1810aeed3e9SJustin Hibbits         p_Dst8 = (uint8_t*)(p_Dst32);
1820aeed3e9SJustin Hibbits     }
1830aeed3e9SJustin Hibbits     else
1840aeed3e9SJustin Hibbits     {
1850aeed3e9SJustin Hibbits         /* source is not aligned (destination is aligned)*/
1860aeed3e9SJustin Hibbits         p_Src32 = (uint32_t*)(p_Src8 - (leftAlign >> 3));
1870aeed3e9SJustin Hibbits         p_Dst32 = (uint32_t*)(p_Dst8);
1880aeed3e9SJustin Hibbits         lastWord = GET_UINT32(*p_Src32);
1890aeed3e9SJustin Hibbits         p_Src32++;
1900aeed3e9SJustin Hibbits         while(size >> 3) /* size >= 8 */
1910aeed3e9SJustin Hibbits         {
1920aeed3e9SJustin Hibbits             currWord = GET_UINT32(*p_Src32);
1930aeed3e9SJustin Hibbits             WRITE_UINT32(*p_Dst32, (lastWord << leftAlign) | (currWord >> rightAlign));
1940aeed3e9SJustin Hibbits             lastWord = currWord;
1950aeed3e9SJustin Hibbits             p_Src32++;p_Dst32++;
1960aeed3e9SJustin Hibbits             size -= 4;
1970aeed3e9SJustin Hibbits         }
1980aeed3e9SJustin Hibbits         p_Dst8 = (uint8_t*)(p_Dst32);
1990aeed3e9SJustin Hibbits         p_Src8 = (uint8_t*)(p_Src32) - 4 + (leftAlign >> 3);
2000aeed3e9SJustin Hibbits     }
2010aeed3e9SJustin Hibbits 
2020aeed3e9SJustin Hibbits     /* complete the left overs */
2030aeed3e9SJustin Hibbits     while (size--)
2040aeed3e9SJustin Hibbits     {
205852ba100SJustin Hibbits         WRITE_UINT8(*p_Dst8, GET_UINT8(*p_Src8));
2060aeed3e9SJustin Hibbits         p_Dst8++;p_Src8++;
2070aeed3e9SJustin Hibbits     }
2080aeed3e9SJustin Hibbits 
2090aeed3e9SJustin Hibbits     return pDst;
2100aeed3e9SJustin Hibbits }
2110aeed3e9SJustin Hibbits 
Mem2IOCpy32(void * pDst,void * pSrc,uint32_t size)2120aeed3e9SJustin Hibbits void * Mem2IOCpy32(void* pDst,void* pSrc, uint32_t size)
2130aeed3e9SJustin Hibbits {
2140aeed3e9SJustin Hibbits     uint32_t leftAlign;
2150aeed3e9SJustin Hibbits     uint32_t rightAlign;
2160aeed3e9SJustin Hibbits     uint32_t lastWord;
2170aeed3e9SJustin Hibbits     uint32_t currWord;
2180aeed3e9SJustin Hibbits     uint32_t *p_Src32;
2190aeed3e9SJustin Hibbits     uint32_t *p_Dst32;
2200aeed3e9SJustin Hibbits     uint8_t  *p_Src8;
2210aeed3e9SJustin Hibbits     uint8_t  *p_Dst8;
2220aeed3e9SJustin Hibbits 
2230aeed3e9SJustin Hibbits     p_Src8 = (uint8_t*)(pSrc);
2240aeed3e9SJustin Hibbits     p_Dst8 = (uint8_t*)(pDst);
2250aeed3e9SJustin Hibbits     /* first copy byte by byte till the source first alignment
2260aeed3e9SJustin Hibbits      * this step is necessary to ensure we do not even try to access
2270aeed3e9SJustin Hibbits      * data which is before the source buffer, hence it is not ours.
2280aeed3e9SJustin Hibbits      */
2290aeed3e9SJustin Hibbits     while((PTR_TO_UINT(p_Src8) & 3) && size) /* (pSrc mod 4) > 0 and size > 0 */
2300aeed3e9SJustin Hibbits     {
231852ba100SJustin Hibbits         WRITE_UINT8(*p_Dst8, *p_Src8);
2320aeed3e9SJustin Hibbits         p_Dst8++;p_Src8++;
2330aeed3e9SJustin Hibbits         size--;
2340aeed3e9SJustin Hibbits     }
2350aeed3e9SJustin Hibbits 
2360aeed3e9SJustin Hibbits     /* align destination (possibly disaligning source)*/
2370aeed3e9SJustin Hibbits     while((PTR_TO_UINT(p_Dst8) & 3) && size) /* (pDst mod 4) > 0 and size > 0 */
2380aeed3e9SJustin Hibbits     {
239852ba100SJustin Hibbits         WRITE_UINT8(*p_Dst8, *p_Src8);
2400aeed3e9SJustin Hibbits         p_Dst8++;p_Src8++;
2410aeed3e9SJustin Hibbits         size--;
2420aeed3e9SJustin Hibbits     }
2430aeed3e9SJustin Hibbits 
2440aeed3e9SJustin Hibbits     /* dest is aligned and source is not necessarily aligned */
2450aeed3e9SJustin Hibbits     leftAlign = (uint32_t)((PTR_TO_UINT(p_Src8) & 3) << 3); /* leftAlign = (pSrc mod 4)*8 */
2460aeed3e9SJustin Hibbits     rightAlign = 32 - leftAlign;
2470aeed3e9SJustin Hibbits 
2480aeed3e9SJustin Hibbits     if (leftAlign == 0)
2490aeed3e9SJustin Hibbits     {
2500aeed3e9SJustin Hibbits         /* source is also aligned */
2510aeed3e9SJustin Hibbits         p_Src32 = (uint32_t*)(p_Src8);
2520aeed3e9SJustin Hibbits         p_Dst32 = (uint32_t*)(p_Dst8);
2530aeed3e9SJustin Hibbits         while (size >> 2) /* size >= 4 */
2540aeed3e9SJustin Hibbits         {
2550aeed3e9SJustin Hibbits             WRITE_UINT32(*p_Dst32, *p_Src32);
2560aeed3e9SJustin Hibbits             p_Dst32++;p_Src32++;
2570aeed3e9SJustin Hibbits             size -= 4;
2580aeed3e9SJustin Hibbits         }
2590aeed3e9SJustin Hibbits         p_Src8 = (uint8_t*)(p_Src32);
2600aeed3e9SJustin Hibbits         p_Dst8 = (uint8_t*)(p_Dst32);
2610aeed3e9SJustin Hibbits     }
2620aeed3e9SJustin Hibbits     else
2630aeed3e9SJustin Hibbits     {
2640aeed3e9SJustin Hibbits         /* source is not aligned (destination is aligned)*/
2650aeed3e9SJustin Hibbits         p_Src32 = (uint32_t*)(p_Src8 - (leftAlign >> 3));
2660aeed3e9SJustin Hibbits         p_Dst32 = (uint32_t*)(p_Dst8);
2670aeed3e9SJustin Hibbits         lastWord = *p_Src32++;
2680aeed3e9SJustin Hibbits         while(size >> 3) /* size >= 8 */
2690aeed3e9SJustin Hibbits         {
2700aeed3e9SJustin Hibbits             currWord = *p_Src32;
2710aeed3e9SJustin Hibbits             WRITE_UINT32(*p_Dst32, (lastWord << leftAlign) | (currWord >> rightAlign));
2720aeed3e9SJustin Hibbits             lastWord = currWord;
2730aeed3e9SJustin Hibbits             p_Src32++;p_Dst32++;
2740aeed3e9SJustin Hibbits             size -= 4;
2750aeed3e9SJustin Hibbits         }
2760aeed3e9SJustin Hibbits         p_Dst8 = (uint8_t*)(p_Dst32);
2770aeed3e9SJustin Hibbits         p_Src8 = (uint8_t*)(p_Src32) - 4 + (leftAlign >> 3);
2780aeed3e9SJustin Hibbits     }
2790aeed3e9SJustin Hibbits 
2800aeed3e9SJustin Hibbits     /* complete the left overs */
2810aeed3e9SJustin Hibbits     while (size--)
2820aeed3e9SJustin Hibbits     {
283852ba100SJustin Hibbits         WRITE_UINT8(*p_Dst8, *p_Src8);
2840aeed3e9SJustin Hibbits         p_Dst8++;p_Src8++;
2850aeed3e9SJustin Hibbits     }
2860aeed3e9SJustin Hibbits 
2870aeed3e9SJustin Hibbits     return pDst;
2880aeed3e9SJustin Hibbits }
2890aeed3e9SJustin Hibbits 
IO2MemCpy32(void * pDst,void * pSrc,uint32_t size)2900aeed3e9SJustin Hibbits void * IO2MemCpy32(void* pDst,void* pSrc, uint32_t size)
2910aeed3e9SJustin Hibbits {
2920aeed3e9SJustin Hibbits     uint32_t leftAlign;
2930aeed3e9SJustin Hibbits     uint32_t rightAlign;
2940aeed3e9SJustin Hibbits     uint32_t lastWord;
2950aeed3e9SJustin Hibbits     uint32_t currWord;
2960aeed3e9SJustin Hibbits     uint32_t *p_Src32;
2970aeed3e9SJustin Hibbits     uint32_t *p_Dst32;
2980aeed3e9SJustin Hibbits     uint8_t  *p_Src8;
2990aeed3e9SJustin Hibbits     uint8_t  *p_Dst8;
3000aeed3e9SJustin Hibbits 
3010aeed3e9SJustin Hibbits     p_Src8 = (uint8_t*)(pSrc);
3020aeed3e9SJustin Hibbits     p_Dst8 = (uint8_t*)(pDst);
3030aeed3e9SJustin Hibbits     /* first copy byte by byte till the source first alignment
3040aeed3e9SJustin Hibbits      * this step is necessary to ensure we do not even try to access
3050aeed3e9SJustin Hibbits      * data which is before the source buffer, hence it is not ours.
3060aeed3e9SJustin Hibbits      */
3070aeed3e9SJustin Hibbits     while((PTR_TO_UINT(p_Src8) & 3) && size) /* (pSrc mod 4) > 0 and size > 0 */
3080aeed3e9SJustin Hibbits     {
309852ba100SJustin Hibbits         *p_Dst8 = GET_UINT8(*p_Src8);
3100aeed3e9SJustin Hibbits         p_Dst8++;p_Src8++;
3110aeed3e9SJustin Hibbits         size--;
3120aeed3e9SJustin Hibbits     }
3130aeed3e9SJustin Hibbits 
3140aeed3e9SJustin Hibbits     /* align destination (possibly disaligning source)*/
3150aeed3e9SJustin Hibbits     while((PTR_TO_UINT(p_Dst8) & 3) && size) /* (pDst mod 4) > 0 and size > 0 */
3160aeed3e9SJustin Hibbits     {
317852ba100SJustin Hibbits         *p_Dst8 = GET_UINT8(*p_Src8);
3180aeed3e9SJustin Hibbits         p_Dst8++;p_Src8++;
3190aeed3e9SJustin Hibbits         size--;
3200aeed3e9SJustin Hibbits     }
3210aeed3e9SJustin Hibbits 
3220aeed3e9SJustin Hibbits     /* dest is aligned and source is not necessarily aligned */
3230aeed3e9SJustin Hibbits     leftAlign = (uint32_t)((PTR_TO_UINT(p_Src8) & 3) << 3); /* leftAlign = (pSrc mod 4)*8 */
3240aeed3e9SJustin Hibbits     rightAlign = 32 - leftAlign;
3250aeed3e9SJustin Hibbits 
3260aeed3e9SJustin Hibbits     if (leftAlign == 0)
3270aeed3e9SJustin Hibbits     {
3280aeed3e9SJustin Hibbits         /* source is also aligned */
3290aeed3e9SJustin Hibbits         p_Src32 = (uint32_t*)(p_Src8);
3300aeed3e9SJustin Hibbits         p_Dst32 = (uint32_t*)(p_Dst8);
3310aeed3e9SJustin Hibbits         while (size >> 2) /* size >= 4 */
3320aeed3e9SJustin Hibbits         {
3330aeed3e9SJustin Hibbits             *p_Dst32 = GET_UINT32(*p_Src32);
3340aeed3e9SJustin Hibbits             p_Dst32++;p_Src32++;
3350aeed3e9SJustin Hibbits             size -= 4;
3360aeed3e9SJustin Hibbits         }
3370aeed3e9SJustin Hibbits         p_Src8 = (uint8_t*)(p_Src32);
3380aeed3e9SJustin Hibbits         p_Dst8 = (uint8_t*)(p_Dst32);
3390aeed3e9SJustin Hibbits     }
3400aeed3e9SJustin Hibbits     else
3410aeed3e9SJustin Hibbits     {
3420aeed3e9SJustin Hibbits         /* source is not aligned (destination is aligned)*/
3430aeed3e9SJustin Hibbits         p_Src32 = (uint32_t*)(p_Src8 - (leftAlign >> 3));
3440aeed3e9SJustin Hibbits         p_Dst32 = (uint32_t*)(p_Dst8);
3450aeed3e9SJustin Hibbits         lastWord = GET_UINT32(*p_Src32);
3460aeed3e9SJustin Hibbits         p_Src32++;
3470aeed3e9SJustin Hibbits         while(size >> 3) /* size >= 8 */
3480aeed3e9SJustin Hibbits         {
3490aeed3e9SJustin Hibbits             currWord = GET_UINT32(*p_Src32);
3500aeed3e9SJustin Hibbits             *p_Dst32 = (lastWord << leftAlign) | (currWord >> rightAlign);
3510aeed3e9SJustin Hibbits             lastWord = currWord;
3520aeed3e9SJustin Hibbits             p_Src32++;p_Dst32++;
3530aeed3e9SJustin Hibbits             size -= 4;
3540aeed3e9SJustin Hibbits         }
3550aeed3e9SJustin Hibbits         p_Dst8 = (uint8_t*)(p_Dst32);
3560aeed3e9SJustin Hibbits         p_Src8 = (uint8_t*)(p_Src32) - 4 + (leftAlign >> 3);
3570aeed3e9SJustin Hibbits     }
3580aeed3e9SJustin Hibbits 
3590aeed3e9SJustin Hibbits     /* complete the left overs */
3600aeed3e9SJustin Hibbits     while (size--)
3610aeed3e9SJustin Hibbits     {
362852ba100SJustin Hibbits         *p_Dst8 = GET_UINT8(*p_Src8);
3630aeed3e9SJustin Hibbits         p_Dst8++;p_Src8++;
3640aeed3e9SJustin Hibbits     }
3650aeed3e9SJustin Hibbits 
3660aeed3e9SJustin Hibbits     return pDst;
3670aeed3e9SJustin Hibbits }
3680aeed3e9SJustin Hibbits 
MemCpy64(void * pDst,void * pSrc,uint32_t size)3690aeed3e9SJustin Hibbits void * MemCpy64(void* pDst,void* pSrc, uint32_t size)
3700aeed3e9SJustin Hibbits {
3710aeed3e9SJustin Hibbits     uint32_t leftAlign;
3720aeed3e9SJustin Hibbits     uint32_t rightAlign;
3730aeed3e9SJustin Hibbits     uint64_t lastWord;
3740aeed3e9SJustin Hibbits     uint64_t currWord;
3750aeed3e9SJustin Hibbits     uint64_t *pSrc64;
3760aeed3e9SJustin Hibbits     uint64_t *pDst64;
3770aeed3e9SJustin Hibbits     uint8_t  *p_Src8;
3780aeed3e9SJustin Hibbits     uint8_t  *p_Dst8;
3790aeed3e9SJustin Hibbits 
3800aeed3e9SJustin Hibbits     p_Src8 = (uint8_t*)(pSrc);
3810aeed3e9SJustin Hibbits     p_Dst8 = (uint8_t*)(pDst);
3820aeed3e9SJustin Hibbits     /* first copy byte by byte till the source first alignment
3830aeed3e9SJustin Hibbits      * this step is necessarily to ensure we do not even try to access
3840aeed3e9SJustin Hibbits      * data which is before the source buffer, hence it is not ours.
3850aeed3e9SJustin Hibbits      */
3860aeed3e9SJustin Hibbits     while((PTR_TO_UINT(p_Src8) & 7) && size) /* (pSrc mod 8) > 0 and size > 0 */
3870aeed3e9SJustin Hibbits     {
3880aeed3e9SJustin Hibbits         *p_Dst8++ = *p_Src8++;
3890aeed3e9SJustin Hibbits         size--;
3900aeed3e9SJustin Hibbits     }
3910aeed3e9SJustin Hibbits 
3920aeed3e9SJustin Hibbits     /* align destination (possibly disaligning source)*/
3930aeed3e9SJustin Hibbits     while((PTR_TO_UINT(p_Dst8) & 7) && size) /* (pDst mod 8) > 0 and size > 0 */
3940aeed3e9SJustin Hibbits     {
3950aeed3e9SJustin Hibbits         *p_Dst8++ = *p_Src8++;
3960aeed3e9SJustin Hibbits         size--;
3970aeed3e9SJustin Hibbits     }
3980aeed3e9SJustin Hibbits 
3990aeed3e9SJustin Hibbits     /* dest is aligned and source is not necessarily aligned */
4000aeed3e9SJustin Hibbits     leftAlign = (uint32_t)((PTR_TO_UINT(p_Src8) & 7) << 3); /* leftAlign = (pSrc mod 8)*8 */
4010aeed3e9SJustin Hibbits     rightAlign = 64 - leftAlign;
4020aeed3e9SJustin Hibbits 
4030aeed3e9SJustin Hibbits 
4040aeed3e9SJustin Hibbits     if (leftAlign == 0)
4050aeed3e9SJustin Hibbits     {
4060aeed3e9SJustin Hibbits         /* source is also aligned */
4070aeed3e9SJustin Hibbits         pSrc64 = (uint64_t*)(p_Src8);
4080aeed3e9SJustin Hibbits         pDst64 = (uint64_t*)(p_Dst8);
4090aeed3e9SJustin Hibbits         while (size >> 3) /* size >= 8 */
4100aeed3e9SJustin Hibbits         {
4110aeed3e9SJustin Hibbits             *pDst64++ = *pSrc64++;
4120aeed3e9SJustin Hibbits             size -= 8;
4130aeed3e9SJustin Hibbits         }
4140aeed3e9SJustin Hibbits         p_Src8 = (uint8_t*)(pSrc64);
4150aeed3e9SJustin Hibbits         p_Dst8 = (uint8_t*)(pDst64);
4160aeed3e9SJustin Hibbits     }
4170aeed3e9SJustin Hibbits     else
4180aeed3e9SJustin Hibbits     {
4190aeed3e9SJustin Hibbits         /* source is not aligned (destination is aligned)*/
4200aeed3e9SJustin Hibbits         pSrc64 = (uint64_t*)(p_Src8 - (leftAlign >> 3));
4210aeed3e9SJustin Hibbits         pDst64 = (uint64_t*)(p_Dst8);
4220aeed3e9SJustin Hibbits         lastWord = *pSrc64++;
4230aeed3e9SJustin Hibbits         while(size >> 4) /* size >= 16 */
4240aeed3e9SJustin Hibbits         {
4250aeed3e9SJustin Hibbits             currWord = *pSrc64;
4260aeed3e9SJustin Hibbits             *pDst64 = (lastWord << leftAlign) | (currWord >> rightAlign);
4270aeed3e9SJustin Hibbits             lastWord = currWord;
4280aeed3e9SJustin Hibbits             pSrc64++;
4290aeed3e9SJustin Hibbits             pDst64++;
4300aeed3e9SJustin Hibbits             size -= 8;
4310aeed3e9SJustin Hibbits         }
4320aeed3e9SJustin Hibbits         p_Dst8 = (uint8_t*)(pDst64);
4330aeed3e9SJustin Hibbits         p_Src8 = (uint8_t*)(pSrc64) - 8 + (leftAlign >> 3);
4340aeed3e9SJustin Hibbits     }
4350aeed3e9SJustin Hibbits 
4360aeed3e9SJustin Hibbits     /* complete the left overs */
4370aeed3e9SJustin Hibbits     while (size--)
4380aeed3e9SJustin Hibbits         *p_Dst8++ = *p_Src8++;
4390aeed3e9SJustin Hibbits 
4400aeed3e9SJustin Hibbits     return pDst;
4410aeed3e9SJustin Hibbits }
4420aeed3e9SJustin Hibbits 
MemSet32(void * pDst,uint8_t val,uint32_t size)4430aeed3e9SJustin Hibbits void * MemSet32(void* pDst, uint8_t val, uint32_t size)
4440aeed3e9SJustin Hibbits {
4450aeed3e9SJustin Hibbits     uint32_t val32;
4460aeed3e9SJustin Hibbits     uint32_t *p_Dst32;
4470aeed3e9SJustin Hibbits     uint8_t  *p_Dst8;
4480aeed3e9SJustin Hibbits 
4490aeed3e9SJustin Hibbits     p_Dst8 = (uint8_t*)(pDst);
4500aeed3e9SJustin Hibbits 
4510aeed3e9SJustin Hibbits     /* generate four 8-bit val's in 32-bit container */
4520aeed3e9SJustin Hibbits     val32  = (uint32_t) val;
4530aeed3e9SJustin Hibbits     val32 |= (val32 <<  8);
4540aeed3e9SJustin Hibbits     val32 |= (val32 << 16);
4550aeed3e9SJustin Hibbits 
4560aeed3e9SJustin Hibbits     /* align destination to 32 */
4570aeed3e9SJustin Hibbits     while((PTR_TO_UINT(p_Dst8) & 3) && size) /* (pDst mod 4) > 0 and size > 0 */
4580aeed3e9SJustin Hibbits     {
4590aeed3e9SJustin Hibbits         *p_Dst8++ = val;
4600aeed3e9SJustin Hibbits         size--;
4610aeed3e9SJustin Hibbits     }
4620aeed3e9SJustin Hibbits 
4630aeed3e9SJustin Hibbits     /* 32-bit chunks */
4640aeed3e9SJustin Hibbits     p_Dst32 = (uint32_t*)(p_Dst8);
4650aeed3e9SJustin Hibbits     while (size >> 2) /* size >= 4 */
4660aeed3e9SJustin Hibbits     {
4670aeed3e9SJustin Hibbits         *p_Dst32++ = val32;
4680aeed3e9SJustin Hibbits         size -= 4;
4690aeed3e9SJustin Hibbits     }
4700aeed3e9SJustin Hibbits 
4710aeed3e9SJustin Hibbits     /* complete the leftovers */
4720aeed3e9SJustin Hibbits     p_Dst8 = (uint8_t*)(p_Dst32);
4730aeed3e9SJustin Hibbits     while (size--)
4740aeed3e9SJustin Hibbits         *p_Dst8++ = val;
4750aeed3e9SJustin Hibbits 
4760aeed3e9SJustin Hibbits     return pDst;
4770aeed3e9SJustin Hibbits }
4780aeed3e9SJustin Hibbits 
IOMemSet32(void * pDst,uint8_t val,uint32_t size)4790aeed3e9SJustin Hibbits void * IOMemSet32(void* pDst, uint8_t val, uint32_t size)
4800aeed3e9SJustin Hibbits {
4810aeed3e9SJustin Hibbits     uint32_t val32;
4820aeed3e9SJustin Hibbits     uint32_t *p_Dst32;
4830aeed3e9SJustin Hibbits     uint8_t  *p_Dst8;
4840aeed3e9SJustin Hibbits 
4850aeed3e9SJustin Hibbits     p_Dst8 = (uint8_t*)(pDst);
4860aeed3e9SJustin Hibbits 
4870aeed3e9SJustin Hibbits     /* generate four 8-bit val's in 32-bit container */
4880aeed3e9SJustin Hibbits     val32  = (uint32_t) val;
4890aeed3e9SJustin Hibbits     val32 |= (val32 <<  8);
4900aeed3e9SJustin Hibbits     val32 |= (val32 << 16);
4910aeed3e9SJustin Hibbits 
4920aeed3e9SJustin Hibbits     /* align destination to 32 */
4930aeed3e9SJustin Hibbits     while((PTR_TO_UINT(p_Dst8) & 3) && size) /* (pDst mod 4) > 0 and size > 0 */
4940aeed3e9SJustin Hibbits     {
495852ba100SJustin Hibbits         WRITE_UINT8(*p_Dst8, val);
4960aeed3e9SJustin Hibbits         p_Dst8++;
4970aeed3e9SJustin Hibbits         size--;
4980aeed3e9SJustin Hibbits     }
4990aeed3e9SJustin Hibbits 
5000aeed3e9SJustin Hibbits     /* 32-bit chunks */
5010aeed3e9SJustin Hibbits     p_Dst32 = (uint32_t*)(p_Dst8);
5020aeed3e9SJustin Hibbits     while (size >> 2) /* size >= 4 */
5030aeed3e9SJustin Hibbits     {
5040aeed3e9SJustin Hibbits         WRITE_UINT32(*p_Dst32, val32);
5050aeed3e9SJustin Hibbits         p_Dst32++;
5060aeed3e9SJustin Hibbits         size -= 4;
5070aeed3e9SJustin Hibbits     }
5080aeed3e9SJustin Hibbits 
5090aeed3e9SJustin Hibbits     /* complete the leftovers */
5100aeed3e9SJustin Hibbits     p_Dst8 = (uint8_t*)(p_Dst32);
5110aeed3e9SJustin Hibbits     while (size--)
5120aeed3e9SJustin Hibbits     {
513852ba100SJustin Hibbits         WRITE_UINT8(*p_Dst8, val);
5140aeed3e9SJustin Hibbits         p_Dst8++;
5150aeed3e9SJustin Hibbits     }
5160aeed3e9SJustin Hibbits 
5170aeed3e9SJustin Hibbits     return pDst;
5180aeed3e9SJustin Hibbits }
5190aeed3e9SJustin Hibbits 
MemSet64(void * pDst,uint8_t val,uint32_t size)5200aeed3e9SJustin Hibbits void * MemSet64(void* pDst, uint8_t val, uint32_t size)
5210aeed3e9SJustin Hibbits {
5220aeed3e9SJustin Hibbits     uint64_t val64;
5230aeed3e9SJustin Hibbits     uint64_t *pDst64;
5240aeed3e9SJustin Hibbits     uint8_t  *p_Dst8;
5250aeed3e9SJustin Hibbits 
5260aeed3e9SJustin Hibbits     p_Dst8 = (uint8_t*)(pDst);
5270aeed3e9SJustin Hibbits 
5280aeed3e9SJustin Hibbits     /* generate four 8-bit val's in 32-bit container */
5290aeed3e9SJustin Hibbits     val64  = (uint64_t) val;
5300aeed3e9SJustin Hibbits     val64 |= (val64 <<  8);
5310aeed3e9SJustin Hibbits     val64 |= (val64 << 16);
5320aeed3e9SJustin Hibbits     val64 |= (val64 << 24);
5330aeed3e9SJustin Hibbits     val64 |= (val64 << 32);
5340aeed3e9SJustin Hibbits 
5350aeed3e9SJustin Hibbits     /* align destination to 64 */
5360aeed3e9SJustin Hibbits     while((PTR_TO_UINT(p_Dst8) & 7) && size) /* (pDst mod 8) > 0 and size > 0 */
5370aeed3e9SJustin Hibbits     {
5380aeed3e9SJustin Hibbits         *p_Dst8++ = val;
5390aeed3e9SJustin Hibbits         size--;
5400aeed3e9SJustin Hibbits     }
5410aeed3e9SJustin Hibbits 
5420aeed3e9SJustin Hibbits     /* 64-bit chunks */
5430aeed3e9SJustin Hibbits     pDst64 = (uint64_t*)(p_Dst8);
5440aeed3e9SJustin Hibbits     while (size >> 4) /* size >= 8 */
5450aeed3e9SJustin Hibbits     {
5460aeed3e9SJustin Hibbits         *pDst64++ = val64;
5470aeed3e9SJustin Hibbits         size -= 8;
5480aeed3e9SJustin Hibbits     }
5490aeed3e9SJustin Hibbits 
5500aeed3e9SJustin Hibbits     /* complete the leftovers */
5510aeed3e9SJustin Hibbits     p_Dst8 = (uint8_t*)(pDst64);
5520aeed3e9SJustin Hibbits     while (size--)
5530aeed3e9SJustin Hibbits         *p_Dst8++ = val;
5540aeed3e9SJustin Hibbits 
5550aeed3e9SJustin Hibbits     return pDst;
5560aeed3e9SJustin Hibbits }
5570aeed3e9SJustin Hibbits 
MemDisp(uint8_t * p,int size)5580aeed3e9SJustin Hibbits void MemDisp(uint8_t *p, int size)
5590aeed3e9SJustin Hibbits {
5600aeed3e9SJustin Hibbits     uint32_t    space = (uint32_t)(PTR_TO_UINT(p) & 0x3);
5610aeed3e9SJustin Hibbits     uint8_t     *p_Limit;
5620aeed3e9SJustin Hibbits 
5630aeed3e9SJustin Hibbits     if (space)
5640aeed3e9SJustin Hibbits     {
5650aeed3e9SJustin Hibbits         p_Limit = (p - space + 4);
5660aeed3e9SJustin Hibbits 
5670aeed3e9SJustin Hibbits         XX_Print("0x%08X: ", (p - space));
5680aeed3e9SJustin Hibbits 
5690aeed3e9SJustin Hibbits         while (space--)
5700aeed3e9SJustin Hibbits         {
5710aeed3e9SJustin Hibbits             XX_Print("--");
5720aeed3e9SJustin Hibbits         }
5730aeed3e9SJustin Hibbits         while (size  && (p < p_Limit))
5740aeed3e9SJustin Hibbits         {
5750aeed3e9SJustin Hibbits             XX_Print("%02x", *(uint8_t*)p);
5760aeed3e9SJustin Hibbits             size--;
5770aeed3e9SJustin Hibbits             p++;
5780aeed3e9SJustin Hibbits         }
5790aeed3e9SJustin Hibbits 
5800aeed3e9SJustin Hibbits         XX_Print(" ");
5810aeed3e9SJustin Hibbits         p_Limit += 12;
5820aeed3e9SJustin Hibbits 
5830aeed3e9SJustin Hibbits         while ((size > 3) && (p < p_Limit))
5840aeed3e9SJustin Hibbits         {
5850aeed3e9SJustin Hibbits             XX_Print("%08x ", *(uint32_t*)p);
5860aeed3e9SJustin Hibbits             size -= 4;
5870aeed3e9SJustin Hibbits             p += 4;
5880aeed3e9SJustin Hibbits         }
5890aeed3e9SJustin Hibbits         XX_Print("\r\n");
5900aeed3e9SJustin Hibbits     }
5910aeed3e9SJustin Hibbits 
5920aeed3e9SJustin Hibbits     while (size > 15)
5930aeed3e9SJustin Hibbits     {
5940aeed3e9SJustin Hibbits         XX_Print("0x%08X: %08x %08x %08x %08x\r\n",
5950aeed3e9SJustin Hibbits                  p, *(uint32_t *)p, *(uint32_t *)(p + 4),
5960aeed3e9SJustin Hibbits                  *(uint32_t *)(p + 8), *(uint32_t *)(p + 12));
5970aeed3e9SJustin Hibbits         size -= 16;
5980aeed3e9SJustin Hibbits         p += 16;
5990aeed3e9SJustin Hibbits     }
6000aeed3e9SJustin Hibbits 
6010aeed3e9SJustin Hibbits     if (size)
6020aeed3e9SJustin Hibbits     {
6030aeed3e9SJustin Hibbits         XX_Print("0x%08X: ", p);
6040aeed3e9SJustin Hibbits 
6050aeed3e9SJustin Hibbits         while (size > 3)
6060aeed3e9SJustin Hibbits         {
6070aeed3e9SJustin Hibbits             XX_Print("%08x ", *(uint32_t *)p);
6080aeed3e9SJustin Hibbits             size -= 4;
6090aeed3e9SJustin Hibbits             p += 4;
6100aeed3e9SJustin Hibbits         }
6110aeed3e9SJustin Hibbits         while (size)
6120aeed3e9SJustin Hibbits         {
6130aeed3e9SJustin Hibbits             XX_Print("%02x", *(uint8_t *)p);
6140aeed3e9SJustin Hibbits             size--;
6150aeed3e9SJustin Hibbits             p++;
6160aeed3e9SJustin Hibbits         }
6170aeed3e9SJustin Hibbits 
6180aeed3e9SJustin Hibbits         XX_Print("\r\n");
6190aeed3e9SJustin Hibbits     }
6200aeed3e9SJustin Hibbits }
621