__aeabi_ldivmod: fix sign logic
[minix.git] / lib / libc / stdlib / l64a.c
blobbc78ab1ea00916478a93297bcc9e01048c33ecff
1 /* $NetBSD: l64a.c,v 1.14 2012/03/13 21:13:48 christos Exp $ */
3 /*
4 * Written by J.T. Conklin <jtc@NetBSD.org>.
5 * Public domain.
6 */
8 #include <sys/cdefs.h>
9 #if defined(LIBC_SCCS) && !defined(lint)
10 __RCSID("$NetBSD: l64a.c,v 1.14 2012/03/13 21:13:48 christos Exp $");
11 #endif
13 #include "namespace.h"
15 #include <assert.h>
16 #include <errno.h>
17 #include <stdlib.h>
19 #ifdef __weak_alias
20 __weak_alias(l64a,_l64a)
21 __weak_alias(l64a_r,_l64a_r)
22 #endif
24 char *
25 l64a(long value)
27 static char buf[8];
29 (void)l64a_r(value, buf, (int)sizeof (buf));
30 return buf;
33 int
34 l64a_r(long value, char *buffer, int buflen)
36 char *s = buffer;
37 int digit;
38 unsigned long v = value;
40 _DIAGASSERT(buffer != NULL);
42 if (value == 0UL)
43 goto out;
45 for (; v != 0 && buflen > 1; s++, buflen--) {
46 digit = (int)(v & 0x3f);
48 if (digit < 2)
49 *s = digit + '.';
50 else if (digit < 12)
51 *s = digit + '0' - 2;
52 else if (digit < 38)
53 *s = digit + 'A' - 12;
54 else
55 *s = digit + 'a' - 38;
56 v >>= 6;
59 out:
60 *s = '\0';
62 return (v == 0UL ? 0 : -1);