17c478bd9Sstevel@tonic-gate/* 27c478bd9Sstevel@tonic-gate * CDDL HEADER START 37c478bd9Sstevel@tonic-gate * 47c478bd9Sstevel@tonic-gate * The contents of this file are subject to the terms of the 5*9a70fc3bSMark J. Nelson * Common Development and Distribution License (the "License"). 6*9a70fc3bSMark J. Nelson * You may not use this file except in compliance with the License. 77c478bd9Sstevel@tonic-gate * 87c478bd9Sstevel@tonic-gate * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 97c478bd9Sstevel@tonic-gate * or http://www.opensolaris.org/os/licensing. 107c478bd9Sstevel@tonic-gate * See the License for the specific language governing permissions 117c478bd9Sstevel@tonic-gate * and limitations under the License. 127c478bd9Sstevel@tonic-gate * 137c478bd9Sstevel@tonic-gate * When distributing Covered Code, include this CDDL HEADER in each 147c478bd9Sstevel@tonic-gate * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 157c478bd9Sstevel@tonic-gate * If applicable, add the following below this CDDL HEADER, with the 167c478bd9Sstevel@tonic-gate * fields enclosed by brackets "[]" replaced with your own identifying 177c478bd9Sstevel@tonic-gate * information: Portions Copyright [yyyy] [name of copyright owner] 187c478bd9Sstevel@tonic-gate * 197c478bd9Sstevel@tonic-gate * CDDL HEADER END 207c478bd9Sstevel@tonic-gate */ 217c478bd9Sstevel@tonic-gate/* 227c478bd9Sstevel@tonic-gate * Copyright 2005 Sun Microsystems, Inc. All rights reserved. 237c478bd9Sstevel@tonic-gate * Use is subject to license terms. 247c478bd9Sstevel@tonic-gate */ 257c478bd9Sstevel@tonic-gate 26*9a70fc3bSMark J. Nelson .file "strlen.s" 277c478bd9Sstevel@tonic-gate 287c478bd9Sstevel@tonic-gate/ 297c478bd9Sstevel@tonic-gate/ strlen(s) 307c478bd9Sstevel@tonic-gate/ 317c478bd9Sstevel@tonic-gate/ Returns the number of non-NULL bytes in string argument. 327c478bd9Sstevel@tonic-gate/ 337c478bd9Sstevel@tonic-gate/ 347c478bd9Sstevel@tonic-gate/ Fast assembly language version of the following C-program strlen 357c478bd9Sstevel@tonic-gate/ which represents the `standard' for the C-library. 367c478bd9Sstevel@tonic-gate/ 377c478bd9Sstevel@tonic-gate/ size_t 387c478bd9Sstevel@tonic-gate/ strlen(const char *s) 397c478bd9Sstevel@tonic-gate/ { 407c478bd9Sstevel@tonic-gate/ const char *s0 = s + 1; 417c478bd9Sstevel@tonic-gate/ 427c478bd9Sstevel@tonic-gate/ while (*s++ != '\0') 437c478bd9Sstevel@tonic-gate/ ; 447c478bd9Sstevel@tonic-gate/ return (s - s0); 457c478bd9Sstevel@tonic-gate/ } 467c478bd9Sstevel@tonic-gate/ 477c478bd9Sstevel@tonic-gate/ In this assembly language version, the following expression is used 487c478bd9Sstevel@tonic-gate/ to check if a 32-bit word data contains a null byte or not: 497c478bd9Sstevel@tonic-gate/ (((A & 0x7f7f7f7f) + 0x7f7f7f7f) | A) & 0x80808080 507c478bd9Sstevel@tonic-gate/ If the above expression geneates a value other than 0x80808080, 517c478bd9Sstevel@tonic-gate/ that means the 32-bit word data contains a null byte. 527c478bd9Sstevel@tonic-gate/ 537c478bd9Sstevel@tonic-gate 547c478bd9Sstevel@tonic-gate#include "SYS.h" 557c478bd9Sstevel@tonic-gate 567c478bd9Sstevel@tonic-gate ENTRY(strlen) 577c478bd9Sstevel@tonic-gate mov 4(%esp), %edx / src in %edx 587c478bd9Sstevel@tonic-gate mov %edx, %eax / cpy src to %eax 597c478bd9Sstevel@tonic-gate 607c478bd9Sstevel@tonic-gate and $3, %edx / is src aligned? 617c478bd9Sstevel@tonic-gate jz countbytes 627c478bd9Sstevel@tonic-gate / work byte-wise until aligned 637c478bd9Sstevel@tonic-gate cmpb $0, (%eax) / is *src == 0 ? 647c478bd9Sstevel@tonic-gate jz done 657c478bd9Sstevel@tonic-gate inc %eax / increment src 667c478bd9Sstevel@tonic-gate cmp $3, %edx / if aligned, jump to word-wise check 677c478bd9Sstevel@tonic-gate jz countbytes 687c478bd9Sstevel@tonic-gate cmpb $0, (%eax) 697c478bd9Sstevel@tonic-gate jz done 707c478bd9Sstevel@tonic-gate inc %eax 717c478bd9Sstevel@tonic-gate cmp $2, %edx 727c478bd9Sstevel@tonic-gate jz countbytes 737c478bd9Sstevel@tonic-gate cmpb $0, (%eax) 747c478bd9Sstevel@tonic-gate jz done 757c478bd9Sstevel@tonic-gate inc %eax 767c478bd9Sstevel@tonic-gate 777c478bd9Sstevel@tonic-gate .align 16 787c478bd9Sstevel@tonic-gate 797c478bd9Sstevel@tonic-gatecountbytes: 807c478bd9Sstevel@tonic-gate mov (%eax), %ecx / load wrd 817c478bd9Sstevel@tonic-gate add $4, %eax / increment src by 4 (bytes in word) 827c478bd9Sstevel@tonic-gate lea -0x01010101(%ecx), %edx / (wrd - 0x01010101) 837c478bd9Sstevel@tonic-gate not %ecx / ~wrd 847c478bd9Sstevel@tonic-gate and $0x80808080, %ecx / ~wrd & 0x80808080 857c478bd9Sstevel@tonic-gate and %edx, %ecx / (wrd - 0x01010101) & ~wrd & 0x80808080 867c478bd9Sstevel@tonic-gate jz countbytes / if zero, no null byte found -- cont 877c478bd9Sstevel@tonic-gate 887c478bd9Sstevel@tonic-gatehas_zero_byte: 897c478bd9Sstevel@tonic-gate bsfl %ecx, %ecx / find first set bit (null byte) 907c478bd9Sstevel@tonic-gate shr $3, %ecx / switch bit position to byte posn 917c478bd9Sstevel@tonic-gate lea -4(%eax, %ecx, 1), %eax / undo pre-increment and count bytes 927c478bd9Sstevel@tonic-gatedone: 937c478bd9Sstevel@tonic-gate sub 4(%esp), %eax / return (src - old_src) 947c478bd9Sstevel@tonic-gate ret 957c478bd9Sstevel@tonic-gate SET_SIZE(strlen) 96