strings.inc 14 KB

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