strings.inc 11 KB

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