strings.inc 15 KB

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