strings.inc 15 KB

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