memcmp.S revision 1.2 1 1.2 isaki /* $NetBSD: memcmp.S,v 1.2 2024/01/07 07:58:34 isaki Exp $ */
2 1.1 isaki
3 1.1 isaki /*
4 1.1 isaki * Copyright (C) 2020 Tetsuya Isaki. All rights reserved.
5 1.1 isaki * Copyright (C) 2020 Y.Sugahara (moveccr). All rights reserved.
6 1.1 isaki *
7 1.1 isaki * Redistribution and use in source and binary forms, with or without
8 1.1 isaki * modification, are permitted provided that the following conditions
9 1.1 isaki * are met:
10 1.1 isaki * 1. Redistributions of source code must retain the above copyright
11 1.1 isaki * notice, this list of conditions and the following disclaimer.
12 1.1 isaki * 2. Redistributions in binary form must reproduce the above copyright
13 1.1 isaki * notice, this list of conditions and the following disclaimer in the
14 1.1 isaki * documentation and/or other materials provided with the distribution.
15 1.1 isaki *
16 1.1 isaki * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
17 1.1 isaki * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
18 1.1 isaki * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
19 1.1 isaki * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
20 1.1 isaki * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
21 1.1 isaki * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
22 1.1 isaki * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
23 1.1 isaki * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
24 1.1 isaki * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25 1.1 isaki * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26 1.1 isaki * SUCH DAMAGE.
27 1.1 isaki */
28 1.1 isaki
29 1.1 isaki /*
30 1.1 isaki * Size optimized (but slow) version for primary bootloader.
31 1.1 isaki */
32 1.1 isaki
33 1.1 isaki #include <machine/asm.h>
34 1.1 isaki
35 1.1 isaki |
36 1.1 isaki | int memcmp(const void *b1, const void *b2, size_t len)
37 1.1 isaki |
38 1.1 isaki ASENTRY_NOPROFILE(memcmp)
39 1.1 isaki moveml %sp@,%d0-%d1/%a0-%a1 | %d0: (return address)
40 1.1 isaki | %d1: b1
41 1.1 isaki | %a0: b2
42 1.1 isaki | %a1: len
43 1.1 isaki
44 1.1 isaki exg %d1,%a1 | %d1: len
45 1.1 isaki | %a0: b2
46 1.1 isaki | %a1: b1
47 1.1 isaki moveql #0,%d0
48 1.1 isaki loop:
49 1.1 isaki subql #1,%d1 | if (--len < 0)
50 1.1 isaki jcs exit | goto exit
51 1.1 isaki compare:
52 1.1 isaki cmpmb %a0@+,%a1@+
53 1.1 isaki jeq loop
54 1.1 isaki | To comply with standards, recalc exact return value.
55 1.1 isaki | Although everyone expects only 0 or not...
56 1.1 isaki moveq #0,%d1
57 1.1 isaki moveb %a1@-,%d0
58 1.1 isaki moveb %a0@-,%d1
59 1.1 isaki subl %d1,%d0 | (uint)*b1 - (uint)*b2
60 1.1 isaki exit:
61 1.1 isaki rts
62 1.1 isaki
63 1.1 isaki
64 1.1 isaki #if defined(SELFTEST)
65 1.1 isaki #include "iocscall.h"
66 1.1 isaki .macro PRINT msg
67 1.1 isaki leal \msg,%a1
68 1.1 isaki IOCS(__B_PRINT)
69 1.1 isaki .endm
70 1.1 isaki
71 1.1 isaki .macro TEST name
72 1.1 isaki leal \name,%a2
73 1.1 isaki jbsr test
74 1.1 isaki .endm
75 1.1 isaki
76 1.1 isaki ASENTRY_NOPROFILE(selftest_memcmp)
77 1.1 isaki moveml %d2-%d7/%a2-%a6,%sp@-
78 1.1 isaki PRINT %pc@(msg_testname)
79 1.1 isaki
80 1.1 isaki TEST test1
81 1.1 isaki TEST test2
82 1.1 isaki TEST test3
83 1.1 isaki TEST test4
84 1.1 isaki TEST test5
85 1.1 isaki
86 1.1 isaki PRINT %pc@(msg_crlf)
87 1.1 isaki moveml %sp@+,%d2-%d7/%a2-%a6
88 1.1 isaki rts
89 1.1 isaki
90 1.1 isaki test:
91 1.1 isaki movel %a2@+,buf:W | contents of b1
92 1.1 isaki movel %a2@+,(buf+4):W | contents of b2
93 1.1 isaki movel %a2@+,%sp@- | push len
94 1.1 isaki peal (buf+4):W | push b2
95 1.1 isaki peal (buf):W | push b1
96 1.1 isaki jbsr memcmp
97 1.1 isaki leal %sp@(12),%sp
98 1.1 isaki
99 1.1 isaki cmpl %a2@,%d0 | compare return value
100 1.1 isaki jne fail
101 1.1 isaki PRINT %pc@(msg_ok)
102 1.1 isaki rts
103 1.1 isaki fail:
104 1.1 isaki PRINT %pc@(msg_fail)
105 1.1 isaki rts
106 1.1 isaki
107 1.1 isaki test1:
108 1.1 isaki | b1 == b2 within length
109 1.2 isaki .long 0x11223344 | b1
110 1.1 isaki .long 0x11223355 | b2
111 1.1 isaki .long 3 | len
112 1.1 isaki .long 0 | expected
113 1.1 isaki
114 1.1 isaki test2:
115 1.1 isaki | b1 > b2 before the last
116 1.1 isaki .long 0x11813344 | b1
117 1.1 isaki .long 0x11013344 | b2
118 1.1 isaki .long 3 | len
119 1.1 isaki .long 0x00000080 | expected
120 1.1 isaki
121 1.1 isaki test3:
122 1.1 isaki | b1 < b2 in the last byte
123 1.1 isaki .long 0x11220044 | b1
124 1.1 isaki .long 0x11220144 | b2
125 1.1 isaki .long 3 | len
126 1.1 isaki .long -1 | expected
127 1.1 isaki
128 1.1 isaki test4: | len == 0
129 1.1 isaki .long 0x11223344 | b1
130 1.1 isaki .long 0x11223344 | b2
131 1.1 isaki .long 0 | len
132 1.1 isaki .long 0 | expected
133 1.1 isaki
134 1.1 isaki test5: | *b1 - *b2 = 0 - 255 = -255
135 1.1 isaki .long 0x00000000 | b1
136 1.1 isaki .long 0xff000000 | b2
137 1.1 isaki .long 1 | len
138 1.1 isaki .long -255 | expected
139 1.1 isaki
140 1.1 isaki msg_testname:
141 1.1 isaki .asciz "memcmp"
142 1.1 isaki msg_ok:
143 1.1 isaki .asciz " ok"
144 1.1 isaki msg_fail:
145 1.1 isaki .asciz " fail"
146 1.1 isaki msg_crlf:
147 1.1 isaki .asciz "\r\n"
148 1.1 isaki
149 1.1 isaki BSS(buf, 8)
150 1.1 isaki #endif
151