strings.inc 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495
  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. function strcopy(dest,source : pchar) : pchar;assembler;
  16. { in: dest in r3, source in r4 }
  17. { out: result (dest) in r3 }
  18. asm
  19. { in: dest in r3, source in r4 }
  20. { out: result (dest) in r3 }
  21. { load the begin of the source string in the data cache }
  22. dcbt 0,r4
  23. { get # of misaligned bytes }
  24. rlwinm. r10,r4,0,31-2+1,31
  25. subfic r10,r10,4
  26. mtctr r10
  27. { since we have to return dest intact, use another register for }
  28. { dest in the copy loop }
  29. subi r9,r3,1
  30. subi r4,r4,1
  31. beq LStrCopyAligned
  32. LStrCopyAlignLoop:
  33. { load next byte }
  34. lbzu r0,1(r4)
  35. { end of string? }
  36. cmpli cr0,r0,0
  37. { store byte }
  38. stbu r0,1(r9)
  39. { loop if misaligned bytes left and not end of string found }
  40. bdnzf eq,LStrCopyAlignLoop
  41. beq LStrCopyDone
  42. LStrCopyAligned:
  43. subi r4,r4,3
  44. subi r9,r9,3
  45. { setup magic constants }
  46. li r8,0x0feff
  47. addis r8,r8,0x0feff
  48. li r7,0x08080
  49. addis r7,r7,0x08081
  50. { load first 4 bytes }
  51. lwzu r0,4(r4)
  52. LStrCopyAlignedLoop:
  53. { test for zero byte }
  54. add r10,r0,r8
  55. andc r10,r10,r0
  56. and. r10,r10,r7
  57. bne LStrCopyEndFound
  58. stwu r0,4(r9)
  59. { load next 4 bytes (do it here so the load can begin while the }
  60. { the branch is processed) }
  61. lwzu r0,4(r4)
  62. b LStrCopyAlignedLoop
  63. LStrCopyEndFound:
  64. { result is either 0, 8, 16 or 24 depending on which byte is zero }
  65. cntlzw r10,r10
  66. addi r9,r9,3
  67. LStrCopyWrapUpLoop:
  68. subi r10,r10,8
  69. rlwinm r0,r0,8,0,31
  70. stbu r0,1(r9)
  71. bge LStrCopyWrapUpLoop
  72. LStrCopyDone:
  73. { r3 still contains dest here }
  74. end ['r4','r7','r8','r0','r9','r10','cr0','ctr'];
  75. function strecopy(dest,source : pchar) : pchar;assembler;
  76. { in: dest in r3, source in r4 }
  77. { out: result (end of new dest) in r3 }
  78. asm
  79. { load the begin of the source string in the data cache }
  80. dcbt 0,r4
  81. { get # of misaligned bytes }
  82. rlwinm. r10,r4,0,31-2+1,31
  83. subfic r10,r10,4
  84. mtctr r10
  85. subi r3,r3,1
  86. subi r4,r4,1
  87. beq LStrCopyAligned
  88. LStrCopyAlignLoop:
  89. { load next byte }
  90. lbzu r0,1(r4)
  91. { end of string? }
  92. cmpli cr0,r0,0
  93. { store byte }
  94. stbu r0,1(r3)
  95. { loop if misaligned bytes left and not end of string found }
  96. bdnzf eq,LStrCopyAlignLoop
  97. beq LStrCopyDone
  98. LStrCopyAligned:
  99. subi r4,r4,3
  100. subi r3,r3,3
  101. { setup magic constants }
  102. li r8,0x0feff
  103. addis r8,r8,0x0feff
  104. li r9,0x08080
  105. addis r9,r9,0x08081
  106. LStrCopyAlignedLoop:
  107. { load next 4 bytes }
  108. lwzu r0,4(r4)
  109. { test for zero byte }
  110. add r10,r0,r8
  111. andc r10,r10,r0
  112. and. r10,r10,r9
  113. bne LStrCopyEndFound
  114. stwu r0,4(r3)
  115. b LStrCopyAlignedLoop
  116. LStrCopyEndFound:
  117. { result is either 0, 8, 16 or 24 depending on which byte is zero }
  118. cntlzw r10,r10
  119. addi r3,r3,3
  120. LStrCopyWrapUpLoop:
  121. subic. r10,r10,8
  122. rlwinm r0,r0,8,0,31
  123. stbu r0,1(r3)
  124. bge LStrCopyWrapUpLoop
  125. LStrCopyDone:
  126. { r3 contains new dest here }
  127. end ['r3','r4','r8','r0','r3','r10','cr0','ctr'];
  128. function strlcopy(dest,source : pchar;maxlen : longint) : pchar;assembler;
  129. { in: dest in r3, source in r4, maxlen in r5 }
  130. { out: result (dest) in r3 }
  131. asm
  132. { load the begin of the source string in the data cache }
  133. dcbt 0,r4
  134. mtctr r5
  135. subi r4,r4,1
  136. subi r0,r3,1
  137. LStrlCopyLoop:
  138. lbzu r10,1(r4)
  139. cmpli r10,0
  140. stbu r10,1(r0)
  141. bdnzf cr0*4+eq, LStrlCopyLoop
  142. { if we stopped because we copied a #0, we're done }
  143. beq LStrlCopyDone
  144. { otherwise add the #0 }
  145. li r10,0
  146. stb r10,1(r0)
  147. LStrlCopyDone:
  148. end ['r0','r4','r30','cr0'];
  149. function strlen(p : pchar) : longint;assembler;
  150. {$i strlen.inc}
  151. function strend(p : pchar) : pchar;assembler;
  152. { in: p in r3 }
  153. { out: result (end of p) in r3 }
  154. asm
  155. { load the begin of the string in the data cache }
  156. dcbt 0,r3
  157. { empty/invalid string? }
  158. cmpli r3,0
  159. { if yes, do nothing }
  160. beq LStrEndDone
  161. subi r3,r3,1
  162. LStrEndLoop:
  163. lbzu r0,1(r3)
  164. cmpli r0,0
  165. bne LStrEndLoop
  166. LStrEndDone:
  167. end ['r0','r3','r4','cr0'];
  168. function strcomp(str1,str2 : pchar) : longint;assembler;
  169. { in: str1 in r3, str2 in r4 }
  170. { out: result (= 0 if strings equal, < 0 if str1 < str2, > 0 if str1 > str2 }
  171. { in r3 }
  172. asm
  173. { use r0 instead of r3 for str1 since r3 contains result }
  174. subi r0,r3,1
  175. subi r4,r4,1
  176. LStrCompLoop:
  177. { load next chars }
  178. lbzu r9,1(r0)
  179. { check if one is zero }
  180. cmpli cr1,r9,0
  181. lbzu r10,1(r4)
  182. { calculate difference }
  183. sub. r3,r9,r10
  184. { if chars not equal, we're ready }
  185. bne LStrCompDone
  186. { if they are equal and one is zero, then the other one is zero too }
  187. { and we're done as well (r3 also contains 0 then) }
  188. { otherwise loop }
  189. bne cr1,LStrCompLoop
  190. LStrCompDone:
  191. end ['r0','r3','r4','r9','r10','cr0','cr1'];
  192. function strlcomp(str1,str2 : pchar;l : longint) : longint;assembler;
  193. { (same as strcomp, but maximally compare until l'th character) }
  194. { in: str1 in r3, str2 in r4, l in r5 }
  195. { out: result (= 0 if strings equal, < 0 if str1 < str2, > 0 if str1 > str2 }
  196. { in r3 }
  197. asm
  198. { load the begin of one of the strings in the data cache }
  199. dcbt 0,r3
  200. { use r0 instead of r3 for str1 since r3 contains result }
  201. cmpl r5,0
  202. subi r0,r3,1
  203. li r3,0
  204. beq LStrlCompDone
  205. mtctr r5
  206. subi r4,r4,1
  207. LStrlCompLoop:
  208. { load next chars }
  209. lbzu r9,1(r0)
  210. { check if one is zero }
  211. cmpli cr1,r9,0
  212. lbzu r10,1(r4)
  213. { calculate difference }
  214. sub. r3,r9,r10
  215. { if chars not equal, we're ready }
  216. bne LStrlCompDone
  217. { if they are equal and one is zero, then the other one is zero too }
  218. { and we're done as well (r3 also contains 0 then) }
  219. { otherwise loop (if ctr <> 0) }
  220. bdnzf cr1*4+eq,LStrlCompLoop
  221. LStrlCompDone:
  222. end ['r0','r3','r4','r9','r10','cr0','cr1','ctr'];
  223. function stricomp(str1,str2 : pchar) : longint;assembler;
  224. { in: str1 in r3, str2 in r4 }
  225. { out: result of case insensitive comparison (< 0, = 0, > 0) }
  226. asm
  227. { use r28 instead of r3 for str1 since r3 contains result }
  228. subi r28,r3,1
  229. subi r4,r4,1
  230. LStriCompLoop:
  231. { load next chars }
  232. lbzu r29,1(r28)
  233. { check if one is zero }
  234. cmpli cr1,r29,0
  235. lbzu r30,1(r4)
  236. { calculate difference }
  237. sub. r3,r29,r30
  238. { if chars are equal, no further test is necessary }
  239. beq+ LStriCompEqual
  240. { make both lowercase, no branches }
  241. li r27,0
  242. li r25,0
  243. { r3 := r29 - 'A' }
  244. subic r3,r29,'A'
  245. { if r29 < 'A' then r27 := 0 else r27 := $ffffffff }
  246. addme r27,r27
  247. { same for r30 }
  248. subic r3,r30,'A'
  249. addme r25,r25
  250. { r3 := 'Z' - r29 }
  251. subfic r3,r29,'Z'
  252. { if r29 < 'A' then r27 := 0 else r27 := $20 }
  253. andi r27,r27,0x020
  254. { if r29 > Z then r26 := 0 else r26 := $ffffffff }
  255. subfe r26,r26,r26
  256. { same for r30 }
  257. subfic r3,r30,'Z'
  258. andi r25,r25,0x020
  259. subfe r24,r24,r24
  260. { if (r29 in ['A'..'Z'] then r27 := $20 else r27 := 0 }
  261. and r27,r27,r26
  262. { same for r30 }
  263. and r25,r25,r24
  264. { make lowercase }
  265. add r29,r29,r27
  266. { same for r30 }
  267. add r30,r30,r25
  268. { compare again }
  269. sub. r3,r29,r30
  270. bne LStrCompDone
  271. LStriCompEqual:
  272. { if they are equal and one is zero, then the other one is zero too }
  273. { and we're done as well (r3 also contains 0 then) }
  274. { otherwise loop }
  275. bne cr1,LStriCompLoop
  276. LStriCompDone:
  277. end ['r3','r4','r26','r27','r28','r29','r30','cr0','cr1'];
  278. function strlicomp(str1,str2 : pchar;l : longint) : longint;assembler;
  279. { (same as stricomp, but maximally compare until l'th character) }
  280. { in: str1 in r3, str2 in r4, l in r5 }
  281. { out: result of case insensitive comparison (< 0, = 0, > 0) }
  282. asm
  283. { load the begin of one of the string in the data cache }
  284. dcbt 0,r3
  285. { use r0 instead of r3 for str1 since r3 contains result }
  286. cmpl r5,0
  287. subi r0,r3,1
  288. li r3,0
  289. beq- LStrlCompDone
  290. mtctr r5
  291. subi r4,r4,1
  292. LStriCompLoop:
  293. { load next chars }
  294. lbzu r9,1(r0)
  295. { check if one is zero }
  296. cmpli cr1,r9,0
  297. lbzu r10,1(r4)
  298. { calculate difference }
  299. sub. r3,r9,r10
  300. { if chars are equal, no further test is necessary }
  301. beq+ LStriCompEqual
  302. { see stricomp for explanation }
  303. li r8,0
  304. li r5,0
  305. subic r3,r9,'A'
  306. addme r8,r8
  307. subic r3,r10,'A'
  308. addme r5,r5
  309. subfic r3,r9,'Z'
  310. andi r8,r8,0x020
  311. subfe r7,r7,r7
  312. subfic r3,r10,'Z'
  313. andi r5,r5,0x020
  314. subfe r24,r24,r24
  315. and r8,r8,r7
  316. and r5,r5,r24
  317. add r9,r9,r8
  318. add r10,r10,r5
  319. { compare again }
  320. sub. r3,r9,r10
  321. bne LStrCompDone
  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 (if ctr <> 0) }
  326. bdnzf cr1*4+eq,LStriCompLoop
  327. LStriCompDone:
  328. end ['r0','r3','r4','r5','r7','r8','r9','r10','cr0','cr1','ctr'];
  329. function strscan(p : pchar;c : char) : pchar;assembler;
  330. asm
  331. { empty/invalid string? }
  332. cmpli r3,0
  333. { if yes, do nothing }
  334. beq LStrScanDone
  335. subi r3,r3,1
  336. LStrScanLoop:
  337. lbzu r0,1(r3)
  338. cmpl cr1,r0,r4
  339. cmpli r0,0
  340. beq cr1,LStrScanDone
  341. bne LStrScanLoop
  342. LStrScanDone:
  343. end ['r0','r3','r4','cr0','cr1'];
  344. function strrscan(p : pchar;c : char) : pchar;assembler;
  345. asm
  346. { empty/invalid string? }
  347. cmpli r3,0
  348. { if yes, do nothing }
  349. beq LStrrScanDone
  350. { make r0 $ffffffff, later on we take min(r0,r3) }
  351. li r0,0x0ffff
  352. subi r3,r3,1
  353. LStrrScanLoop:
  354. lbzu r10,1(r3)
  355. cmpl cr1,r10,r4
  356. cmpli cr0,r10,0
  357. bne+ cr1,LStrrScanNotFound
  358. { store address of found position }
  359. mr r0,r3
  360. LStrrScanNotFound:
  361. bne LStrrScanLoop
  362. { Select min of r3 and r0 -> end of string or found position }
  363. { From the PPC compiler writer's guide, not sure if I could ever }
  364. { come up with something like this :) }
  365. subfc r10,r3,r0 { r10 = r0 - r3, CA = (r0 >= r3) ? 1 : 0 }
  366. subfe r0,r0,r0 { r0' = (r0 >= r3) ? 0 : -1 }
  367. and r10,r10,r0 { r10 = (r0 >= r3) ? 0 : r0 - r3 }
  368. add r3,r10,r3 { r3 = (r0 >= r3) ? r3 : r0 }
  369. LStrrScanDone:
  370. end ['r0','r3','r4','r10','cr0','cr1'];
  371. function strupper(p : pchar) : pchar;assembler;
  372. asm
  373. cmpli r3,0
  374. beq LStrUpperNil
  375. subi r9,r3,1
  376. LStrUpperLoop:
  377. lbzu r10,1(r9)
  378. { a <= x <= b <=> cardinal(x-a) <= cardinal(b-a) }
  379. subi r0,r10,97
  380. cmpli r0,122-97
  381. cmpli cr1,r10,0
  382. subi r10,r10,0x20
  383. bgt LStrUpper1
  384. stb r10,0(r9)
  385. LStrUpper1:
  386. bne cr1,LStrUpperLoop
  387. LStrUpperNil:
  388. end ['r0','r9','r10','cr0','cr1'];
  389. function strlower(p : pchar) : pchar;assembler;
  390. asm
  391. cmpli r3,0
  392. beq LStrLowerNil
  393. subi r9,r3,1
  394. LStrLowerLoop:
  395. lbzu r10,1(r9)
  396. { a <= x <= b <=> cardinal(x-a) <= cardinal(b-a) }
  397. subi r0,r10,65
  398. cmpli r0,90-65
  399. cmpli cr1,r10,0
  400. addi r10,r10,0x20
  401. bgt LStrLower1
  402. stb r10,0(r9)
  403. LStrLower1:
  404. bne cr1,LStrLowerLoop
  405. LStrLowerNil:
  406. end ['r0','r9','r10','cr0','cr1'];
  407. {
  408. $Log$
  409. Revision 1.10 2001-09-28 13:25:04 jonas
  410. * fixed wrong alignment code (sometimes we aligned to multiple of 8
  411. instead of the desired multiple of 4)
  412. Revision 1.9 2001/09/27 15:30:29 jonas
  413. * conversion to compilerproc and to structure used by i386 rtl
  414. * some bugfixes
  415. * powerpc.inc is almost complete (only fillchar/word/dword, get_frame etc
  416. and the class helpers are still needed
  417. - removed unnecessary register saving in set.inc (thanks to compilerproc)
  418. * use registers reserved for parameters as much as possible instead of
  419. those reserved for local vars (since those have to be saved by the
  420. called anyway, while the ones for local vars have to be saved by the
  421. callee)
  422. Revision 1.8 2001/07/21 15:51:50 jonas
  423. * fixed small bug in stricomp
  424. Revision 1.7 2001/07/07 12:46:12 jonas
  425. * some small bugfixes and cache optimizations
  426. Revision 1.6 2001/02/23 14:05:33 jonas
  427. * optimized strcopy/strecopy
  428. Revision 1.5 2001/02/11 17:59:14 jonas
  429. * fixed bug in strscan
  430. Revision 1.4 2001/02/11 12:15:03 jonas
  431. * some small optimizations and bugfixes
  432. Revision 1.3 2001/02/10 16:09:43 jonas
  433. + implemented all missing routines and changed reg allocation to follow ABI
  434. Revision 1.2 2001/02/10 12:28:22 jonas
  435. * fixed some bugs, simplified/optimized already implemented routines and code some more
  436. Revision 1.1 2000/11/05 17:17:08 jonas
  437. + first implementation, not yet finished
  438. }