2005-04-17 02:20:36 +04:00
/ *
* Copyright ( C ) P a u l M a c k e r r a s 1 9 9 7 .
*
* This p r o g r a m i s f r e e s o f t w a r e ; you can redistribute it and/or
* modify i t u n d e r t h e t e r m s o f t h e G N U G e n e r a l P u b l i c L i c e n s e
* as p u b l i s h e d b y t h e F r e e S o f t w a r e F o u n d a t i o n ; either version
* 2 of t h e L i c e n s e , o r ( a t y o u r o p t i o n ) a n y l a t e r v e r s i o n .
*
* NOTE : this c o d e r u n s i n 3 2 b i t m o d e a n d i s p a c k a g e d a s E L F 3 2 .
* /
2005-08-08 07:24:38 +04:00
# include " p p c _ a s m . h "
2005-04-17 02:20:36 +04:00
.text
.globl strcpy
strcpy :
addi r5 ,r3 ,- 1
addi r4 ,r4 ,- 1
1 : lbzu r0 ,1 ( r4 )
cmpwi 0 ,r0 ,0
stbu r0 ,1 ( r5 )
bne 1 b
blr
.globl strncpy
strncpy :
cmpwi 0 ,r5 ,0
beqlr
mtctr r5
addi r6 ,r3 ,- 1
addi r4 ,r4 ,- 1
1 : lbzu r0 ,1 ( r4 )
cmpwi 0 ,r0 ,0
stbu r0 ,1 ( r6 )
bdnzf 2 ,1 b / * d e c c t r , b r a n c h i f c t r ! = 0 & & ! c r0 . e q * /
blr
.globl strcat
strcat :
addi r5 ,r3 ,- 1
addi r4 ,r4 ,- 1
1 : lbzu r0 ,1 ( r5 )
cmpwi 0 ,r0 ,0
bne 1 b
addi r5 ,r5 ,- 1
1 : lbzu r0 ,1 ( r4 )
cmpwi 0 ,r0 ,0
stbu r0 ,1 ( r5 )
bne 1 b
blr
2007-08-28 08:52:57 +04:00
.globl strchr
strchr :
addi r3 ,r3 ,- 1
1 : lbzu r0 ,1 ( r3 )
cmpw 0 ,r0 ,r4
beqlr
cmpwi 0 ,r0 ,0
bne 1 b
li r3 ,0
blr
2005-04-17 02:20:36 +04:00
.globl strcmp
strcmp :
addi r5 ,r3 ,- 1
addi r4 ,r4 ,- 1
1 : lbzu r3 ,1 ( r5 )
cmpwi 1 ,r3 ,0
lbzu r0 ,1 ( r4 )
subf. r3 ,r0 ,r3
beqlr 1
beq 1 b
blr
2007-08-28 08:52:57 +04:00
.globl strncmp
strncmp :
mtctr r5
addi r5 ,r3 ,- 1
addi r4 ,r4 ,- 1
1 : lbzu r3 ,1 ( r5 )
cmpwi 1 ,r3 ,0
lbzu r0 ,1 ( r4 )
subf. r3 ,r0 ,r3
beqlr 1
bdnzt e q ,1 b
blr
2005-04-17 02:20:36 +04:00
.globl strlen
strlen :
addi r4 ,r3 ,- 1
1 : lbzu r0 ,1 ( r4 )
cmpwi 0 ,r0 ,0
bne 1 b
subf r3 ,r3 ,r4
blr
.globl memset
memset :
rlwimi r4 ,r4 ,8 ,1 6 ,2 3
rlwimi r4 ,r4 ,1 6 ,0 ,1 5
addi r6 ,r3 ,- 4
cmplwi 0 ,r5 ,4
blt 7 f
stwu r4 ,4 ( r6 )
beqlr
andi. r0 ,r6 ,3
add r5 ,r0 ,r5
subf r6 ,r0 ,r6
rlwinm r0 ,r5 ,3 2 - 2 ,2 ,3 1
mtctr r0
bdz 6 f
1 : stwu r4 ,4 ( r6 )
bdnz 1 b
6 : andi. r5 ,r5 ,3
7 : cmpwi 0 ,r5 ,0
beqlr
mtctr r5
addi r6 ,r6 ,3
8 : stbu r4 ,1 ( r6 )
bdnz 8 b
blr
.globl memmove
memmove :
cmplw 0 ,r3 ,r4
bgt b a c k w a r d s _ m e m c p y
/* fall through */
.globl memcpy
memcpy :
2005-10-29 04:46:46 +04:00
rlwinm. r7 ,r5 ,3 2 - 3 ,3 ,3 1 / * r7 = r5 > > 3 * /
2005-04-17 02:20:36 +04:00
addi r6 ,r3 ,- 4
addi r4 ,r4 ,- 4
2006-01-14 07:06:51 +03:00
beq 3 f / * i f l e s s t h a n 8 b y t e s t o d o * /
2005-04-17 02:20:36 +04:00
andi. r0 ,r6 ,3 / * g e t d e s t w o r d a l i g n e d * /
mtctr r7
bne 5 f
2006-01-14 07:06:51 +03:00
andi. r0 ,r4 ,3 / * c h e c k s r c w o r d a l i g n e d t o o * /
bne 3 f
2005-04-17 02:20:36 +04:00
1 : lwz r7 ,4 ( r4 )
lwzu r8 ,8 ( r4 )
stw r7 ,4 ( r6 )
stwu r8 ,8 ( r6 )
bdnz 1 b
andi. r5 ,r5 ,7
2 : cmplwi 0 ,r5 ,4
blt 3 f
lwzu r0 ,4 ( r4 )
addi r5 ,r5 ,- 4
stwu r0 ,4 ( r6 )
3 : cmpwi 0 ,r5 ,0
beqlr
mtctr r5
addi r4 ,r4 ,3
addi r6 ,r6 ,3
4 : lbzu r0 ,1 ( r4 )
stbu r0 ,1 ( r6 )
bdnz 4 b
blr
5 : subfic r0 ,r0 ,4
2006-01-14 07:06:51 +03:00
cmpw c r1 ,r0 ,r5
add r7 ,r0 ,r4
andi. r7 ,r7 ,3 / * w i l l s o u r c e b e w o r d - a l i g n e d t o o ? * /
ble c r1 ,3 b
bne 3 b / * d o b y t e - b y - b y t e i f n o t * /
2005-04-17 02:20:36 +04:00
mtctr r0
6 : lbz r7 ,4 ( r4 )
addi r4 ,r4 ,1
stb r7 ,4 ( r6 )
addi r6 ,r6 ,1
bdnz 6 b
subf r5 ,r0 ,r5
rlwinm. r7 ,r5 ,3 2 - 3 ,3 ,3 1
beq 2 b
mtctr r7
b 1 b
.globl backwards_memcpy
backwards_memcpy :
2005-10-29 04:46:46 +04:00
rlwinm. r7 ,r5 ,3 2 - 3 ,3 ,3 1 / * r7 = r5 > > 3 * /
2005-04-17 02:20:36 +04:00
add r6 ,r3 ,r5
add r4 ,r4 ,r5
2006-01-14 07:06:51 +03:00
beq 3 f
2005-04-17 02:20:36 +04:00
andi. r0 ,r6 ,3
mtctr r7
bne 5 f
2006-01-14 07:06:51 +03:00
andi. r0 ,r4 ,3
bne 3 f
2005-04-17 02:20:36 +04:00
1 : lwz r7 ,- 4 ( r4 )
lwzu r8 ,- 8 ( r4 )
stw r7 ,- 4 ( r6 )
stwu r8 ,- 8 ( r6 )
bdnz 1 b
andi. r5 ,r5 ,7
2 : cmplwi 0 ,r5 ,4
blt 3 f
lwzu r0 ,- 4 ( r4 )
subi r5 ,r5 ,4
stwu r0 ,- 4 ( r6 )
3 : cmpwi 0 ,r5 ,0
beqlr
mtctr r5
4 : lbzu r0 ,- 1 ( r4 )
stbu r0 ,- 1 ( r6 )
bdnz 4 b
blr
2006-01-14 07:06:51 +03:00
5 : cmpw c r1 ,r0 ,r5
subf r7 ,r0 ,r4
andi. r7 ,r7 ,3
ble c r1 ,3 b
bne 3 b
mtctr r0
2005-04-17 02:20:36 +04:00
6 : lbzu r7 ,- 1 ( r4 )
stbu r7 ,- 1 ( r6 )
bdnz 6 b
subf r5 ,r0 ,r5
rlwinm. r7 ,r5 ,3 2 - 3 ,3 ,3 1
beq 2 b
mtctr r7
b 1 b
2007-10-12 06:36:20 +04:00
.globl memchr
memchr :
cmpwi 0 ,r5 ,0
blelr
mtctr r5
addi r3 ,r3 ,- 1
1 : lbzu r0 ,1 ( r3 )
cmpw r0 ,r4
beqlr
bdnz 1 b
li r3 ,0
blr
2005-04-17 02:20:36 +04:00
.globl memcmp
memcmp :
cmpwi 0 ,r5 ,0
2008-10-17 13:27:51 +04:00
ble 2 f
2005-04-17 02:20:36 +04:00
mtctr r5
addi r6 ,r3 ,- 1
addi r4 ,r4 ,- 1
1 : lbzu r3 ,1 ( r6 )
lbzu r0 ,1 ( r4 )
subf. r3 ,r0 ,r3
bdnzt 2 ,1 b
blr
2008-10-17 13:27:51 +04:00
2 : li r3 ,0
blr
2005-04-17 02:20:36 +04:00
/ *
* Flush t h e d c a c h e a n d i n v a l i d a t e t h e i c a c h e f o r a r a n g e o f a d d r e s s e s .
*
* flush_ c a c h e ( a d d r , l e n )
* /
.global flush_cache
flush_cache :
addi 4 ,4 ,0 x1 f / * l e n = ( l e n + 0 x1 f ) / 0 x20 * /
rlwinm. 4 ,4 ,2 7 ,5 ,3 1
mtctr 4
beqlr
1 : dcbf 0 ,3
icbi 0 ,3
addi 3 ,3 ,0 x20
bdnz 1 b
sync
isync
blr