⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 _fpmaxtostr.c

📁 Axis 221 camera embedded programing interface
💻 C
📖 第 1 页 / 共 2 页
字号:
/* Copyright (C) 2004       Manuel Novoa III    <mjn3@codepoet.org> * * GNU Library General Public License (LGPL) version 2 or later. * * Dedicated to Toni.  See uClibc/DEDICATION.mjn3 for details. */#include "_stdio.h"#include <printf.h>#include <float.h>#include <locale.h>#include <bits/uClibc_fpmax.h>typedef void (__fp_outfunc_t)(FILE *fp, intptr_t type, intptr_t len,							  intptr_t buf);/* Copyright (C) 2000, 2001, 2003      Manuel Novoa III * * Function:  * *     size_t _fpmaxtostr(FILE * fp, __fpmax_t x, struct printf_info *info, *                         __fp_outfunc_t fp_outfunc); * * This is derived from the old _dtostr, whic I wrote for uClibc to provide * floating point support for the printf functions.  It handles +/- infinity, * nan, and signed 0 assuming you have ieee arithmetic.  It also now handles * digit grouping (for the uClibc supported locales) and hexadecimal float * notation.  Finally, via the fp_outfunc parameter, it now supports wide * output. * * Notes: * * At most DECIMAL_DIG significant digits are kept.  Any trailing digits * are treated as 0 as they are really just the results of rounding noise * anyway.  If you want to do better, use an arbitary precision arithmetic * package.  ;-) * * It should also be fairly portable, as no assumptions are made about the * bit-layout of doubles.  Of course, that does make it less efficient than * it could be. * *//*****************************************************************************//* Don't change anything that follows unless you know what you're doing.     *//*****************************************************************************//* Fairly portable nan check.  Bitwise for i386 generated larger code. * If you have a better version, comment this out. */#define isnan(x)             ((x) != (x))/* Without seminumerical functions to examine the sign bit, this is * about the best we can do to test for '-0'. */#define zeroisnegative(x)    ((1./(x)) < 0)/*****************************************************************************//* Don't change anything that follows peroid!!!  ;-)                         *//*****************************************************************************/#ifdef __UCLIBC_HAS_HEXADECIMAL_FLOATS__#if FLT_RADIX != 2#error FLT_RADIX != 2 is not currently supported#endif#endif /* __UCLIBC_HAS_HEXADECIMAL_FLOATS__ */#define NUM_HEX_DIGITS      ((FPMAX_MANT_DIG + 3)/ 4)/* WARNING: Adjust _fp_out_wide() below if this changes! *//* With 32 bit ints, we can get 9 decimal digits per block. */#define DIGITS_PER_BLOCK     9#define HEX_DIGITS_PER_BLOCK 8/* Maximum number of subcases to output double is... *  0 - sign *  1 - padding and initial digit *  2 - digits left of the radix *  3 - 0s left of the radix        or   radix *  4 - radix                       or   digits right of the radix *  5 - 0s right of the radix *  6 - exponent *  7 - trailing space padding * although not all cases may occur. */#define MAX_CALLS 8/*****************************************************************************/#define NUM_DIGIT_BLOCKS   ((DECIMAL_DIG+DIGITS_PER_BLOCK-1)/DIGITS_PER_BLOCK)#define NUM_HEX_DIGIT_BLOCKS \   ((NUM_HEX_DIGITS+HEX_DIGITS_PER_BLOCK-1)/HEX_DIGITS_PER_BLOCK)/* WARNING: Adjust _fp_out_wide() below if this changes! *//* extra space for '-', '.', 'e+###', and nul */#define BUF_SIZE  ( 3 + NUM_DIGIT_BLOCKS * DIGITS_PER_BLOCK )/*****************************************************************************/static const char fmt[] = "inf\0INF\0nan\0NAN\0.\0,";#define INF_OFFSET        0		/* must be 1st */#define NAN_OFFSET        8		/* must be 2nd.. see hex sign handling */#define DECPT_OFFSET     16#define THOUSEP_OFFSET   18#define EMPTY_STRING_OFFSET 3/*****************************************************************************/#if FPMAX_MAX_10_EXP < -FPMAX_MIN_10_EXP#error scaling code can not handle FPMAX_MAX_10_EXP < -FPMAX_MIN_10_EXP#endifstatic const __fpmax_t exp10_table[] ={	1e1L, 1e2L, 1e4L, 1e8L, 1e16L, 1e32L,	/* floats */#if FPMAX_MAX_10_EXP < 32#error unsupported FPMAX_MAX_10_EXP (< 32).  ANSI/ISO C requires >= 37.#endif#if FPMAX_MAX_10_EXP >= 64	1e64L,#endif#if FPMAX_MAX_10_EXP >= 128	1e128L,#endif#if FPMAX_MAX_10_EXP >= 256	1e256L,#endif#if FPMAX_MAX_10_EXP >= 512	1e512L,#endif#if FPMAX_MAX_10_EXP >= 1024	1e1024L,#endif#if FPMAX_MAX_10_EXP >= 2048	1e2048L,#endif#if FPMAX_MAX_10_EXP >= 4096	1e4096L#endif#if FPMAX_MAX_10_EXP >= 8192#error unsupported FPMAX_MAX_10_EXP.  please increase table#endif};#define EXP10_TABLE_SIZE     (sizeof(exp10_table)/sizeof(exp10_table[0]))#define EXP10_TABLE_MAX      (1U<<(EXP10_TABLE_SIZE-1))/*****************************************************************************/#ifdef __UCLIBC_HAS_HEXADECIMAL_FLOATS__#if FLT_RADIX != 2#error FLT_RADIX != 2 is not currently supported#endif#if FPMAX_MAX_EXP < -FPMAX_MIN_EXP#error scaling code can not handle FPMAX_MAX_EXP < -FPMAX_MIN_EXP#endifstatic const __fpmax_t exp16_table[] = {	0x1.0p4L, 0x1.0p8L, 0x1.0p16L, 0x1.0p32L, 0x1.0p64L,#if FPMAX_MAX_EXP >= 128	0x1.0p128L,#endif#if FPMAX_MAX_EXP >= 256	0x1.0p256L,#endif#if FPMAX_MAX_EXP >= 512	0x1.0p512L,#endif#if FPMAX_MAX_EXP >= 1024	0x1.0p1024L,#endif#if FPMAX_MAX_EXP >= 2048	0x1.0p2048L,#endif#if FPMAX_MAX_EXP >= 4096	0x1.0p4096L,#endif#if FPMAX_MAX_EXP >= 8192	0x1.0p8192L,#endif#if FPMAX_MAX_EXP >= 16384	0x1.0p16384L#endif#if FPMAX_MAX_EXP >= 32768 #error unsupported FPMAX_MAX_EXP.  please increase table#endif};#define EXP16_TABLE_SIZE     (sizeof(exp16_table)/sizeof(exp16_table[0]))#define EXP16_TABLE_MAX      (1U<<(EXP16_TABLE_SIZE-1))#endif /* __UCLIBC_HAS_HEXADECIMAL_FLOATS__ *//*****************************************************************************/#define FPO_ZERO_PAD    (0x80 | '0')#define FPO_STR_WIDTH   (0x80 | ' ');#define FPO_STR_PREC    'p'size_t _fpmaxtostr(FILE * fp, __fpmax_t x, struct printf_info *info,				   __fp_outfunc_t fp_outfunc){#ifdef __UCLIBC_HAS_HEXADECIMAL_FLOATS__	__fpmax_t lower_bnd;	__fpmax_t upper_bnd = 1e9;#endif /* __UCLIBC_HAS_HEXADECIMAL_FLOATS__ */	uint_fast32_t digit_block;#ifdef __UCLIBC_HAS_HEXADECIMAL_FLOATS__	uint_fast32_t base = 10;	const __fpmax_t *power_table;	int dpb = DIGITS_PER_BLOCK;	int ndb = NUM_DIGIT_BLOCKS;	int nd = DECIMAL_DIG;	int sufficient_precision = 0;#endif /* __UCLIBC_HAS_HEXADECIMAL_FLOATS__ */#ifdef __UCLIBC_HAS_GLIBC_DIGIT_GROUPING__	int num_groups = 0;	int initial_group;	   /* This does not need to be initialized. */	int tslen;			   /* This does not need to be initialized. */	int nblk2;			   /* This does not need to be initialized. */	const char *ts;		   /* This does not need to be initialized. */#endif /* __UCLIBC_HAS_GLIBC_DIGIT_GROUPING__ */	int i, j;	int round, o_exp;	int exp, exp_neg;	int width, preci;	int cnt;	char *s;	char *e;	intptr_t pc_fwi[3*MAX_CALLS];	intptr_t *ppc;	intptr_t *ppc_last;#ifdef __UCLIBC_MJN3_ONLY__#warning TODO: The size of exp_buf[] should really be determined by the float constants.#endif /* __UCLIBC_MJN3_ONLY__ */	char exp_buf[16];	char buf[BUF_SIZE];	char sign_str[6];			/* Last 2 are for 1st digit + nul. */	char o_mode;	char mode;	width = info->width;	preci = info->prec;	mode = info->spec;	*exp_buf = 'e';	if ((mode|0x20) == 'a') {#ifdef __UCLIBC_HAS_HEXADECIMAL_FLOATS__		*exp_buf = 'p';		if (preci < 0) {			preci = NUM_HEX_DIGITS;			sufficient_precision = 1;		}#else		mode += ('g' - 'a');#endif	}	if (preci < 0) {		preci = 6;	}	*sign_str = '\0';	if (PRINT_INFO_FLAG_VAL(info,showsign)) {		*sign_str = '+';	} else if (PRINT_INFO_FLAG_VAL(info,space)) {		*sign_str = ' ';	}	*(sign_str+1) = 0;	pc_fwi[5] = INF_OFFSET;	if (isnan(x)) {				/* First, check for nan. */		pc_fwi[5] = NAN_OFFSET;		goto INF_NAN;	}	if (x == 0) {				/* Handle 0 now to avoid false positive. */#if 1		if (zeroisnegative(x)) { /* Handle 'signed' zero. */			*sign_str = '-';		}#endif		exp = -1;		goto GENERATE_DIGITS;	}	if (x < 0) {				/* Convert negatives to positives. */		*sign_str = '-';		x = -x;	}	if (__FPMAX_ZERO_OR_INF_CHECK(x)) {	/* Inf since zero handled above. */	INF_NAN:		info->pad = ' ';		ppc = pc_fwi + 6;		pc_fwi[3] = FPO_STR_PREC;		pc_fwi[4] = 3;		if (mode < 'a') {			pc_fwi[5] += 4;		}		pc_fwi[5] = (intptr_t)(fmt + pc_fwi[5]);		goto EXIT_SPECIAL;	}#ifdef __UCLIBC_MJN3_ONLY__#warning TODO: Clean up defines when hexadecimal float notation is unsupported.#endif /* __UCLIBC_MJN3_ONLY__ */#ifdef __UCLIBC_HAS_HEXADECIMAL_FLOATS__	if ((mode|0x20) == 'a') {		lower_bnd = 0x1.0p31L;		upper_bnd = 0x1.0p32L;		power_table = exp16_table;		exp = HEX_DIGITS_PER_BLOCK - 1;		i = EXP16_TABLE_SIZE;		j = EXP16_TABLE_MAX;		dpb = HEX_DIGITS_PER_BLOCK;		ndb = NUM_HEX_DIGIT_BLOCKS;		nd = NUM_HEX_DIGITS;		base = 16;	} else {		lower_bnd = 1e8;/* 		upper_bnd = 1e9; */		power_table = exp10_table;		exp = DIGITS_PER_BLOCK - 1;		i = EXP10_TABLE_SIZE;		j = EXP10_TABLE_MAX;/* 		dpb = DIGITS_PER_BLOCK; *//* 		ndb = NUM_DIGIT_BLOCKS; *//* 		base = 10; */	}#else  /* __UCLIBC_HAS_HEXADECIMAL_FLOATS__ */#define lower_bnd    1e8#define upper_bnd    1e9#define power_table  exp10_table#define dpb          DIGITS_PER_BLOCK#define base         10#define ndb          NUM_DIGIT_BLOCKS#define nd           DECIMAL_DIG	exp = DIGITS_PER_BLOCK - 1;	i = EXP10_TABLE_SIZE;	j = EXP10_TABLE_MAX;#endif /* __UCLIBC_HAS_HEXADECIMAL_FLOATS__ */	exp_neg = 0;	if (x < lower_bnd) {		/* Do we need to scale up or down? */		exp_neg = 1;	}	do {		--i;		if (exp_neg) {			if (x * power_table[i] < upper_bnd) {				x *= power_table[i];				exp -= j;			}		} else {			if (x / power_table[i] >= lower_bnd) {				x /= power_table[i];				exp += j;			}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -