Skip to content
vsprintf.c 62.2 KiB
Newer Older
Linus Torvalds's avatar
Linus Torvalds committed
/*
 *  linux/lib/vsprintf.c
 *
 *  Copyright (C) 1991, 1992  Linus Torvalds
 */

/* vsprintf.c -- Lars Wirzenius & Linus Torvalds. */
/*
 * Wirzenius wrote this portably, Torvalds fucked it up :-)
 */

Linus Torvalds's avatar
Linus Torvalds committed
 * Fri Jul 13 2001 Crutcher Dunnavant <crutcher+kernel@datastacks.com>
 * - changed to provide snprintf and vsnprintf functions
 * So Feb  1 16:51:32 CET 2004 Juergen Quade <quade@hsnr.de>
 * - scnprintf and vscnprintf
 */

#include <stdarg.h>
#include <linux/module.h>	/* for KSYM_SYMBOL_LEN */
Linus Torvalds's avatar
Linus Torvalds committed
#include <linux/types.h>
#include <linux/string.h>
#include <linux/ctype.h>
#include <linux/kernel.h>
#include <linux/kallsyms.h>
#include <linux/math64.h>
#include <linux/uaccess.h>
#include <linux/ioport.h>
#include <linux/dcache.h>
#include <linux/cred.h>
Linus Torvalds's avatar
Linus Torvalds committed

#include <asm/page.h>		/* for PAGE_SIZE */
#include <asm/sections.h>	/* for dereference_function_descriptor() */
Linus Torvalds's avatar
Linus Torvalds committed

#include <linux/string_helpers.h>
Linus Torvalds's avatar
Linus Torvalds committed
/**
 * simple_strtoull - convert a string to an unsigned long long
Linus Torvalds's avatar
Linus Torvalds committed
 * @cp: The start of the string
 * @endp: A pointer to the end of the parsed string will be placed here
 * @base: The number base to use
 *
 * This function is obsolete. Please use kstrtoull instead.
Linus Torvalds's avatar
Linus Torvalds committed
 */
unsigned long long simple_strtoull(const char *cp, char **endp, unsigned int base)
Linus Torvalds's avatar
Linus Torvalds committed
{
	unsigned long long result;
	unsigned int rv;
	cp = _parse_integer_fixup_radix(cp, &base);
	rv = _parse_integer(cp, base, &result);
	/* FIXME */
	cp += (rv & ~KSTRTOX_OVERFLOW);
Linus Torvalds's avatar
Linus Torvalds committed
	if (endp)
		*endp = (char *)cp;
Linus Torvalds's avatar
Linus Torvalds committed
	return result;
}
EXPORT_SYMBOL(simple_strtoull);
Linus Torvalds's avatar
Linus Torvalds committed

/**
 * simple_strtoul - convert a string to an unsigned long
Linus Torvalds's avatar
Linus Torvalds committed
 * @cp: The start of the string
 * @endp: A pointer to the end of the parsed string will be placed here
 * @base: The number base to use
 *
 * This function is obsolete. Please use kstrtoul instead.
Linus Torvalds's avatar
Linus Torvalds committed
 */
unsigned long simple_strtoul(const char *cp, char **endp, unsigned int base)
Linus Torvalds's avatar
Linus Torvalds committed
{
	return simple_strtoull(cp, endp, base);
Linus Torvalds's avatar
Linus Torvalds committed
}
EXPORT_SYMBOL(simple_strtoul);
Linus Torvalds's avatar
Linus Torvalds committed

/**
 * simple_strtol - convert a string to a signed long
Linus Torvalds's avatar
Linus Torvalds committed
 * @cp: The start of the string
 * @endp: A pointer to the end of the parsed string will be placed here
 * @base: The number base to use
 *
 * This function is obsolete. Please use kstrtol instead.
Linus Torvalds's avatar
Linus Torvalds committed
 */
long simple_strtol(const char *cp, char **endp, unsigned int base)
Linus Torvalds's avatar
Linus Torvalds committed
{
	if (*cp == '-')
		return -simple_strtoul(cp + 1, endp, base);
	return simple_strtoul(cp, endp, base);
Linus Torvalds's avatar
Linus Torvalds committed
}
Linus Torvalds's avatar
Linus Torvalds committed

/**
 * simple_strtoll - convert a string to a signed long long
 * @cp: The start of the string
 * @endp: A pointer to the end of the parsed string will be placed here
 * @base: The number base to use
 *
 * This function is obsolete. Please use kstrtoll instead.
Linus Torvalds's avatar
Linus Torvalds committed
 */
long long simple_strtoll(const char *cp, char **endp, unsigned int base)
Linus Torvalds's avatar
Linus Torvalds committed
{
		return -simple_strtoull(cp + 1, endp, base);
	return simple_strtoull(cp, endp, base);
Linus Torvalds's avatar
Linus Torvalds committed
}
EXPORT_SYMBOL(simple_strtoll);
Linus Torvalds's avatar
Linus Torvalds committed

static noinline_for_stack
int skip_atoi(const char **s)
Linus Torvalds's avatar
Linus Torvalds committed
{
Linus Torvalds's avatar
Linus Torvalds committed

Linus Torvalds's avatar
Linus Torvalds committed
		i = i*10 + *((*s)++) - '0';
Linus Torvalds's avatar
Linus Torvalds committed
	return i;
}

/* Decimal conversion is by far the most typical, and is used
 * for /proc and /sys data. This directly impacts e.g. top performance
 * with many processes running. We optimize it for speed
 * using ideas described at <http://www.cs.uiowa.edu/~jones/bcd/divide.html>
 * (with permission from the author, Douglas W. Jones).
 */
#if BITS_PER_LONG != 32 || BITS_PER_LONG_LONG != 64
/* Formats correctly any integer in [0, 999999999] */
static noinline_for_stack
char *put_dec_full9(char *buf, unsigned q)
	/*
	 * Possible ways to approx. divide by 10
	 * (x * 0x1999999a) >> 32 x < 1073741829 (multiply must be 64-bit)
	 * (x * 0xcccd) >> 19     x <      81920 (x < 262149 when 64-bit mul)
	 * (x * 0x6667) >> 18     x <      43699
	 * (x * 0x3334) >> 17     x <      16389
	 * (x * 0x199a) >> 16     x <      16389
	 * (x * 0x0ccd) >> 15     x <      16389
	 * (x * 0x0667) >> 14     x <       2739
	 * (x * 0x0334) >> 13     x <       1029
	 * (x * 0x019a) >> 12     x <       1029
	 * (x * 0x00cd) >> 11     x <       1029 shorter code than * 0x67 (on i386)
	 * (x * 0x0067) >> 10     x <        179
	 * (x * 0x0034) >>  9     x <         69 same
	 * (x * 0x001a) >>  8     x <         69 same
	 * (x * 0x000d) >>  7     x <         69 same, shortest code (on i386)
	 * (x * 0x0007) >>  6     x <         19
	 * See <http://www.cs.uiowa.edu/~jones/bcd/divide.html>
	 */
	r      = (q * (uint64_t)0x1999999a) >> 32;
	*buf++ = (q - 10 * r) + '0'; /* 1 */
	q      = (r * (uint64_t)0x1999999a) >> 32;
	*buf++ = (r - 10 * q) + '0'; /* 2 */
	r      = (q * (uint64_t)0x1999999a) >> 32;
	*buf++ = (q - 10 * r) + '0'; /* 3 */
	q      = (r * (uint64_t)0x1999999a) >> 32;
	*buf++ = (r - 10 * q) + '0'; /* 4 */
	r      = (q * (uint64_t)0x1999999a) >> 32;
	*buf++ = (q - 10 * r) + '0'; /* 5 */
	/* Now value is under 10000, can avoid 64-bit multiply */
	q      = (r * 0x199a) >> 16;
	*buf++ = (r - 10 * q)  + '0'; /* 6 */
	r      = (q * 0xcd) >> 11;
	*buf++ = (q - 10 * r)  + '0'; /* 7 */
	q      = (r * 0xcd) >> 11;
	*buf++ = (r - 10 * q) + '0'; /* 8 */
	*buf++ = q + '0'; /* 9 */
#endif

/* Similar to above but do not pad with zeros.
 * Code can be easily arranged to print 9 digits too, but our callers
 * always call put_dec_full9() instead when the number has 9 decimal digits.
 */
static noinline_for_stack
char *put_dec_trunc8(char *buf, unsigned r)
	unsigned q;

	/* Copy of previous function's body with added early returns */
	while (r >= 10000) {
		q = r + '0';
		r  = (r * (uint64_t)0x1999999a) >> 32;
		*buf++ = q - 10*r;
	}

	q      = (r * 0x199a) >> 16;	/* r <= 9999 */
	*buf++ = (r - 10 * q)  + '0';
	if (q == 0)
		return buf;
	r      = (q * 0xcd) >> 11;	/* q <= 999 */
	*buf++ = (q - 10 * r)  + '0';
Loading
Loading full blame...