10Sstevel@tonic-gate/* 20Sstevel@tonic-gate * CDDL HEADER START 30Sstevel@tonic-gate * 40Sstevel@tonic-gate * The contents of this file are subject to the terms of the 56812Sraf * Common Development and Distribution License (the "License"). 66812Sraf * You may not use this file except in compliance with the License. 70Sstevel@tonic-gate * 80Sstevel@tonic-gate * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 90Sstevel@tonic-gate * or http://www.opensolaris.org/os/licensing. 100Sstevel@tonic-gate * See the License for the specific language governing permissions 110Sstevel@tonic-gate * and limitations under the License. 120Sstevel@tonic-gate * 130Sstevel@tonic-gate * When distributing Covered Code, include this CDDL HEADER in each 140Sstevel@tonic-gate * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 150Sstevel@tonic-gate * If applicable, add the following below this CDDL HEADER, with the 160Sstevel@tonic-gate * fields enclosed by brackets "[]" replaced with your own identifying 170Sstevel@tonic-gate * information: Portions Copyright [yyyy] [name of copyright owner] 180Sstevel@tonic-gate * 190Sstevel@tonic-gate * CDDL HEADER END 200Sstevel@tonic-gate */ 216812Sraf 220Sstevel@tonic-gate/* 236812Sraf * Copyright 2008 Sun Microsystems, Inc. All rights reserved. 246812Sraf * Use is subject to license terms. 250Sstevel@tonic-gate */ 260Sstevel@tonic-gate 27*7298SMark.J.Nelson@Sun.COM .file "memcmp.s" 280Sstevel@tonic-gate 290Sstevel@tonic-gate/* 300Sstevel@tonic-gate * memcmp(s1, s2, len) 310Sstevel@tonic-gate * 320Sstevel@tonic-gate * Compare n bytes: s1>s2: >0 s1==s2: 0 s1<s2: <0 330Sstevel@tonic-gate * 340Sstevel@tonic-gate * Fast assembler language version of the following C-program for memcmp 350Sstevel@tonic-gate * which represents the `standard' for the C-library. 360Sstevel@tonic-gate * 370Sstevel@tonic-gate * int 380Sstevel@tonic-gate * memcmp(const void *s1, const void *s2, size_t n) 390Sstevel@tonic-gate * { 400Sstevel@tonic-gate * if (s1 != s2 && n != 0) { 410Sstevel@tonic-gate * const char *ps1 = s1; 420Sstevel@tonic-gate * const char *ps2 = s2; 430Sstevel@tonic-gate * do { 440Sstevel@tonic-gate * if (*ps1++ != *ps2++) 450Sstevel@tonic-gate * return (ps1[-1] - ps2[-1]); 460Sstevel@tonic-gate * } while (--n != 0); 470Sstevel@tonic-gate * } 480Sstevel@tonic-gate * return (NULL); 490Sstevel@tonic-gate * } 500Sstevel@tonic-gate */ 510Sstevel@tonic-gate 520Sstevel@tonic-gate#include <sys/asm_linkage.h> 530Sstevel@tonic-gate 540Sstevel@tonic-gate ANSI_PRAGMA_WEAK(memcmp,function) 550Sstevel@tonic-gate 560Sstevel@tonic-gate ENTRY(memcmp) 570Sstevel@tonic-gate st %g2, [%sp + 68] ! g2 must be restored before retl 580Sstevel@tonic-gate cmp %o0, %o1 ! s1 == s2? 590Sstevel@tonic-gate be .cmpeq 600Sstevel@tonic-gate cmp %o2, 17 610Sstevel@tonic-gate bleu,a .cmpbyt ! for small counts go do bytes 620Sstevel@tonic-gate sub %o1, %o0, %o1 630Sstevel@tonic-gate 640Sstevel@tonic-gate andcc %o0, 3, %o3 ! is s1 aligned? 650Sstevel@tonic-gate bz,a .iss2 ! if so go check s2 660Sstevel@tonic-gate andcc %o1, 3, %o4 ! is s2 aligned? 670Sstevel@tonic-gate cmp %o3, 2 680Sstevel@tonic-gate be .algn2 690Sstevel@tonic-gate cmp %o3, 3 700Sstevel@tonic-gate 710Sstevel@tonic-gate.algn1: ldub [%o0], %o4 ! cmp one byte 720Sstevel@tonic-gate inc %o0 730Sstevel@tonic-gate ldub [%o1], %o5 740Sstevel@tonic-gate inc %o1 750Sstevel@tonic-gate dec %o2 760Sstevel@tonic-gate be .algn3 770Sstevel@tonic-gate cmp %o4, %o5 780Sstevel@tonic-gate be .algn2 790Sstevel@tonic-gate nop 800Sstevel@tonic-gate b,a .noteq 810Sstevel@tonic-gate 820Sstevel@tonic-gate.algn2: lduh [%o0], %o4 830Sstevel@tonic-gate inc 2, %o0 840Sstevel@tonic-gate ldub [%o1], %o5 850Sstevel@tonic-gate inc 1, %o1 860Sstevel@tonic-gate srl %o4, 8, %o3 870Sstevel@tonic-gate cmp %o3, %o5 880Sstevel@tonic-gate be,a 1f 890Sstevel@tonic-gate ldub [%o1], %o5 ! delay slot, get next byte from s2 900Sstevel@tonic-gate b .noteq 910Sstevel@tonic-gate mov %o3, %o4 ! delay slot, move *s1 to %o4 920Sstevel@tonic-gate1: inc %o1 930Sstevel@tonic-gate dec 2, %o2 940Sstevel@tonic-gate and %o4, 0xff, %o4 950Sstevel@tonic-gate cmp %o4, %o5 960Sstevel@tonic-gate.algn3: be,a .iss2 970Sstevel@tonic-gate andcc %o1, 3, %o4 ! delay slot, is s2 aligned? 980Sstevel@tonic-gate b,a .noteq 990Sstevel@tonic-gate 1000Sstevel@tonic-gate.cmpbyt:b .bytcmp 1010Sstevel@tonic-gate deccc %o2 1020Sstevel@tonic-gate1: ldub [%o0 + %o1], %o5 ! byte compare loop 1030Sstevel@tonic-gate inc %o0 1040Sstevel@tonic-gate cmp %o4, %o5 1050Sstevel@tonic-gate be,a .bytcmp 1060Sstevel@tonic-gate deccc %o2 ! delay slot, compare count (len) 1070Sstevel@tonic-gate b,a .noteq 1080Sstevel@tonic-gate.bytcmp:bgeu,a 1b 1090Sstevel@tonic-gate ldub [%o0], %o4 1100Sstevel@tonic-gate.cmpeq: ld [%sp + 68], %g2 1110Sstevel@tonic-gate retl ! strings compare equal 1120Sstevel@tonic-gate clr %o0 1130Sstevel@tonic-gate 1140Sstevel@tonic-gate.noteq_word: ! words aren't equal. find unequal byte 1150Sstevel@tonic-gate srl %o4, 24, %o1 ! first byte 1160Sstevel@tonic-gate srl %o5, 24, %o2 1170Sstevel@tonic-gate cmp %o1, %o2 1180Sstevel@tonic-gate bne 1f 1190Sstevel@tonic-gate sll %o4, 8, %o4 1200Sstevel@tonic-gate sll %o5, 8, %o5 1210Sstevel@tonic-gate srl %o4, 24, %o1 1220Sstevel@tonic-gate srl %o5, 24, %o2 1230Sstevel@tonic-gate cmp %o1, %o2 1240Sstevel@tonic-gate bne 1f 1250Sstevel@tonic-gate sll %o4, 8, %o4 1260Sstevel@tonic-gate sll %o5, 8, %o5 1270Sstevel@tonic-gate srl %o4, 24, %o1 1280Sstevel@tonic-gate srl %o5, 24, %o2 1290Sstevel@tonic-gate cmp %o1, %o2 1300Sstevel@tonic-gate bne 1f 1310Sstevel@tonic-gate sll %o4, 8, %o4 1320Sstevel@tonic-gate sll %o5, 8, %o5 1330Sstevel@tonic-gate srl %o4, 24, %o1 1340Sstevel@tonic-gate srl %o5, 24, %o2 1350Sstevel@tonic-gate1: 1360Sstevel@tonic-gate ld [%sp + 68], %g2 1370Sstevel@tonic-gate retl 1380Sstevel@tonic-gate sub %o1, %o2, %o0 ! delay slot 1390Sstevel@tonic-gate 1400Sstevel@tonic-gate.noteq: 1410Sstevel@tonic-gate ld [%sp + 68], %g2 1420Sstevel@tonic-gate retl ! strings aren't equal 1430Sstevel@tonic-gate sub %o4, %o5, %o0 ! delay slot, return(*s1 - *s2) 1440Sstevel@tonic-gate 1450Sstevel@tonic-gate.iss2: andn %o2, 3, %o3 ! count of aligned bytes 1460Sstevel@tonic-gate and %o2, 3, %o2 ! remaining bytes 1470Sstevel@tonic-gate bz .w4cmp ! if s2 word aligned, compare words 1480Sstevel@tonic-gate cmp %o4, 2 1490Sstevel@tonic-gate be .w2cmp ! s2 half aligned 1500Sstevel@tonic-gate cmp %o4, 1 1510Sstevel@tonic-gate 1520Sstevel@tonic-gate.w3cmp: 1530Sstevel@tonic-gate dec 4, %o3 ! avoid reading beyond the last byte 1540Sstevel@tonic-gate inc 4, %o2 1550Sstevel@tonic-gate ldub [%o1], %g1 ! read a byte to align for word reads 1560Sstevel@tonic-gate inc 1, %o1 1570Sstevel@tonic-gate be .w1cmp ! aligned to 1 or 3 bytes 1580Sstevel@tonic-gate sll %g1, 24, %o5 1590Sstevel@tonic-gate 1600Sstevel@tonic-gate sub %o1, %o0, %o1 1610Sstevel@tonic-gate2: ld [%o0 + %o1], %g1 1620Sstevel@tonic-gate ld [%o0], %o4 1630Sstevel@tonic-gate inc 4, %o0 1640Sstevel@tonic-gate srl %g1, 8, %g2 ! merge with the other half 1650Sstevel@tonic-gate or %g2, %o5, %o5 1660Sstevel@tonic-gate cmp %o4, %o5 1670Sstevel@tonic-gate bne .noteq_word 1680Sstevel@tonic-gate deccc 4, %o3 1690Sstevel@tonic-gate bnz 2b 1700Sstevel@tonic-gate sll %g1, 24, %o5 1710Sstevel@tonic-gate sub %o1, 1, %o1 ! used 3 bytes of the last word read 1720Sstevel@tonic-gate b .bytcmp 1730Sstevel@tonic-gate deccc %o2 1740Sstevel@tonic-gate 1750Sstevel@tonic-gate.w1cmp: 1760Sstevel@tonic-gate dec 4, %o3 ! avoid reading beyond the last byte 1770Sstevel@tonic-gate inc 4, %o2 1780Sstevel@tonic-gate lduh [%o1], %g1 ! read 3 bytes to word align 1790Sstevel@tonic-gate inc 2, %o1 1800Sstevel@tonic-gate sll %g1, 8, %g2 1810Sstevel@tonic-gate or %o5, %g2, %o5 1820Sstevel@tonic-gate 1830Sstevel@tonic-gate sub %o1, %o0, %o1 1840Sstevel@tonic-gate3: ld [%o0 + %o1], %g1 1850Sstevel@tonic-gate ld [%o0], %o4 1860Sstevel@tonic-gate inc 4, %o0 1870Sstevel@tonic-gate srl %g1, 24, %g2 ! merge with the other half 1880Sstevel@tonic-gate or %g2, %o5, %o5 1890Sstevel@tonic-gate cmp %o4, %o5 1900Sstevel@tonic-gate bne .noteq_word 1910Sstevel@tonic-gate deccc 4, %o3 1920Sstevel@tonic-gate bnz 3b 1930Sstevel@tonic-gate sll %g1, 8, %o5 1940Sstevel@tonic-gate sub %o1, 3, %o1 ! used 1 byte of the last word read 1950Sstevel@tonic-gate b .bytcmp 1960Sstevel@tonic-gate deccc %o2 1970Sstevel@tonic-gate 1980Sstevel@tonic-gate.w2cmp: 1990Sstevel@tonic-gate dec 4, %o3 ! avoid reading beyond the last byte 2000Sstevel@tonic-gate inc 4, %o2 2010Sstevel@tonic-gate lduh [%o1], %g1 ! read a halfword to align s2 2020Sstevel@tonic-gate inc 2, %o1 2030Sstevel@tonic-gate sll %g1, 16, %o5 2040Sstevel@tonic-gate 2050Sstevel@tonic-gate sub %o1, %o0, %o1 2060Sstevel@tonic-gate4: ld [%o0 + %o1], %g1 ! read a word from s2 2070Sstevel@tonic-gate ld [%o0], %o4 ! read a word from s1 2080Sstevel@tonic-gate inc 4, %o0 2090Sstevel@tonic-gate srl %g1, 16, %g2 ! merge with the other half 2100Sstevel@tonic-gate or %g2, %o5, %o5 2110Sstevel@tonic-gate cmp %o4, %o5 2120Sstevel@tonic-gate bne .noteq_word 2130Sstevel@tonic-gate deccc 4, %o3 2140Sstevel@tonic-gate bnz 4b 2150Sstevel@tonic-gate sll %g1, 16, %o5 2160Sstevel@tonic-gate sub %o1, 2, %o1 ! only used half of the last read word 2170Sstevel@tonic-gate b .bytcmp 2180Sstevel@tonic-gate deccc %o2 2190Sstevel@tonic-gate 2200Sstevel@tonic-gate.w4cmp: 2210Sstevel@tonic-gate sub %o1, %o0, %o1 2220Sstevel@tonic-gate ld [%o0 + %o1], %o5 2230Sstevel@tonic-gate5: ld [%o0], %o4 2240Sstevel@tonic-gate inc 4, %o0 2250Sstevel@tonic-gate cmp %o4, %o5 2260Sstevel@tonic-gate bne .noteq_word 2270Sstevel@tonic-gate deccc 4, %o3 2280Sstevel@tonic-gate bnz,a 5b 2290Sstevel@tonic-gate ld [%o0 + %o1], %o5 2300Sstevel@tonic-gate b .bytcmp ! compare remaining bytes, if any 2310Sstevel@tonic-gate deccc %o2 2320Sstevel@tonic-gate 2330Sstevel@tonic-gate SET_SIZE(memcmp) 234