Add not-cancel.h for m68k/nptl
[glibc.git] / sysdeps / i386 / memcmp.S
blobf24ec9383fc7cb302cc512faefdb2d0e9cee3fcb
1 /* Compare two memory blocks for differences in the first COUNT bytes.
2    Copyright (C) 1995-2013 Free Software Foundation, Inc.
3    This file is part of the GNU C Library.
5    The GNU C Library is free software; you can redistribute it and/or
6    modify it under the terms of the GNU Lesser General Public
7    License as published by the Free Software Foundation; either
8    version 2.1 of the License, or (at your option) any later version.
10    The GNU C Library 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.  See the GNU
13    Lesser General Public License for more details.
15    You should have received a copy of the GNU Lesser General Public
16    License along with the GNU C Library; if not, see
17    <http://www.gnu.org/licenses/>.  */
19 #include <sysdep.h>
20 #include "asm-syntax.h"
21 #include "bp-sym.h"
22 #include "bp-asm.h"
24 #define PARMS   LINKAGE+4       /* space for 1 saved reg */
25 #define BLK1    PARMS
26 #define BLK2    BLK1+PTR_SIZE
27 #define LEN     BLK2+PTR_SIZE
29         .text
30 ENTRY (BP_SYM (memcmp))
31         ENTER
33         pushl %esi              /* Save callee-safe registers.  */
34         cfi_adjust_cfa_offset (4)
35         movl %edi, %edx         /* Note that %edx is not used and can
36                                    so be used to save %edi.  It's faster.  */
37         cfi_register (edi, edx)
39         movl BLK1(%esp), %esi
40         cfi_rel_offset (esi, 0)
41         movl BLK2(%esp), %edi
42         movl LEN(%esp), %ecx
43         CHECK_BOUNDS_LOW (%esi, BLK1(%esp))
44         CHECK_BOUNDS_LOW (%edi, BLK2(%esp))
46         cld                     /* Set direction of comparison.  */
48         xorl %eax, %eax         /* Default result.  */
50         repe                    /* Compare at most %ecx bytes.  */
51         cmpsb
52         jz L(1)                 /* If even last byte was equal we return 0.  */
54         /* The memory blocks are not equal.  So result of the last
55            subtraction is present in the carry flag.  It is set when
56            the byte in block #2 is bigger.  In this case we have to
57            return -1 (=0xffffffff), else 1.  */
58         sbbl %eax, %eax         /* This is tricky.  %eax == 0 and carry is set
59                                    or not depending on last subtraction.  */
61         /* At this point %eax == 0, if the byte of block #1 was bigger, and
62            0xffffffff if the last byte of block #2 was bigger.  The latter
63            case is already correct but the former needs a little adjustment.
64            Note that the following operation does not change 0xffffffff.  */
65         orb $1, %al             /* Change 0 to 1.  */
67 L(1):   CHECK_BOUNDS_HIGH (%esi, BLK1(%esp), jbe)
68         CHECK_BOUNDS_HIGH (%edi, BLK2(%esp), jbe)
69         popl %esi               /* Restore registers.  */
70         cfi_adjust_cfa_offset (-4)
71         cfi_restore (esi)
72         movl %edx, %edi
73         cfi_restore (edi)
75         LEAVE
76         ret
77 END (BP_SYM (memcmp))
79 #undef bcmp
80 weak_alias (BP_SYM (memcmp), BP_SYM (bcmp))
81 libc_hidden_builtin_def (BP_SYM (memcmp))