strings.inc 17 KB

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