strings.inc 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487
  1. {
  2. $Id$
  3. This file is part of the Free Pascal run time library.
  4. Copyright (c) 2000 by Jonas Maebe, member of the
  5. Free Pascal development team
  6. Processor dependent part of strings.pp, that can be shared with
  7. sysutils unit.
  8. See the file COPYING.FPC, included in this distribution,
  9. for details about the copyright.
  10. This program is distributed in the hope that it will be useful,
  11. but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  13. **********************************************************************}
  14. { Note: the implementation of these routines is for BIG ENDIAN only!! (JM) }
  15. function strcopy(dest,source : pchar) : pchar;assembler;
  16. { in: dest in r3, source in r4 }
  17. { out: result (dest) in r3 }
  18. asm
  19. { in: dest in r3, source in r4 }
  20. { out: result (dest) in r3 }
  21. { load the begin of the source string in the data cache }
  22. dcbt 0,r4
  23. { get # of misaligned bytes }
  24. rlwinm. r10,r4,0,31-2+1,31
  25. subfic r10,r10,4
  26. mtctr r10
  27. { since we have to return dest intact, use another register for }
  28. { dest in the copy loop }
  29. subi r9,r3,1
  30. subi r4,r4,1
  31. beq LStrCopyAligned
  32. LStrCopyAlignLoop:
  33. { load next byte }
  34. lbzu r0,1(r4)
  35. { end of string? }
  36. cmplwi cr0,r0,0
  37. { store byte }
  38. stbu r0,1(r9)
  39. { loop if misaligned bytes left and not end of string found }
  40. bdnzf eq,LStrCopyAlignLoop
  41. beq LStrCopyDone
  42. LStrCopyAligned:
  43. subi r4,r4,3
  44. subi r9,r9,3
  45. { setup magic constants }
  46. lis r8,(0xfefefeff)@ha
  47. addi r8,r8,(0xfefefeff)@l
  48. lis r7,(0x80808080)@ha
  49. addi r7,r7,(0x80808080)@l
  50. { load first 4 bytes }
  51. lwzu r0,4(r4)
  52. LStrCopyAlignedLoop:
  53. { test for zero byte }
  54. add r10,r0,r8
  55. andc r10,r10,r0
  56. and. r10,r10,r7
  57. bne LStrCopyEndFound
  58. stwu r0,4(r9)
  59. { load next 4 bytes (do it here so the load can begin while the }
  60. { the branch is processed) }
  61. lwzu r0,4(r4)
  62. b LStrCopyAlignedLoop
  63. LStrCopyEndFound:
  64. { adjust for possible $01 bytes coming before the terminating 0 byte }
  65. rlwinm r8,r0,7,0,31
  66. andc r10,r10,r8
  67. { result is either 0, 8, 16 or 24 depending on which byte is zero }
  68. cntlzw r10,r10
  69. addi r9,r9,3
  70. LStrCopyWrapUpLoop:
  71. subic. r10,r10,8
  72. rlwinm r0,r0,8,0,31
  73. stbu r0,1(r9)
  74. bge LStrCopyWrapUpLoop
  75. LStrCopyDone:
  76. { r3 still contains dest here }
  77. end ['R4','R7','R8','R0','R9','R10','CR0','CTR'];
  78. function strecopy(dest,source : pchar) : pchar;assembler;
  79. { in: dest in r3, source in r4 }
  80. { out: result (end of new dest) in r3 }
  81. asm
  82. { load the begin of the source string in the data cache }
  83. dcbt 0,r4
  84. { get # of misaligned bytes }
  85. rlwinm. r10,r4,0,31-2+1,31
  86. subfic r10,r10,4
  87. mtctr r10
  88. subi r3,r3,1
  89. subi r4,r4,1
  90. beq LStrECopyAligned
  91. LStrECopyAlignLoop:
  92. { load next byte }
  93. lbzu r0,1(r4)
  94. { end of string? }
  95. cmplwi cr0,r0,0
  96. { store byte }
  97. stbu r0,1(r3)
  98. { loop if misaligned bytes left and not end of string found }
  99. bdnzf eq,LStrECopyAlignLoop
  100. beq LStrECopyDone
  101. LStrECopyAligned:
  102. subi r4,r4,3
  103. subi r3,r3,3
  104. { setup magic constants }
  105. lis r8,(0xfefefeff)@ha
  106. addi r8,r8,(0xfefefeff)@l
  107. lis r7,(0x80808080)@ha
  108. addi r7,r7,(0x80808080)@l
  109. {
  110. li r8,-257 { 0x0feff }
  111. andis. r8,r8,0x0fefe
  112. li r9,-32640 { 0x08080 }
  113. andis. r9,r9,0x08080
  114. }
  115. LStrECopyAlignedLoop:
  116. { load next 4 bytes }
  117. lwzu r0,4(r4)
  118. { test for zero byte }
  119. add r10,r0,r8
  120. andc r10,r10,r0
  121. and. r10,r10,r7
  122. bne LStrECopyEndFound
  123. stwu r0,4(r3)
  124. b LStrECopyAlignedLoop
  125. LStrECopyEndFound:
  126. { adjust for possible $01 bytes coming before the terminating 0 byte }
  127. rlwinm r8,r0,7,0,31
  128. andc r10,r10,r8
  129. { result is either 0, 8, 16 or 24 depending on which byte is zero }
  130. cntlzw r10,r10
  131. addi r3,r3,3
  132. LStrECopyWrapUpLoop:
  133. subic. r10,r10,8
  134. rlwinm r0,r0,8,0,31
  135. stbu r0,1(r3)
  136. bge LStrECopyWrapUpLoop
  137. LStrECopyDone:
  138. { r3 contains new dest here }
  139. end ['R3','R4','R8','R0','R3','R9','R10','CR0','CTR'];
  140. function strlcopy(dest,source : pchar;maxlen : longint) : pchar;assembler;
  141. { in: dest in r3, source in r4, maxlen in r5 }
  142. { out: result (dest) in r3 }
  143. asm
  144. { load the begin of the source string in the data cache }
  145. dcbt 0,r4
  146. mtctr r5
  147. subi r4,r4,1
  148. subi r10,r3,1
  149. LStrlCopyLoop:
  150. lbzu r0,1(r4)
  151. cmplwi r0,0
  152. stbu r0,1(r10)
  153. bdnzf cr0*4+eq, LStrlCopyLoop
  154. { if we stopped because we copied a #0, we're done }
  155. beq LStrlCopyDone
  156. { otherwise add the #0 }
  157. li r0,0
  158. stb r0,1(r10)
  159. LStrlCopyDone:
  160. end ['R0','R4','R10','CR0'];
  161. function strlen(p : pchar) : longint;assembler;
  162. {$i strlen.inc}
  163. function strend(p : pchar) : pchar;assembler;
  164. { in: p in r3 }
  165. { out: result (end of p) in r3 }
  166. asm
  167. { load the begin of the string in the data cache }
  168. dcbt 0,r3
  169. { empty/invalid string? }
  170. cmplwi r3,0
  171. { if yes, do nothing }
  172. beq LStrEndDone
  173. subi r3,r3,1
  174. LStrEndLoop:
  175. lbzu r0,1(r3)
  176. cmplwi r0,0
  177. bne LStrEndLoop
  178. LStrEndDone:
  179. end ['R0','R3','R4','CR0'];
  180. function strcomp(str1,str2 : pchar) : longint;assembler;
  181. { in: str1 in r3, str2 in r4 }
  182. { out: result (= 0 if strings equal, < 0 if str1 < str2, > 0 if str1 > str2 }
  183. { in r3 }
  184. asm
  185. { use r0 instead of r3 for str1 since r3 contains result }
  186. subi r9,r3,1
  187. subi r4,r4,1
  188. LStrCompLoop:
  189. { load next chars }
  190. lbzu r0,1(r9)
  191. { check if one is zero }
  192. cmplwi cr1,r0,0
  193. lbzu r10,1(r4)
  194. { calculate difference }
  195. sub. r3,r0,r10
  196. { if chars not equal, we're ready }
  197. bne LStrCompDone
  198. { if they are equal and one is zero, then the other one is zero too }
  199. { and we're done as well (r3 also contains 0 then) }
  200. { otherwise loop }
  201. bne cr1,LStrCompLoop
  202. LStrCompDone:
  203. end ['R0','R3','R4','R9','R10','CR0','CR1'];
  204. function strlcomp(str1,str2 : pchar;l : longint) : longint;assembler;
  205. { (same as strcomp, but maximally compare until l'th character) }
  206. { in: str1 in r3, str2 in r4, l in r5 }
  207. { out: result (= 0 if strings equal, < 0 if str1 < str2, > 0 if str1 > str2 }
  208. { in r3 }
  209. asm
  210. { load the begin of one of the strings in the data cache }
  211. dcbt 0,r3
  212. { use r0 instead of r3 for str1 since r3 contains result }
  213. cmplwi r5,0
  214. subi r9,r3,1
  215. li r3,0
  216. beq LStrlCompDone
  217. mtctr r5
  218. subi r4,r4,1
  219. LStrlCompLoop:
  220. { load next chars }
  221. lbzu r0,1(r9)
  222. { check if one is zero }
  223. cmplwi cr1,r0,0
  224. lbzu r10,1(r4)
  225. { calculate difference }
  226. sub. r3,r0,r10
  227. { if chars not equal, we're ready }
  228. bne LStrlCompDone
  229. { if they are equal and one is zero, then the other one is zero too }
  230. { and we're done as well (r3 also contains 0 then) }
  231. { otherwise loop (if ctr <> 0) }
  232. bdnzf cr1*4+eq,LStrlCompLoop
  233. LStrlCompDone:
  234. end ['R0','R3','R4','R9','R10','CR0','CR1','CTR'];
  235. function stricomp(str1,str2 : pchar) : longint;assembler;
  236. { in: str1 in r3, str2 in r4 }
  237. { out: result of case insensitive comparison (< 0, = 0, > 0) }
  238. asm
  239. { use r28 instead of r3 for str1 since r3 contains result }
  240. subi r28,r3,1
  241. subi r4,r4,1
  242. LStriCompLoop:
  243. { load next chars }
  244. lbzu r29,1(r28)
  245. { check if one is zero }
  246. cmplwi cr1,r29,0
  247. lbzu r30,1(r4)
  248. { calculate difference }
  249. sub. r3,r29,r30
  250. { if chars are equal, no further test is necessary }
  251. beq+ LStriCompEqual
  252. { make both lowercase, no branches }
  253. li r27,0
  254. li r25,0
  255. { r3 := r29 - 'A' }
  256. subic r3,r29,'A'
  257. { if r29 < 'A' then r27 := 0 else r27 := $ffffffff }
  258. addme r27,r27
  259. { same for r30 }
  260. subic r3,r30,'A'
  261. addme r25,r25
  262. { r3 := 'Z' - r29 }
  263. subfic r3,r29,'Z'
  264. { if r29 < 'A' then r27 := 0 else r27 := $20 }
  265. andi. r27,r27,0x020
  266. { if r29 > Z then r26 := 0 else r26 := $ffffffff }
  267. subfe r26,r26,r26
  268. { same for r30 }
  269. subfic r3,r30,'Z'
  270. andi. r25,r25,0x020
  271. subfe r24,r24,r24
  272. { if (r29 in ['A'..'Z'] then r27 := $20 else r27 := 0 }
  273. and r27,r27,r26
  274. { same for r30 }
  275. and r25,r25,r24
  276. { make lowercase }
  277. add r29,r29,r27
  278. { same for r30 }
  279. add r30,r30,r25
  280. { compare again }
  281. sub. r3,r29,r30
  282. bne LStriCompDone
  283. LStriCompEqual:
  284. { if they are equal and one is zero, then the other one is zero too }
  285. { and we're done as well (r3 also contains 0 then) }
  286. { otherwise loop }
  287. bne cr1,LStriCompLoop
  288. LStriCompDone:
  289. end ['R3','R4','R26','R27','R28','R29','R30','CR0','CR1'];
  290. function strlicomp(str1,str2 : pchar;l : longint) : longint;assembler;
  291. { (same as stricomp, but maximally compare until l'th character) }
  292. { in: str1 in r3, str2 in r4, l in r5 }
  293. { out: result of case insensitive comparison (< 0, = 0, > 0) }
  294. asm
  295. { load the begin of one of the string in the data cache }
  296. dcbt 0,r3
  297. { use r0 instead of r3 for str1 since r3 contains result }
  298. cmplwi r5,0
  299. subi r9,r3,1
  300. li r3,0
  301. beq- LStrliCompDone
  302. mtctr r5
  303. subi r4,r4,1
  304. LStrliCompLoop:
  305. { load next chars }
  306. lbzu r0,1(r9)
  307. { check if one is zero }
  308. cmplwi cr1,r0,0
  309. lbzu r10,1(r4)
  310. { calculate difference }
  311. sub. r3,r0,r10
  312. { if chars are equal, no further test is necessary }
  313. beq LStrliCompEqual
  314. { see stricomp for explanation }
  315. li r8,0
  316. li r5,0
  317. subic r3,r0,'A'
  318. addme r8,r8
  319. subic r3,r10,'A'
  320. addme r5,r5
  321. subfic r3,r0,'Z'
  322. andi. r8,r8,0x020
  323. subfe r7,r7,r7
  324. subfic r3,r10,'Z'
  325. andi. r5,r5,0x020
  326. subfe r24,r24,r24
  327. and r8,r8,r7
  328. and r5,r5,r24
  329. add r0,r0,r8
  330. add r10,r10,r5
  331. { compare again }
  332. sub. r3,r0,r10
  333. bne LStrliCompDone
  334. LStrliCompEqual:
  335. { if they are equal and one is zero, then the other one is zero too }
  336. { and we're done as well (r3 also contains 0 then) }
  337. { otherwise loop (if ctr <> 0) }
  338. bdnzf cr1*4+eq,LStrliCompLoop
  339. LStrliCompDone:
  340. end ['R0','R3','R4','R5','R7','R8','R9','R10','CR0','CR1','CTR'];
  341. function strscan(p : pchar;c : char) : pchar;assembler;
  342. asm
  343. { empty/invalid string? }
  344. cmplwi r3,0
  345. { if yes, do nothing }
  346. beq LStrScanDone
  347. subi r3,r3,1
  348. LStrScanLoop:
  349. lbzu r0,1(r3)
  350. cmplwi r0,0
  351. cmplw cr1,r0,r4
  352. bne LStrScanLoop
  353. beq cr1,LStrScanDone
  354. li r3, 0
  355. LStrScanDone:
  356. end ['R0','R3','R4','CR0','CR1'];
  357. function strrscan(p : pchar;c : char) : pchar;assembler;
  358. asm
  359. { empty/invalid string? }
  360. cmplwi r3,0
  361. { if yes, do nothing }
  362. beq LStrrScanDone
  363. { make r5 will be walking through the string }
  364. subi r5,r3,1
  365. { assume not found }
  366. li r3,0
  367. LStrrScanLoop:
  368. lbzu r10,1(r5)
  369. cmplw cr1,r10,r4
  370. cmplwi cr0,r10,0
  371. bne+ cr1,LStrrScanNotFound
  372. { store address of found position }
  373. mr r3,r5
  374. LStrrScanNotFound:
  375. bne LStrrScanLoop
  376. LStrrScanDone:
  377. end ['R0','R3','R4','R10','CR0','CR1'];
  378. function strupper(p : pchar) : pchar;assembler;
  379. asm
  380. cmplwi r3,0
  381. beq LStrUpperNil
  382. subi r9,r3,1
  383. LStrUpperLoop:
  384. lbzu r10,1(r9)
  385. { a <= x <= b <=> cardinal(x-a) <= cardinal(b-a) }
  386. subi r0,r10,97
  387. cmplwi r0,122-97
  388. cmplwi cr1,r10,0
  389. subi r10,r10,0x20
  390. bgt LStrUpper1
  391. stb r10,0(r9)
  392. LStrUpper1:
  393. bne cr1,LStrUpperLoop
  394. LStrUpperNil:
  395. end ['R0','R9','R10','CR0','CR1'];
  396. function strlower(p : pchar) : pchar;assembler;
  397. asm
  398. cmplwi r3,0
  399. beq LStrLowerNil
  400. subi r9,r3,1
  401. LStrLowerLoop:
  402. lbzu r10,1(r9)
  403. { a <= x <= b <=> cardinal(x-a) <= cardinal(b-a) }
  404. subi r0,r10,65
  405. cmplwi r0,90-65
  406. cmplwi cr1,r10,0
  407. addi r10,r10,0x20
  408. bgt LStrLower1
  409. stb r10,0(r9)
  410. LStrLower1:
  411. bne cr1,LStrLowerLoop
  412. LStrLowerNil:
  413. end ['R0','R9','R10','CR0','CR1'];
  414. {
  415. $Log$
  416. Revision 1.18 2003-05-28 19:18:10 jonas
  417. * fixed strcopy and strecopy if there are #1 chars right before the end
  418. of the string to copied
  419. Revision 1.17 2003/05/24 10:16:24 jonas
  420. * fixed strscan and strrscan
  421. Revision 1.16 2003/05/17 12:55:30 florian
  422. * fixed copy&paste bug in strecopy
  423. Revision 1.15 2003/05/17 00:01:13 jonas
  424. * fixed strcopy
  425. Revision 1.14 2002/09/11 07:49:40 jonas
  426. * fixed assembler errors
  427. Revision 1.13 2002/09/07 16:01:26 peter
  428. * old logs removed and tabs fixed
  429. Revision 1.12 2002/09/06 16:58:43 jonas
  430. * fixed wrong references (used r0 as base register)
  431. Revision 1.11 2002/08/10 17:14:36 jonas
  432. * various fixes, mostly changing the names of the modifies registers to
  433. upper case since that seems to be required by the compiler
  434. }