strings.inc 15 KB

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