strings.inc 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566
  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 r28 instead of r3 for str1 since r3 contains result }
  281. subi r28,r3,1
  282. subi r4,r4,1
  283. .LStriCompLoop:
  284. { load next chars }
  285. lbzu r29,1(r28)
  286. { check if one is zero }
  287. cmplwi cr1,r29,0
  288. lbzu r30,1(r4)
  289. { calculate difference }
  290. sub. r3,r29,r30
  291. { if chars are equal, no further test is necessary }
  292. beq+ .LStriCompEqual
  293. { make both lowercase, no branches }
  294. li r27,0
  295. li r25,0
  296. { r3 := r29 - 'A' }
  297. subic r3,r29,65
  298. { if r29 < 'A' then r27 := 0 else r27 := $ffffffff }
  299. addme r27,r27
  300. { same for r30 }
  301. subic r3,r30,65
  302. addme r25,r25
  303. { r3 := 'Z' - r29 }
  304. subfic r3,r29,90
  305. { if r29 < 'A' then r27 := 0 else r27 := $20 }
  306. andi. r27,r27,0x020
  307. { if r29 > Z then r26 := 0 else r26 := $ffffffff }
  308. subfe r26,r26,r26
  309. { same for r30 }
  310. subfic r3,r30,90
  311. andi. r25,r25,0x020
  312. subfe r24,r24,r24
  313. { if (r29 in ['A'..'Z'] then r27 := $20 else r27 := 0 }
  314. and r27,r27,r26
  315. { same for r30 }
  316. and r25,r25,r24
  317. { make lowercase }
  318. add r29,r29,r27
  319. { same for r30 }
  320. add r30,r30,r25
  321. { compare again }
  322. sub. r3,r29,r30
  323. bne .LStriCompDone
  324. .LStriCompEqual:
  325. { if they are equal and one is zero, then the other one is zero too }
  326. { and we're done as well (r3 also contains 0 then) }
  327. { otherwise loop }
  328. bne cr1,.LStriCompLoop
  329. .LStriCompDone:
  330. end;
  331. {$endif FPC_UNIT_HAS_STRICOMP}
  332. {$ifndef FPC_UNIT_HAS_STRLICOMP}
  333. {$define FPC_UNIT_HAS_STRLICOMP}
  334. function strlicomp(str1,str2 : pchar;l : longint) : longint;assembler;
  335. { (same as stricomp, but maximally compare until l'th character) }
  336. { in: str1 in r3, str2 in r4, l in r5 }
  337. { out: result of case insensitive comparison (< 0, = 0, > 0) }
  338. asm
  339. { load the begin of one of the string in the data cache }
  340. dcbt 0,r3
  341. { use r0 instead of r3 for str1 since r3 contains result }
  342. cmplwi r5,0
  343. subi r9,r3,1
  344. li r3,0
  345. beq- .LStrliCompDone
  346. mtctr r5
  347. subi r4,r4,1
  348. .LStrliCompLoop:
  349. { load next chars }
  350. lbzu r0,1(r9)
  351. { check if one is zero }
  352. cmplwi cr1,r0,0
  353. lbzu r10,1(r4)
  354. { calculate difference }
  355. sub. r3,r0,r10
  356. { if chars are equal, no further test is necessary }
  357. beq .LStrliCompEqual
  358. { see stricomp for explanation }
  359. li r8,0
  360. li r5,0
  361. subic r3,r0,65
  362. addme r8,r8
  363. subic r3,r10,65
  364. addme r5,r5
  365. subfic r3,r0,90
  366. andi. r8,r8,0x020
  367. subfe r7,r7,r7
  368. subfic r3,r10,90
  369. andi. r5,r5,0x020
  370. subfe r24,r24,r24
  371. and r8,r8,r7
  372. and r5,r5,r24
  373. add r0,r0,r8
  374. add r10,r10,r5
  375. { compare again }
  376. sub. r3,r0,r10
  377. bne .LStrliCompDone
  378. .LStrliCompEqual:
  379. { if they are equal and one is zero, then the other one is zero too }
  380. { and we're done as well (r3 also contains 0 then) }
  381. { otherwise loop (if ctr <> 0) }
  382. bdnzf cr1*4+eq,.LStrliCompLoop
  383. .LStrliCompDone:
  384. end;
  385. {$endif FPC_UNIT_HAS_STRLICOMP}
  386. {$ifndef FPC_UNIT_HAS_STRSCAN}
  387. {$define FPC_UNIT_HAS_STRSCAN}
  388. function strscan(p : pchar;c : char) : pchar;assembler;
  389. asm
  390. { empty/invalid string? }
  391. cmplwi r3,0
  392. { if yes, do nothing }
  393. beq .LStrScanDone
  394. subi r3,r3,1
  395. .LStrScanLoop:
  396. lbzu r0,1(r3)
  397. cmplw cr1,r0,r4
  398. cmplwi r0,0
  399. beq cr1,.LStrScanDone
  400. bne .LStrScanLoop
  401. li r3, 0
  402. .LStrScanDone:
  403. end;
  404. {$endif FPC_UNIT_HAS_STRSCAN}
  405. {$ifndef FPC_UNIT_HAS_STRRSCAN}
  406. {$define FPC_UNIT_HAS_STRRSCAN}
  407. function strrscan(p : pchar;c : char) : pchar;assembler;
  408. asm
  409. { empty/invalid string? }
  410. cmplwi r3,0
  411. { if yes, do nothing }
  412. beq .LStrrScanDone
  413. { make r5 will be walking through the string }
  414. subi r5,r3,1
  415. { assume not found }
  416. li r3,0
  417. .LStrrScanLoop:
  418. lbzu r10,1(r5)
  419. cmplw cr1,r10,r4
  420. cmplwi cr0,r10,0
  421. bne+ cr1,.LStrrScanNotFound
  422. { store address of found position }
  423. mr r3,r5
  424. .LStrrScanNotFound:
  425. bne .LStrrScanLoop
  426. .LStrrScanDone:
  427. end;
  428. {$endif FPC_UNIT_HAS_STRRSCAN}
  429. {$ifndef FPC_UNIT_HAS_STRUPPER}
  430. {$define FPC_UNIT_HAS_STRUPPER}
  431. function strupper(p : pchar) : pchar;assembler;
  432. asm
  433. cmplwi r3,0
  434. beq .LStrUpperNil
  435. subi r9,r3,1
  436. .LStrUpperLoop:
  437. lbzu r10,1(r9)
  438. { a <= x <= b <=> cardinal(x-a) <= cardinal(b-a) }
  439. subi r0,r10,97
  440. cmplwi r0,122-97
  441. cmplwi cr1,r10,0
  442. subi r10,r10,0x20
  443. bgt .LStrUpper1
  444. stb r10,0(r9)
  445. .LStrUpper1:
  446. bne cr1,.LStrUpperLoop
  447. .LStrUpperNil:
  448. end;
  449. {$endif FPC_UNIT_HAS_STRUPPER}
  450. {$ifndef FPC_UNIT_HAS_STRLOWER}
  451. {$define FPC_UNIT_HAS_STRLOWER}
  452. function strlower(p : pchar) : pchar;assembler;
  453. asm
  454. cmplwi r3,0
  455. beq .LStrLowerNil
  456. subi r9,r3,1
  457. .LStrLowerLoop:
  458. lbzu r10,1(r9)
  459. { a <= x <= b <=> cardinal(x-a) <= cardinal(b-a) }
  460. subi r0,r10,65
  461. cmplwi r0,90-65
  462. cmplwi cr1,r10,0
  463. addi r10,r10,0x20
  464. bgt .LStrLower1
  465. stb r10,0(r9)
  466. .LStrLower1:
  467. bne cr1,.LStrLowerLoop
  468. .LStrLowerNil:
  469. end;
  470. {$endif FPC_UNIT_HAS_STRLOWER}
  471. {
  472. $Log$
  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. }