strings.inc 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568
  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. addi r8,r8,0xfeff
  52. { load constant 0x80808080}
  53. lis r7,0x8080
  54. addi 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. addi r8,r8,0xfeff
  123. { load constant 0x80808080}
  124. lis r7,0x8080
  125. addi 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. {
  133. li r8,-257 { 0x0feff }
  134. andis. r8,r8,0x0fefe
  135. li r9,-32640 { 0x08080 }
  136. andis. r9,r9,0x08080
  137. }
  138. .LStrECopyAlignedLoop:
  139. { load next 4 bytes }
  140. lwzu r0,4(r4)
  141. { test for zero byte }
  142. add r10,r0,r8
  143. andc r10,r10,r0
  144. and. r10,r10,r7
  145. bne .LStrECopyEndFound
  146. stwu r0,4(r3)
  147. b .LStrECopyAlignedLoop
  148. .LStrECopyEndFound:
  149. { adjust for possible $01 bytes coming before the terminating 0 byte }
  150. rlwinm r8,r0,7,0,31
  151. andc r10,r10,r8
  152. { result is either 0, 8, 16 or 24 depending on which byte is zero }
  153. cntlzw r10,r10
  154. addi r3,r3,3
  155. .LStrECopyWrapUpLoop:
  156. subic. r10,r10,8
  157. rlwinm r0,r0,8,0,31
  158. stbu r0,1(r3)
  159. bge .LStrECopyWrapUpLoop
  160. .LStrECopyDone:
  161. { r3 contains new dest here }
  162. end;
  163. {$endif FPC_UNIT_HAS_STRECOPY}
  164. {$ifndef FPC_UNIT_HAS_STRLCOPY}
  165. {$define FPC_UNIT_HAS_STRLCOPY}
  166. function strlcopy(dest,source : pchar;maxlen : longint) : pchar;assembler;
  167. { in: dest in r3, source in r4, maxlen in r5 }
  168. { out: result (dest) in r3 }
  169. asm
  170. { load the begin of the source string in the data cache }
  171. dcbt 0,r4
  172. mtctr r5
  173. subi r4,r4,1
  174. subi r10,r3,1
  175. .LStrlCopyLoop:
  176. lbzu r0,1(r4)
  177. cmplwi r0,0
  178. stbu r0,1(r10)
  179. bdnzf cr0*4+eq, .LStrlCopyLoop
  180. { if we stopped because we copied a #0, we're done }
  181. beq .LStrlCopyDone
  182. { otherwise add the #0 }
  183. li r0,0
  184. stb r0,1(r10)
  185. .LStrlCopyDone:
  186. end;
  187. {$endif FPC_UNIT_HAS_STRLCOPY}
  188. {$ifndef FPC_UNIT_HAS_STRLEN}
  189. {$define FPC_UNIT_HAS_STRLEN}
  190. function strlen(p : pchar) : longint;assembler;
  191. {$i strlen.inc}
  192. {$endif FPC_UNIT_HAS_STRLEN}
  193. {$ifndef FPC_UNIT_HAS_STREND}
  194. {$define FPC_UNIT_HAS_STREND}
  195. function strend(p : pchar) : pchar;assembler;
  196. { in: p in r3 }
  197. { out: result (end of p) in r3 }
  198. asm
  199. { load the begin of the string in the data cache }
  200. dcbt 0,r3
  201. { empty/invalid string? }
  202. cmplwi r3,0
  203. { if yes, do nothing }
  204. beq .LStrEndDone
  205. subi r3,r3,1
  206. .LStrEndLoop:
  207. lbzu r0,1(r3)
  208. cmplwi r0,0
  209. bne .LStrEndLoop
  210. .LStrEndDone:
  211. end;
  212. {$endif FPC_UNIT_HAS_STREND}
  213. {$ifndef FPC_UNIT_HAS_STRCOMP}
  214. {$define FPC_UNIT_HAS_STRCOMP}
  215. function strcomp(str1,str2 : pchar) : longint;assembler;
  216. { in: str1 in r3, str2 in r4 }
  217. { out: result (= 0 if strings equal, < 0 if str1 < str2, > 0 if str1 > str2 }
  218. { in r3 }
  219. asm
  220. { use r0 instead of r3 for str1 since r3 contains result }
  221. subi r9,r3,1
  222. subi r4,r4,1
  223. .LStrCompLoop:
  224. { load next chars }
  225. lbzu r0,1(r9)
  226. { check if one is zero }
  227. cmplwi cr1,r0,0
  228. lbzu r10,1(r4)
  229. { calculate difference }
  230. sub. r3,r0,r10
  231. { if chars not equal, we're ready }
  232. bne .LStrCompDone
  233. { if they are equal and one is zero, then the other one is zero too }
  234. { and we're done as well (r3 also contains 0 then) }
  235. { otherwise loop }
  236. bne cr1,.LStrCompLoop
  237. .LStrCompDone:
  238. end;
  239. {$endif FPC_UNIT_HAS_STRCOMP}
  240. {$ifndef FPC_UNIT_HAS_STRLCOMP}
  241. {$define FPC_UNIT_HAS_STRLCOMP}
  242. function strlcomp(str1,str2 : pchar;l : longint) : longint;assembler;
  243. { (same as strcomp, but maximally compare until l'th character) }
  244. { in: str1 in r3, str2 in r4, l in r5 }
  245. { out: result (= 0 if strings equal, < 0 if str1 < str2, > 0 if str1 > str2 }
  246. { in r3 }
  247. asm
  248. { load the begin of one of the strings in the data cache }
  249. dcbt 0,r3
  250. { use r0 instead of r3 for str1 since r3 contains result }
  251. cmplwi r5,0
  252. subi r9,r3,1
  253. li r3,0
  254. beq .LStrlCompDone
  255. mtctr r5
  256. subi r4,r4,1
  257. .LStrlCompLoop:
  258. { load next chars }
  259. lbzu r0,1(r9)
  260. { check if one is zero }
  261. cmplwi cr1,r0,0
  262. lbzu r10,1(r4)
  263. { calculate difference }
  264. sub. r3,r0,r10
  265. { if chars not equal, we're ready }
  266. bne .LStrlCompDone
  267. { if they are equal and one is zero, then the other one is zero too }
  268. { and we're done as well (r3 also contains 0 then) }
  269. { otherwise loop (if ctr <> 0) }
  270. bdnzf cr1*4+eq,.LStrlCompLoop
  271. .LStrlCompDone:
  272. end;
  273. {$endif FPC_UNIT_HAS_STRLCOMP}
  274. {$ifndef FPC_UNIT_HAS_STRICOMP}
  275. {$define FPC_UNIT_HAS_STRICOMP}
  276. function stricomp(str1,str2 : pchar) : longint;assembler;
  277. { in: str1 in r3, str2 in r4 }
  278. { out: result of case insensitive comparison (< 0, = 0, > 0) }
  279. asm
  280. { use r5 instead of r3 for str1 since r3 contains result }
  281. subi r5,r3,1
  282. subi r4,r4,1
  283. .LStriCompLoop:
  284. { load next chars }
  285. lbzu r6,1(r5)
  286. { check if one is zero }
  287. cmplwi cr1,r6,0
  288. lbzu r7,1(r4)
  289. { calculate difference }
  290. sub. r3,r6,r7
  291. { if chars are equal, no further test is necessary }
  292. beq+ .LStriCompEqual
  293. { make both lowercase, no branches }
  294. { r3 := pred('A') - r6 }
  295. subfic r3,r6,64
  296. { if r6 < 'A' then r8 := 0 else r8 := $ffffffff }
  297. subfe r8,r8,r8
  298. { same for r7 }
  299. subfic r3,r7,64
  300. subfe r9,r9,r9
  301. { r3 := r6 - succ('Z') }
  302. subic r3,r6,91
  303. { if r6 < 'A' then r8 := 0 else r8 := $20 }
  304. andi. r8,r8,0x020
  305. { if r6 > Z then r10 := 0 else r10 := $ffffffff }
  306. subfe r10,r10,r10
  307. { same for r7 }
  308. subic r3,r7,91
  309. andi. r9,r9,0x020
  310. subfe r11,r11,r11
  311. { if (r6 in ['A'..'Z'] then r8 := $20 else r8 := 0 }
  312. and r8,r8,r10
  313. { same for r7 }
  314. and r9,r9,r11
  315. { make lowercase }
  316. add r6,r6,r8
  317. { same for r7 }
  318. add r7,r7,r9
  319. { compare again }
  320. sub. r3,r6,r7
  321. bne- .LStriCompDone
  322. .LStriCompEqual:
  323. { if they are equal and one is zero, then the other one is zero too }
  324. { and we're done as well (r3 also contains 0 then) }
  325. { otherwise loop }
  326. bne cr1,.LStriCompLoop
  327. .LStriCompDone:
  328. end;
  329. {$endif FPC_UNIT_HAS_STRICOMP}
  330. {$ifndef FPC_UNIT_HAS_STRLICOMP}
  331. {$define FPC_UNIT_HAS_STRLICOMP}
  332. function strlicomp(str1,str2 : pchar;l : longint) : longint;assembler;
  333. { (same as stricomp, but maximally compare until l'th character) }
  334. { in: str1 in r3, str2 in r4, l in r5 }
  335. { out: result of case insensitive comparison (< 0, = 0, > 0) }
  336. asm
  337. { load the begin of one of the string in the data cache }
  338. dcbt 0,r3
  339. { use r0 instead of r3 for str1 since r3 contains result }
  340. cmplwi r5,0
  341. subi r9,r3,1
  342. li r3,0
  343. beq- .LStrliCompDone
  344. mtctr r5
  345. subi r4,r4,1
  346. .LStrliCompLoop:
  347. { load next chars }
  348. lbzu r0,1(r9)
  349. { check if one is zero }
  350. cmplwi cr1,r0,0
  351. lbzu r10,1(r4)
  352. { calculate difference }
  353. sub. r3,r0,r10
  354. { if chars are equal, no further test is necessary }
  355. beq .LStrliCompEqual
  356. { see stricomp for explanation }
  357. subfic r3,r0,64
  358. subfe r8,r8,r8
  359. subfic r3,r10,64
  360. subfe r5,r5,r5
  361. subic r3,r0,91
  362. andi. r8,r8,0x020
  363. subfe r7,r7,r7
  364. subic r3,r10,91
  365. andi. r5,r5,0x020
  366. subfe r11,r11,r11
  367. and r8,r8,r7
  368. and r5,r5,r11
  369. add r0,r0,r8
  370. add r10,r10,r5
  371. { compare again }
  372. sub. r3,r0,r10
  373. bne .LStrliCompDone
  374. .LStrliCompEqual:
  375. { if they are equal and one is zero, then the other one is zero too }
  376. { and we're done as well (r3 also contains 0 then) }
  377. { otherwise loop (if ctr <> 0) }
  378. bdnzf cr1*4+eq,.LStrliCompLoop
  379. .LStrliCompDone:
  380. end;
  381. {$endif FPC_UNIT_HAS_STRLICOMP}
  382. {$ifndef FPC_UNIT_HAS_STRSCAN}
  383. {$define FPC_UNIT_HAS_STRSCAN}
  384. function strscan(p : pchar;c : char) : pchar;assembler;
  385. asm
  386. { empty/invalid string? }
  387. cmplwi r3,0
  388. { if yes, do nothing }
  389. beq .LStrScanDone
  390. subi r3,r3,1
  391. .LStrScanLoop:
  392. lbzu r0,1(r3)
  393. cmplw cr1,r0,r4
  394. cmplwi r0,0
  395. beq cr1,.LStrScanDone
  396. bne .LStrScanLoop
  397. li r3, 0
  398. .LStrScanDone:
  399. end;
  400. {$endif FPC_UNIT_HAS_STRSCAN}
  401. {$ifndef FPC_UNIT_HAS_STRRSCAN}
  402. {$define FPC_UNIT_HAS_STRRSCAN}
  403. function strrscan(p : pchar;c : char) : pchar;assembler;
  404. asm
  405. { empty/invalid string? }
  406. cmplwi r3,0
  407. { if yes, do nothing }
  408. beq .LStrrScanDone
  409. { make r5 will be walking through the string }
  410. subi r5,r3,1
  411. { assume not found }
  412. li r3,0
  413. .LStrrScanLoop:
  414. lbzu r10,1(r5)
  415. cmplw cr1,r10,r4
  416. cmplwi cr0,r10,0
  417. bne+ cr1,.LStrrScanNotFound
  418. { store address of found position }
  419. mr r3,r5
  420. .LStrrScanNotFound:
  421. bne .LStrrScanLoop
  422. .LStrrScanDone:
  423. end;
  424. {$endif FPC_UNIT_HAS_STRRSCAN}
  425. {$ifndef FPC_UNIT_HAS_STRUPPER}
  426. {$define FPC_UNIT_HAS_STRUPPER}
  427. function strupper(p : pchar) : pchar;assembler;
  428. asm
  429. cmplwi r3,0
  430. beq .LStrUpperNil
  431. subi r9,r3,1
  432. .LStrUpperLoop:
  433. lbzu r10,1(r9)
  434. { a <= x <= b <=> cardinal(x-a) <= cardinal(b-a) }
  435. subi r0,r10,97
  436. cmplwi r0,122-97
  437. cmplwi cr1,r10,0
  438. subi r10,r10,0x20
  439. bgt .LStrUpper1
  440. stb r10,0(r9)
  441. .LStrUpper1:
  442. bne cr1,.LStrUpperLoop
  443. .LStrUpperNil:
  444. end;
  445. {$endif FPC_UNIT_HAS_STRUPPER}
  446. {$ifndef FPC_UNIT_HAS_STRLOWER}
  447. {$define FPC_UNIT_HAS_STRLOWER}
  448. function strlower(p : pchar) : pchar;assembler;
  449. asm
  450. cmplwi r3,0
  451. beq .LStrLowerNil
  452. subi r9,r3,1
  453. .LStrLowerLoop:
  454. lbzu r10,1(r9)
  455. { a <= x <= b <=> cardinal(x-a) <= cardinal(b-a) }
  456. subi r0,r10,65
  457. cmplwi r0,90-65
  458. cmplwi cr1,r10,0
  459. addi r10,r10,0x20
  460. bgt .LStrLower1
  461. stb r10,0(r9)
  462. .LStrLower1:
  463. bne cr1,.LStrLowerLoop
  464. .LStrLowerNil:
  465. end;
  466. {$endif FPC_UNIT_HAS_STRLOWER}
  467. {
  468. $Log$
  469. Revision 1.26 2004-08-17 13:34:15 olle
  470. * bugfix
  471. Revision 1.25 2004/08/09 16:43:33 jonas
  472. * fixed stricomp() and strlicomp()
  473. Revision 1.24 2004/05/01 17:02:37 jonas
  474. * use some more string routines from libc if FPC_USE_LIBC is used
  475. Revision 1.23 2003/12/28 22:33:35 florian
  476. * strscan fix from Jonas
  477. Revision 1.22 2003/11/29 16:27:19 jonas
  478. * fixed several ppc assembler reader related problems
  479. * local vars in assembler procedures now start at offset 4
  480. * fixed second_int_to_bool (apparently an integer can be in LOC_JUMP??)
  481. Revision 1.21 2003/08/24 20:51:27 olle
  482. + added MacOS compatible constant loading
  483. Revision 1.20 2003/07/07 20:23:46 peter
  484. * added defines to override generic implementations
  485. Revision 1.19 2003/06/14 12:41:08 jonas
  486. * fixed compilation problems (removed unnecessary modified registers
  487. lists from procedures)
  488. Revision 1.18 2003/05/28 19:18:10 jonas
  489. * fixed strcopy and strecopy if there are #1 chars right before the end
  490. of the string to copied
  491. Revision 1.17 2003/05/24 10:16:24 jonas
  492. * fixed strscan and strrscan
  493. Revision 1.16 2003/05/17 12:55:30 florian
  494. * fixed copy&paste bug in strecopy
  495. Revision 1.15 2003/05/17 00:01:13 jonas
  496. * fixed strcopy
  497. Revision 1.14 2002/09/11 07:49:40 jonas
  498. * fixed assembler errors
  499. Revision 1.13 2002/09/07 16:01:26 peter
  500. * old logs removed and tabs fixed
  501. Revision 1.12 2002/09/06 16:58:43 jonas
  502. * fixed wrong references (used r0 as base register)
  503. Revision 1.11 2002/08/10 17:14:36 jonas
  504. * various fixes, mostly changing the names of the modifies registers to
  505. upper case since that seems to be required by the compiler
  506. }