xref: /onnv-gate/usr/src/lib/libc/port/gen/l64a.c (revision 6812:febeba71273d)
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
5*6812Sraf  * Common Development and Distribution License (the "License").
6*6812Sraf  * 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  */
21*6812Sraf 
220Sstevel@tonic-gate /*
23*6812Sraf  * Copyright 2008 Sun Microsystems, Inc.  All rights reserved.
240Sstevel@tonic-gate  * Use is subject to license terms.
250Sstevel@tonic-gate  */
260Sstevel@tonic-gate 
270Sstevel@tonic-gate /*	Copyright (c) 1988 AT&T	*/
280Sstevel@tonic-gate /*	  All Rights Reserved  	*/
290Sstevel@tonic-gate 
30*6812Sraf #pragma ident	"%Z%%M%	%I%	%E% SMI"
310Sstevel@tonic-gate 
320Sstevel@tonic-gate /*
330Sstevel@tonic-gate  * convert long int to base 64 ascii
340Sstevel@tonic-gate  * char set is [./0-9A-Za-z]
350Sstevel@tonic-gate  * two's complement negatives are assumed,
360Sstevel@tonic-gate  * but no assumptions are made about sign propagation on right shift
370Sstevel@tonic-gate  *
380Sstevel@tonic-gate  */
390Sstevel@tonic-gate 
40*6812Sraf #include "lint.h"
410Sstevel@tonic-gate #include "mtlib.h"
420Sstevel@tonic-gate #include "libc.h"
430Sstevel@tonic-gate #include <values.h>
440Sstevel@tonic-gate #include <synch.h>
450Sstevel@tonic-gate #include <thread.h>
460Sstevel@tonic-gate #include <stdlib.h>
470Sstevel@tonic-gate #include <sys/types.h>
480Sstevel@tonic-gate #include "tsd.h"
490Sstevel@tonic-gate 
500Sstevel@tonic-gate #define	BITSPERCHAR	6 /* to hold entire character set */
510Sstevel@tonic-gate #define	BITSUSED	(BITSPERBYTE * sizeof (int))
520Sstevel@tonic-gate #define	NMAX		((BITSUSED + BITSPERCHAR - 1)/BITSPERCHAR)
530Sstevel@tonic-gate #define	SIGN		(-(1 << (BITSUSED - BITSPERCHAR - 1)))
540Sstevel@tonic-gate #define	CHARMASK	((1 << BITSPERCHAR) - 1)
550Sstevel@tonic-gate #define	WORDMASK	((1 << ((NMAX - 1) * BITSPERCHAR)) - 1)
560Sstevel@tonic-gate 
570Sstevel@tonic-gate char *
l64a(long value)580Sstevel@tonic-gate l64a(long value)
590Sstevel@tonic-gate {
600Sstevel@tonic-gate 	/* XPG4: only the lower 32 bits are used */
610Sstevel@tonic-gate 	int lg = (int)value;
620Sstevel@tonic-gate 	char *buf = tsdalloc(_T_L64A, NMAX + 1, NULL);
630Sstevel@tonic-gate 	char *s = buf;
640Sstevel@tonic-gate 
650Sstevel@tonic-gate 	if (buf == NULL)
660Sstevel@tonic-gate 		return (NULL);
670Sstevel@tonic-gate 
680Sstevel@tonic-gate 	while (lg != 0) {
690Sstevel@tonic-gate 
700Sstevel@tonic-gate 		int c = (lg & CHARMASK) + ('0' - 2);
710Sstevel@tonic-gate 
720Sstevel@tonic-gate 		if (c > '9')
730Sstevel@tonic-gate 			c += 'A' - '9' - 1;
740Sstevel@tonic-gate 		if (c > 'Z')
750Sstevel@tonic-gate 			c += 'a' - 'Z' - 1;
760Sstevel@tonic-gate 		*s++ = (char)c;
770Sstevel@tonic-gate 		/* fill high-order CHAR if negative */
780Sstevel@tonic-gate 		/* but suppress sign propagation */
790Sstevel@tonic-gate 		lg = ((lg < 0) ? (lg >> BITSPERCHAR) | SIGN :
80*6812Sraf 		    lg >> BITSPERCHAR) & WORDMASK;
810Sstevel@tonic-gate 	}
820Sstevel@tonic-gate 	*s = '\0';
830Sstevel@tonic-gate 	return (buf);
840Sstevel@tonic-gate }
85