strings.inc 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508
  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. {$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. {$ifdef macos}
  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. {$ifdef macos}
  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. {$ifndef FPC_UNIT_HAS_STRLCOPY}
  159. {$define FPC_UNIT_HAS_STRLCOPY}
  160. function strlcopy(dest,source : pchar;maxlen : longint) : pchar;assembler;
  161. { in: dest in r3, source in r4, maxlen in r5 }
  162. { out: result (dest) in r3 }
  163. asm
  164. { load the begin of the source string in the data cache }
  165. dcbt 0,r4
  166. mtctr r5
  167. subi r4,r4,1
  168. subi r10,r3,1
  169. .LStrlCopyLoop:
  170. lbzu r0,1(r4)
  171. cmplwi r0,0
  172. stbu r0,1(r10)
  173. bdnzf cr0*4+eq, .LStrlCopyLoop
  174. { if we stopped because we copied a #0, we're done }
  175. beq .LStrlCopyDone
  176. { otherwise add the #0 }
  177. li r0,0
  178. stb r0,1(r10)
  179. .LStrlCopyDone:
  180. end;
  181. {$endif FPC_UNIT_HAS_STRLCOPY}
  182. {$ifndef FPC_UNIT_HAS_STREND}
  183. {$define FPC_UNIT_HAS_STREND}
  184. function strend(p : pchar) : pchar;assembler;
  185. { in: p in r3 }
  186. { out: result (end of p) in r3 }
  187. asm
  188. { load the begin of the string in the data cache }
  189. dcbt 0,r3
  190. { empty/invalid string? }
  191. cmplwi r3,0
  192. { if yes, do nothing }
  193. beq .LStrEndDone
  194. subi r3,r3,1
  195. .LStrEndLoop:
  196. lbzu r0,1(r3)
  197. cmplwi r0,0
  198. bne .LStrEndLoop
  199. .LStrEndDone:
  200. end;
  201. {$endif FPC_UNIT_HAS_STREND}
  202. {$ifndef FPC_UNIT_HAS_STRCOMP}
  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. {$endif FPC_UNIT_HAS_STRCOMP}
  229. {$ifndef FPC_UNIT_HAS_STRLCOMP}
  230. {$define FPC_UNIT_HAS_STRLCOMP}
  231. function strlcomp(str1,str2 : pchar;l : longint) : longint;assembler;
  232. { (same as strcomp, but maximally compare until l'th character) }
  233. { in: str1 in r3, str2 in r4, l in r5 }
  234. { out: result (= 0 if strings equal, < 0 if str1 < str2, > 0 if str1 > str2 }
  235. { in r3 }
  236. asm
  237. { load the begin of one of the strings in the data cache }
  238. dcbt 0,r3
  239. { use r0 instead of r3 for str1 since r3 contains result }
  240. cmplwi r5,0
  241. subi r9,r3,1
  242. li r3,0
  243. beq .LStrlCompDone
  244. mtctr r5
  245. subi r4,r4,1
  246. .LStrlCompLoop:
  247. { load next chars }
  248. lbzu r0,1(r9)
  249. { check if one is zero }
  250. cmplwi cr1,r0,0
  251. lbzu r10,1(r4)
  252. { calculate difference }
  253. sub. r3,r0,r10
  254. { if chars not equal, we're ready }
  255. bne .LStrlCompDone
  256. { if they are equal and one is zero, then the other one is zero too }
  257. { and we're done as well (r3 also contains 0 then) }
  258. { otherwise loop (if ctr <> 0) }
  259. bdnzf cr1*4+eq,.LStrlCompLoop
  260. .LStrlCompDone:
  261. end;
  262. {$endif FPC_UNIT_HAS_STRLCOMP}
  263. {$ifndef FPC_UNIT_HAS_STRICOMP}
  264. {$define FPC_UNIT_HAS_STRICOMP}
  265. function stricomp(str1,str2 : pchar) : longint;assembler;
  266. { in: str1 in r3, str2 in r4 }
  267. { out: result of case insensitive comparison (< 0, = 0, > 0) }
  268. asm
  269. { use r5 instead of r3 for str1 since r3 contains result }
  270. subi r5,r3,1
  271. subi r4,r4,1
  272. .LStriCompLoop:
  273. { load next chars }
  274. lbzu r6,1(r5)
  275. { check if one is zero }
  276. cmplwi cr1,r6,0
  277. lbzu r7,1(r4)
  278. { calculate difference }
  279. sub. r3,r6,r7
  280. { if chars are equal, no further test is necessary }
  281. beq+ .LStriCompEqual
  282. { make both lowercase, no branches }
  283. { r3 := pred('A') - r6 }
  284. subfic r3,r6,64
  285. { if r6 < 'A' then r8 := 0 else r8 := $ffffffff }
  286. subfe r8,r8,r8
  287. { same for r7 }
  288. subfic r3,r7,64
  289. subfe r9,r9,r9
  290. { r3 := r6 - succ('Z') }
  291. subic r3,r6,91
  292. { if r6 < 'A' then r8 := 0 else r8 := $20 }
  293. andi. r8,r8,0x020
  294. { if r6 > Z then r10 := 0 else r10 := $ffffffff }
  295. subfe r10,r10,r10
  296. { same for r7 }
  297. subic r3,r7,91
  298. andi. r9,r9,0x020
  299. subfe r11,r11,r11
  300. { if (r6 in ['A'..'Z'] then r8 := $20 else r8 := 0 }
  301. and r8,r8,r10
  302. { same for r7 }
  303. and r9,r9,r11
  304. { make lowercase }
  305. add r6,r6,r8
  306. { same for r7 }
  307. add r7,r7,r9
  308. { compare again }
  309. sub. r3,r6,r7
  310. bne- .LStriCompDone
  311. .LStriCompEqual:
  312. { if they are equal and one is zero, then the other one is zero too }
  313. { and we're done as well (r3 also contains 0 then) }
  314. { otherwise loop }
  315. bne cr1,.LStriCompLoop
  316. .LStriCompDone:
  317. end;
  318. {$endif FPC_UNIT_HAS_STRICOMP}
  319. {$ifndef FPC_UNIT_HAS_STRLICOMP}
  320. {$define FPC_UNIT_HAS_STRLICOMP}
  321. function strlicomp(str1,str2 : pchar;l : longint) : longint;assembler;
  322. { (same as stricomp, but maximally compare until l'th character) }
  323. { in: str1 in r3, str2 in r4, l in r5 }
  324. { out: result of case insensitive comparison (< 0, = 0, > 0) }
  325. asm
  326. { load the begin of one of the string in the data cache }
  327. dcbt 0,r3
  328. { use r0 instead of r3 for str1 since r3 contains result }
  329. cmplwi r5,0
  330. subi r9,r3,1
  331. li r3,0
  332. beq- .LStrliCompDone
  333. mtctr r5
  334. subi r4,r4,1
  335. .LStrliCompLoop:
  336. { load next chars }
  337. lbzu r0,1(r9)
  338. { check if one is zero }
  339. cmplwi cr1,r0,0
  340. lbzu r10,1(r4)
  341. { calculate difference }
  342. sub. r3,r0,r10
  343. { if chars are equal, no further test is necessary }
  344. beq .LStrliCompEqual
  345. { see stricomp for explanation }
  346. subfic r3,r0,64
  347. subfe r8,r8,r8
  348. subfic r3,r10,64
  349. subfe r5,r5,r5
  350. subic r3,r0,91
  351. andi. r8,r8,0x020
  352. subfe r7,r7,r7
  353. subic r3,r10,91
  354. andi. r5,r5,0x020
  355. subfe r11,r11,r11
  356. and r8,r8,r7
  357. and r5,r5,r11
  358. add r0,r0,r8
  359. add r10,r10,r5
  360. { compare again }
  361. sub. r3,r0,r10
  362. bne .LStrliCompDone
  363. .LStrliCompEqual:
  364. { if they are equal and one is zero, then the other one is zero too }
  365. { and we're done as well (r3 also contains 0 then) }
  366. { otherwise loop (if ctr <> 0) }
  367. bdnzf cr1*4+eq,.LStrliCompLoop
  368. .LStrliCompDone:
  369. end;
  370. {$endif FPC_UNIT_HAS_STRLICOMP}
  371. {$ifndef FPC_UNIT_HAS_STRSCAN}
  372. {$define FPC_UNIT_HAS_STRSCAN}
  373. function strscan(p : pchar;c : char) : pchar;assembler;
  374. asm
  375. { empty/invalid string? }
  376. cmplwi r3,0
  377. { if yes, do nothing }
  378. beq .LStrScanDone
  379. subi r3,r3,1
  380. .LStrScanLoop:
  381. lbzu r0,1(r3)
  382. cmplw cr1,r0,r4
  383. cmplwi r0,0
  384. beq cr1,.LStrScanDone
  385. bne .LStrScanLoop
  386. li r3, 0
  387. .LStrScanDone:
  388. end;
  389. {$endif FPC_UNIT_HAS_STRSCAN}
  390. {$ifndef FPC_UNIT_HAS_STRRSCAN}
  391. {$define FPC_UNIT_HAS_STRRSCAN}
  392. function strrscan(p : pchar;c : char) : pchar;assembler;
  393. asm
  394. { empty/invalid string? }
  395. cmplwi r3,0
  396. { if yes, do nothing }
  397. beq .LStrrScanDone
  398. { make r5 will be walking through the string }
  399. subi r5,r3,1
  400. { assume not found }
  401. li r3,0
  402. .LStrrScanLoop:
  403. lbzu r10,1(r5)
  404. cmplw cr1,r10,r4
  405. cmplwi cr0,r10,0
  406. bne+ cr1,.LStrrScanNotFound
  407. { store address of found position }
  408. mr r3,r5
  409. .LStrrScanNotFound:
  410. bne .LStrrScanLoop
  411. .LStrrScanDone:
  412. end;
  413. {$endif FPC_UNIT_HAS_STRRSCAN}
  414. {$ifndef FPC_UNIT_HAS_STRUPPER}
  415. {$define FPC_UNIT_HAS_STRUPPER}
  416. function strupper(p : pchar) : pchar;assembler;
  417. asm
  418. cmplwi r3,0
  419. beq .LStrUpperNil
  420. subi r9,r3,1
  421. .LStrUpperLoop:
  422. lbzu r10,1(r9)
  423. { a <= x <= b <=> cardinal(x-a) <= cardinal(b-a) }
  424. subi r0,r10,97
  425. cmplwi r0,122-97
  426. cmplwi cr1,r10,0
  427. subi r10,r10,0x20
  428. bgt .LStrUpper1
  429. stb r10,0(r9)
  430. .LStrUpper1:
  431. bne cr1,.LStrUpperLoop
  432. .LStrUpperNil:
  433. end;
  434. {$endif FPC_UNIT_HAS_STRUPPER}
  435. {$ifndef FPC_UNIT_HAS_STRLOWER}
  436. {$define FPC_UNIT_HAS_STRLOWER}
  437. function strlower(p : pchar) : pchar;assembler;
  438. asm
  439. cmplwi r3,0
  440. beq .LStrLowerNil
  441. subi r9,r3,1
  442. .LStrLowerLoop:
  443. lbzu r10,1(r9)
  444. { a <= x <= b <=> cardinal(x-a) <= cardinal(b-a) }
  445. subi r0,r10,65
  446. cmplwi r0,90-65
  447. cmplwi cr1,r10,0
  448. addi r10,r10,0x20
  449. bgt .LStrLower1
  450. stb r10,0(r9)
  451. .LStrLower1:
  452. bne cr1,.LStrLowerLoop
  453. .LStrLowerNil:
  454. end;
  455. {$endif FPC_UNIT_HAS_STRLOWER}
  456. {
  457. $Log$
  458. Revision 1.29 2005-04-28 18:22:34 olle
  459. * Fixed loding of magic constant for macos
  460. Revision 1.28 2005/02/14 17:13:31 peter
  461. * truncate log
  462. }