strings.inc 15 KB

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