strings.inc 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498
  1. {
  2. This file is part of the Free Pascal run time library.
  3. Copyright (c) 2000 by Jonas Maebe, member of the
  4. Free Pascal development team
  5. Processor dependent part of strings.pp, that can be shared with
  6. sysutils unit.
  7. See the file COPYING.FPC, included in this distribution,
  8. for details about the copyright.
  9. This program is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  12. **********************************************************************}
  13. { Note: the implementation of these routines is for BIG ENDIAN only!! (JM) }
  14. {$ifndef FPC_UNIT_HAS_STRCOPY}
  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 cr0*4+eq,.LStrCopyAlignLoop
  42. beq .LStrCopyDone
  43. .LStrCopyAligned:
  44. subi r4,r4,3
  45. subi r9,r9,3
  46. { setup magic constants }
  47. {$if defined(macos) or defined(aix)}
  48. { load constant 0xfefefeff }
  49. lis r8,0xfefe
  50. ori r8,r8,0xfeff
  51. { load constant 0x80808080}
  52. lis r7,0x8080
  53. ori 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. {$endif FPC_UNIT_HAS_STRCOPY}
  89. {$ifndef FPC_UNIT_HAS_STRECOPY}
  90. {$define FPC_UNIT_HAS_STRECOPY}
  91. function strecopy(dest,source : pchar) : pchar;assembler;
  92. { in: dest in r3, source in r4 }
  93. { out: result (end of new dest) in r3 }
  94. asm
  95. { load the begin of the source string in the data cache }
  96. dcbt 0,r4
  97. { get # of misaligned bytes }
  98. rlwinm. r10,r4,0,31-2+1,31
  99. subfic r10,r10,4
  100. mtctr r10
  101. subi r3,r3,1
  102. subi r4,r4,1
  103. beq .LStrECopyAligned
  104. .LStrECopyAlignLoop:
  105. { load next byte }
  106. lbzu r0,1(r4)
  107. { end of string? }
  108. cmplwi cr0,r0,0
  109. { store byte }
  110. stbu r0,1(r3)
  111. { loop if misaligned bytes left and not end of string found }
  112. bdnzf cr0*4+eq,.LStrECopyAlignLoop
  113. beq .LStrECopyDone
  114. .LStrECopyAligned:
  115. subi r4,r4,3
  116. subi r3,r3,3
  117. { setup magic constants }
  118. {$if defined(macos) or defined(aix)}
  119. { load constant 0xfefefeff }
  120. lis r8,0xfefe
  121. ori r8,r8,0xfeff
  122. { load constant 0x80808080}
  123. lis r7,0x8080
  124. ori r7,r7,0x8080
  125. {$else}
  126. lis r8,(0xfefefeff)@ha
  127. addi r8,r8,(0xfefefeff)@l
  128. lis r7,(0x80808080)@ha
  129. addi r7,r7,(0x80808080)@l
  130. {$endif}
  131. .LStrECopyAlignedLoop:
  132. { load next 4 bytes }
  133. lwzu r0,4(r4)
  134. { test for zero byte }
  135. add r10,r0,r8
  136. andc r10,r10,r0
  137. and. r10,r10,r7
  138. bne .LStrECopyEndFound
  139. stwu r0,4(r3)
  140. b .LStrECopyAlignedLoop
  141. .LStrECopyEndFound:
  142. { adjust for possible $01 bytes coming before the terminating 0 byte }
  143. rlwinm r8,r0,7,0,31
  144. andc r10,r10,r8
  145. { result is either 0, 8, 16 or 24 depending on which byte is zero }
  146. cntlzw r10,r10
  147. addi r3,r3,3
  148. .LStrECopyWrapUpLoop:
  149. subic. r10,r10,8
  150. rlwinm r0,r0,8,0,31
  151. stbu r0,1(r3)
  152. bge .LStrECopyWrapUpLoop
  153. .LStrECopyDone:
  154. { r3 contains new dest here }
  155. end;
  156. {$endif FPC_UNIT_HAS_STRECOPY}
  157. {$ifndef FPC_UNIT_HAS_STRLCOPY}
  158. {$define FPC_UNIT_HAS_STRLCOPY}
  159. function strlcopy(dest,source : pchar;maxlen : longint) : pchar;assembler;
  160. { in: dest in r3, source in r4, maxlen in r5 }
  161. { out: result (dest) in r3 }
  162. asm
  163. { load the begin of the source string in the data cache }
  164. dcbt 0,r4
  165. mtctr r5
  166. subi r4,r4,1
  167. subi r10,r3,1
  168. .LStrlCopyLoop:
  169. lbzu r0,1(r4)
  170. cmplwi r0,0
  171. stbu r0,1(r10)
  172. bdnzf cr0*4+eq, .LStrlCopyLoop
  173. { if we stopped because we copied a #0, we're done }
  174. beq .LStrlCopyDone
  175. { otherwise add the #0 }
  176. li r0,0
  177. stb r0,1(r10)
  178. .LStrlCopyDone:
  179. end;
  180. {$endif FPC_UNIT_HAS_STRLCOPY}
  181. {$ifndef FPC_UNIT_HAS_STREND}
  182. {$define FPC_UNIT_HAS_STREND}
  183. function strend(p : pchar) : pchar;assembler;
  184. { in: p in r3 }
  185. { out: result (end of p) in r3 }
  186. asm
  187. { load the begin of the string in the data cache }
  188. dcbt 0,r3
  189. { empty/invalid string? }
  190. cmplwi r3,0
  191. { if yes, do nothing }
  192. beq .LStrEndDone
  193. subi r3,r3,1
  194. .LStrEndLoop:
  195. lbzu r0,1(r3)
  196. cmplwi r0,0
  197. bne .LStrEndLoop
  198. .LStrEndDone:
  199. end;
  200. {$endif FPC_UNIT_HAS_STREND}
  201. {$ifndef FPC_UNIT_HAS_STRCOMP}
  202. {$define FPC_UNIT_HAS_STRCOMP}
  203. function strcomp(str1,str2 : pchar) : longint;assembler;
  204. { in: str1 in r3, str2 in r4 }
  205. { out: result (= 0 if strings equal, < 0 if str1 < str2, > 0 if str1 > str2 }
  206. { in r3 }
  207. asm
  208. { use r0 instead of r3 for str1 since r3 contains result }
  209. subi r9,r3,1
  210. subi r4,r4,1
  211. .LStrCompLoop:
  212. { load next chars }
  213. lbzu r0,1(r9)
  214. { check if one is zero }
  215. cmplwi cr1,r0,0
  216. lbzu r10,1(r4)
  217. { calculate difference }
  218. sub. r3,r0,r10
  219. { if chars not equal, we're ready }
  220. bne .LStrCompDone
  221. { if they are equal and one is zero, then the other one is zero too }
  222. { and we're done as well (r3 also contains 0 then) }
  223. { otherwise loop }
  224. bne cr1,.LStrCompLoop
  225. .LStrCompDone:
  226. end;
  227. {$endif FPC_UNIT_HAS_STRCOMP}
  228. {$ifndef FPC_UNIT_HAS_STRLCOMP}
  229. {$define FPC_UNIT_HAS_STRLCOMP}
  230. function strlcomp(str1,str2 : pchar;l : longint) : longint;assembler;
  231. { (same as strcomp, but maximally compare until l'th character) }
  232. { in: str1 in r3, str2 in r4, l in r5 }
  233. { out: result (= 0 if strings equal, < 0 if str1 < str2, > 0 if str1 > str2 }
  234. { in r3 }
  235. asm
  236. { load the begin of one of the strings in the data cache }
  237. dcbt 0,r3
  238. { use r0 instead of r3 for str1 since r3 contains result }
  239. cmplwi r5,0
  240. subi r9,r3,1
  241. li r3,0
  242. beq .LStrlCompDone
  243. mtctr r5
  244. subi r4,r4,1
  245. .LStrlCompLoop:
  246. { load next chars }
  247. lbzu r0,1(r9)
  248. { check if one is zero }
  249. cmplwi cr1,r0,0
  250. lbzu r10,1(r4)
  251. { calculate difference }
  252. sub. r3,r0,r10
  253. { if chars not equal, we're ready }
  254. bne .LStrlCompDone
  255. { if they are equal and one is zero, then the other one is zero too }
  256. { and we're done as well (r3 also contains 0 then) }
  257. { otherwise loop (if ctr <> 0) }
  258. bdnzf cr1*4+eq,.LStrlCompLoop
  259. .LStrlCompDone:
  260. end;
  261. {$endif FPC_UNIT_HAS_STRLCOMP}
  262. {$ifndef FPC_UNIT_HAS_STRICOMP}
  263. {$define FPC_UNIT_HAS_STRICOMP}
  264. function stricomp(str1,str2 : pchar) : longint;assembler;
  265. { in: str1 in r3, str2 in r4 }
  266. { out: result of case insensitive comparison (< 0, = 0, > 0) }
  267. asm
  268. { use r5 instead of r3 for str1 since r3 contains result }
  269. subi r5,r3,1
  270. subi r4,r4,1
  271. .LStriCompLoop:
  272. { load next chars }
  273. lbzu r6,1(r5)
  274. { check if one is zero }
  275. cmplwi cr1,r6,0
  276. lbzu r7,1(r4)
  277. { calculate difference }
  278. sub. r3,r6,r7
  279. { if chars are equal, no further test is necessary }
  280. beq+ .LStriCompEqual
  281. { make both lowercase, no branches }
  282. { r3 := pred('A') - r6 }
  283. subfic r3,r6,64
  284. { if r6 < 'A' then r8 := 0 else r8 := $ffffffff }
  285. subfe r8,r8,r8
  286. { same for r7 }
  287. subfic r3,r7,64
  288. subfe r9,r9,r9
  289. { r3 := r6 - succ('Z') }
  290. subic r3,r6,91
  291. { if r6 < 'A' then r8 := 0 else r8 := $20 }
  292. andi. r8,r8,0x020
  293. { if r6 > Z then r10 := 0 else r10 := $ffffffff }
  294. subfe r10,r10,r10
  295. { same for r7 }
  296. subic r3,r7,91
  297. andi. r9,r9,0x020
  298. subfe r11,r11,r11
  299. { if (r6 in ['A'..'Z'] then r8 := $20 else r8 := 0 }
  300. and r8,r8,r10
  301. { same for r7 }
  302. and r9,r9,r11
  303. { make lowercase }
  304. add r6,r6,r8
  305. { same for r7 }
  306. add r7,r7,r9
  307. { compare again }
  308. sub. r3,r6,r7
  309. bne- .LStriCompDone
  310. .LStriCompEqual:
  311. { if they are equal and one is zero, then the other one is zero too }
  312. { and we're done as well (r3 also contains 0 then) }
  313. { otherwise loop }
  314. bne cr1,.LStriCompLoop
  315. .LStriCompDone:
  316. end;
  317. {$endif FPC_UNIT_HAS_STRICOMP}
  318. {$ifndef FPC_UNIT_HAS_STRLICOMP}
  319. {$define FPC_UNIT_HAS_STRLICOMP}
  320. function strlicomp(str1,str2 : pchar;l : longint) : longint;assembler;
  321. { (same as stricomp, but maximally compare until l'th character) }
  322. { in: str1 in r3, str2 in r4, l in r5 }
  323. { out: result of case insensitive comparison (< 0, = 0, > 0) }
  324. asm
  325. { load the begin of one of the string in the data cache }
  326. dcbt 0,r3
  327. { use r0 instead of r3 for str1 since r3 contains result }
  328. cmplwi r5,0
  329. subi r9,r3,1
  330. li r3,0
  331. beq- .LStrliCompDone
  332. mtctr r5
  333. subi r4,r4,1
  334. .LStrliCompLoop:
  335. { load next chars }
  336. lbzu r0,1(r9)
  337. { check if one is zero }
  338. cmplwi cr1,r0,0
  339. lbzu r10,1(r4)
  340. { calculate difference }
  341. sub. r3,r0,r10
  342. { if chars are equal, no further test is necessary }
  343. beq .LStrliCompEqual
  344. { see stricomp for explanation }
  345. subfic r3,r0,64
  346. subfe r8,r8,r8
  347. subfic r3,r10,64
  348. subfe r5,r5,r5
  349. subic r3,r0,91
  350. andi. r8,r8,0x020
  351. subfe r7,r7,r7
  352. subic r3,r10,91
  353. andi. r5,r5,0x020
  354. subfe r11,r11,r11
  355. and r8,r8,r7
  356. and r5,r5,r11
  357. add r0,r0,r8
  358. add r10,r10,r5
  359. { compare again }
  360. sub. r3,r0,r10
  361. bne .LStrliCompDone
  362. .LStrliCompEqual:
  363. { if they are equal and one is zero, then the other one is zero too }
  364. { and we're done as well (r3 also contains 0 then) }
  365. { otherwise loop (if ctr <> 0) }
  366. bdnzf cr1*4+eq,.LStrliCompLoop
  367. .LStrliCompDone:
  368. end;
  369. {$endif FPC_UNIT_HAS_STRLICOMP}
  370. {$ifndef FPC_UNIT_HAS_STRSCAN}
  371. {$define FPC_UNIT_HAS_STRSCAN}
  372. function strscan(p : pchar;c : char) : pchar;assembler;
  373. asm
  374. { empty/invalid string? }
  375. cmplwi r3,0
  376. { if yes, do nothing }
  377. beq .LStrScanDone
  378. subi r3,r3,1
  379. .LStrScanLoop:
  380. lbzu r0,1(r3)
  381. cmplw cr1,r0,r4
  382. cmplwi r0,0
  383. beq cr1,.LStrScanDone
  384. bne .LStrScanLoop
  385. li r3, 0
  386. .LStrScanDone:
  387. end;
  388. {$endif FPC_UNIT_HAS_STRSCAN}
  389. {$ifndef FPC_UNIT_HAS_STRRSCAN}
  390. {$define FPC_UNIT_HAS_STRRSCAN}
  391. function strrscan(p : pchar;c : char) : pchar;assembler;
  392. asm
  393. { empty/invalid string? }
  394. cmplwi r3,0
  395. { if yes, do nothing }
  396. beq .LStrrScanDone
  397. { make r5 will be walking through the string }
  398. subi r5,r3,1
  399. { assume not found }
  400. li r3,0
  401. .LStrrScanLoop:
  402. lbzu r10,1(r5)
  403. cmplw cr1,r10,r4
  404. cmplwi cr0,r10,0
  405. bne+ cr1,.LStrrScanNotFound
  406. { store address of found position }
  407. mr r3,r5
  408. .LStrrScanNotFound:
  409. bne .LStrrScanLoop
  410. .LStrrScanDone:
  411. end;
  412. {$endif FPC_UNIT_HAS_STRRSCAN}
  413. {$ifndef FPC_UNIT_HAS_STRUPPER}
  414. {$define FPC_UNIT_HAS_STRUPPER}
  415. function strupper(p : pchar) : pchar;assembler;
  416. asm
  417. cmplwi r3,0
  418. beq .LStrUpperNil
  419. subi r9,r3,1
  420. .LStrUpperLoop:
  421. lbzu r10,1(r9)
  422. { a <= x <= b <=> cardinal(x-a) <= cardinal(b-a) }
  423. subi r0,r10,97
  424. cmplwi r0,122-97
  425. cmplwi cr1,r10,0
  426. subi r10,r10,0x20
  427. bgt .LStrUpper1
  428. stb r10,0(r9)
  429. .LStrUpper1:
  430. bne cr1,.LStrUpperLoop
  431. .LStrUpperNil:
  432. end;
  433. {$endif FPC_UNIT_HAS_STRUPPER}
  434. {$ifndef FPC_UNIT_HAS_STRLOWER}
  435. {$define FPC_UNIT_HAS_STRLOWER}
  436. function strlower(p : pchar) : pchar;assembler;
  437. asm
  438. cmplwi r3,0
  439. beq .LStrLowerNil
  440. subi r9,r3,1
  441. .LStrLowerLoop:
  442. lbzu r10,1(r9)
  443. { a <= x <= b <=> cardinal(x-a) <= cardinal(b-a) }
  444. subi r0,r10,65
  445. cmplwi r0,90-65
  446. cmplwi cr1,r10,0
  447. addi r10,r10,0x20
  448. bgt .LStrLower1
  449. stb r10,0(r9)
  450. .LStrLower1:
  451. bne cr1,.LStrLowerLoop
  452. .LStrLowerNil:
  453. end;
  454. {$endif FPC_UNIT_HAS_STRLOWER}