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

📄 ansistdlib.c

📁 VXWORKS源代码
💻 C
📖 第 1 页 / 共 4 页
字号:
    		t1 = mid;        /* if mid element not selected, swap selection there */        if (t1 != mid)             {            SWAP (t1, mid);            mid -= size;            }        }    /* Standard quicksort, Knuth, Vol. 3, page 116, Algorithm Q. */#define	didswap	n1#define	newbot	t1#define	replace	t2    didswap = 0;    bsv = bot;    FOREVER	{    	while ((bot < mid) && (compar (bot, mid) <= 0))	    {	    bot += size;	    }    	while (top > mid) 	    {    	    if (compar (mid, top) <= 0) 		{    	    	top -= size;    	    	continue;    	        }    	    newbot = bot + size;	/* value of bot after swap */    	    if (bot == mid)		/* top <-> mid, mid == top */    	    	replace = mid = top;    	    else 		{			/* bot <-> top */    	    	replace = top;    	    	top -= size;    	        }    	    goto swap;    	    }    	if (bot == mid)    		break;    	/* bot <-> mid, mid == bot */    	replace = mid;    	newbot = mid = bot;		/* value of bot after swap */    	top -= size;swap:	SWAP(bot, replace);    	bot = newbot;    	didswap = 1;        }    /*     * Quicksort behaves badly in the presence of data which is already     * sorted (see Knuth, Vol. 3, page 119) going from O N lg N to O N^2.     * To avoid this worst case behavior, if a re-partitioning occurs     * without swapping any elements, it is not further partitioned and     * is insert sorted.  This wins big with almost sorted data sets and     * only loses if the data set is very strangely partitioned.  A fix     * for those data sets would be to return prematurely if the insertion     * sort routine is forced to make an excessive number of swaps, and     * continue the partitioning.     */    if (!didswap) 	{    	insertion_sort (bsv, nmemb, size, compar);    	return;        }    /*     * Re-partition or sort as necessary.  Note that the mid element     * itself is correctly positioned and can be ignored.     */#define	nlower	n1#define	nupper	n2    bot = bsv;    nlower = (mid - bot) / size;	/* size of lower partition */    mid += size;    nupper = nmemb - nlower - 1;	/* size of upper partition */    /*     * If must call recursively, do it on the smaller partition; this     * bounds the stack to lg N entries.     */    if (nlower > nupper) 	{    	if (nupper >= THRESH)    	    quick_sort (mid, nupper, size, compar);    	else 	    {    	    SORT (mid, nupper);    	    if (nlower < THRESH) 		{    	    	SORT (bot, nlower);    	    	return;    	        }    	    }    	nmemb = nlower;        }     else 	{    	if (nlower >= THRESH)    	    quick_sort (bot, nlower, size, compar);    	else 	    {    	    SORT (bot, nlower);    	    if (nupper < THRESH) 		{    		SORT (mid, nupper);    		return;    		}    	    }    	bot = mid;    	nmemb = nupper;        }    goto partition;    /* NOTREACHED */    }/******************************************************************************** insertion_sort - internal routine** RETURNS: no value.* NOMANUAL*/static void insertion_sort    (    char *       bot,    int          nmemb,    FAST int     size,    int          (*compar)()    )    {    FAST int     cnt;    FAST uint_t  ch;    FAST char *  s1;    FAST char *  s2;    FAST char *  t1;    FAST char *  t2;    FAST char *  top;    /*     * A simple insertion sort (see Knuth, Vol. 3, page 81, Algorithm     * S).  Insertion sort has the same worst case as most simple sorts     * (O N^2).  It gets used here because it is (O N) in the case of     * sorted data.     */    top = bot + nmemb * size;    for (t1 = bot + size; t1 < top;) 	{    	for (t2 = t1; ((t2 -= size) >= bot) && (compar (t1, t2) < 0);)	    ;    	if (t1 != (t2 += size)) 	    {    	    /* Bubble bytes up through each element. */    	    for (cnt = size; cnt--; ++t1) 		{    	    	ch = *t1;    	    	for (s1 = s2 = t1; (s2 -= size) >= t2; s1 = s2)		    {    	    	    *s1 = *s2;		    }    	    	*s1 = ch;    	        }    	    }         else    	    t1 += size;        }    }/* rand.c - rand, srand functions for stdlib  *//* Copyright 1992-1993 Wind River Systems, Inc. *//*modification history--------------------01c,08feb93,jdi  documentation cleanup for 5.1.01b,20sep92,smb  documentation additions.01a,19jul92,smb  written and documented.*//*DESCRIPTIONINCLUDE FILES: stdlib.hSEE ALSO: American National Standard X3.159-1989NOMANUAL*/#include "vxWorks.h"#include "stdlib.h"ulong_t _Randseed = 1;/********************************************************************************* rand - generate a pseudo-random integer between 0 and RAND_MAX  (ANSI)** This routine generates a pseudo-random integer between 0 and RAND_MAX.* The seed value for rand() can be reset with srand().** INCLUDE FILES: stdlib.h ** RETURNS: A pseudo-random integer.** SEE ALSO: srand()*/int rand (void)     {    _Randseed = _Randseed * 1103515245 + 12345;    return (uint_t) (_Randseed/65536) % (RAND_MAX + 1);    }/********************************************************************************* srand - reset the value of the seed used to generate random numbers (ANSI)** This routine resets the seed value used by rand().  If srand() is then* called with the same seed value, the sequence of pseudo-random numbers is* repeated.  If rand() is called before any calls to srand() have been made,* the same sequence shall be generated as when srand() is first called with* the seed value of 1.** INCLUDE FILES: stdlib.h ** RETURNS: N/A** SEE ALSO: rand()*/void * srand     (    uint_t seed		/* random number seed */    )     {    _Randseed = seed;    return (void *)0;    }/* strtod.c function for stdlib  *//* Copyright 1992-1995 Wind River Systems, Inc. *//*modification history--------------------01e,11feb95,jdi  doc fix.01d,16aug93,dvs  fixed value of endptr for strings that start with d, D, e, or E		 (SPR #2229)01c,08feb93,jdi  documentation cleanup for 5.1.01b,20sep92,smb  documentation additions.01a,19jul92,smb  written  and documented*//*DESCRIPTIONINCLUDE FILES: stdlib.h, math.h, assert.h, arrno.hSEE ALSO: American National Standard X3.159-1989NOMANUAL*/#include "vxWorks.h"#include "stdlib.h"#include "math.h"#include "errno.h"#define Ise(c)          ((c == 'e') || (c == 'E') || (c == 'd') || (c == 'D'))#define Isdigit(c)      ((c <= '9') && (c >= '0'))#define Isspace(c)      ((c == ' ') || (c == '\t') || (c=='\n') || (c=='\v') \			 || (c == '\r') || (c == '\f'))#define Issign(c)       ((c == '-') || (c == '+'))#define Val(c)          ((c - '0'))#define MAXE  308#define MINE  (-308)static double powtab[] = {1.0,		          10.0,			  100.0,			  1000.0,			  10000.0};/* flags */#define SIGN    0x01#define ESIGN   0x02#define DECP    0x04/* locals */int             __ten_mul (double *acc, int digit);double          __adjust (double *acc, int dexp, int sign);double          __exp10 (uint_t x);/******************************************************************************* strtod - convert the initial portion of a string to a double (ANSI) ** This routine converts the initial portion of a specified string <s> to a* double.  First, it decomposes the input string into three parts:  an initial, * possibly empty, sequence of white-space characters (as specified by the * isspace() function); a subject sequence resembling a floating-point* constant; and a final string of one or more unrecognized characters, * including the terminating null character of the input string.  Then, it* attempts to convert the subject sequence to a floating-point number, and * returns the result.** The expected form of the subject sequence is an optional plus or minus* decimal-point character, then an optional exponent part but no floating* suffix.  The subject sequence is defined as the longest initial* subsequence of the input string, starting with the first non-white-space* character, that is of the expected form.  The subject sequence contains* no characters if the input string is empty or consists entirely of * white space, or if the first non-white-space character is other than a * sign, a digit, or a decimal-point character.** If the subject sequence has the expected form, the sequence of characters* starting with the first digit or the decimal-point character (whichever* occurs first) is interpreted as a floating constant, except that the * decimal-point character is used in place of a period, and that if neither* an exponent part nor a decimal-point character appears, a decimal point is* assumed to follow the last digit in the string.  If the subject sequence* begins with a minus sign, the value resulting form the conversion is negated.* A pointer to the final string is stored in the object pointed to by <endptr>,* provided that <endptr> is not a null pointer.** In other than the "C" locale, additional implementation-defined subject* sequence forms may be accepted.  VxWorks supports only the "C" locale.** If the subject sequence is empty or does not have the expected form, no* conversion is performed; the value of <s> is stored in the object pointed* to by <endptr>, provided that <endptr> is not a null pointer.** INCLUDE FILES: stdlib.h ** RETURNS:* The converted value, if any.  If no conversion could be performed, it* returns zero.  If the correct value is outside the range of representable* values, it returns plus or minus HUGE_VAL (according to the sign of the* value), and stores the value of the macro ERANGE in `errno'.  If the* correct value would cause underflow, it returns zero and stores the value* of the macro ERANGE in `errno'.**/double strtod     (    const char * s,	/* string to convert */    char **      endptr	/* ptr to final string */    )    {    /* Note that the point positioning is locale dependant */    const char *start     = s;    double      accum     = 0.0;    int         flags     = 0;    int         texp      = 0;    int         e         = 0;    BOOL        conv_done = FALSE;    while (Isspace (*s)) s++;    if (*s == EOS)	{   /* just leading spaces */	if (endptr != NULL) 	    *endptr = CHAR_FROM_CONST (start);	return (0.0);	}    if (Issign (*s))	{	if (*s == '-') flags = SIGN;	if (*++s == EOS)	    {   /* "+|-" : should be an error ? */	    if (endptr != NULL) 		*endptr = CHAR_FROM_CONST (start);	    return (0.0);	    }	}    /* added code to fix problem with leading e, E, d, or D */    if ( !Isdigit (*s) && (*s != '.'))	{	if (endptr != NULL)	    *endptr = CHAR_FROM_CONST (start);	return (0.0);	}    for ( ; (Isdigit (*s) || (*s == '.')); s++)	{	conv_done = TRUE;	if (*s == '.')	    flags |= DECP;	else	    {	    if ( __ten_mul (&accum, Val(*s)) ) 		texp++;	    if (flags & DECP) 		texp--;	    }	}    if (Ise (*s))	{	conv_done = TRUE;	if (*++s != EOS)             /* skip e|E|d|D */	    {                         /* ! ([s]xxx[.[yyy]]e)  */	    while (Isspace (*s)) s++; /* Ansi allows spaces after e */	    if (*s != EOS)		{                     /*  ! ([s]xxx[.[yyy]]e[space])  */		if (Issign (*s))		    if (*s++ == '-') flags |= ESIGN;		if (*s != EOS)                                      /*  ! ([s]xxx[.[yyy]]e[s])  -- error?? */		    {                		    for(; Isdigit (*s); s++)                                      /* prevent from grossly overflowing */			if (e < MAXE) 			    e = e*10 + Val (*s);		                      /* dont care what comes after this */		    if (flags & ESIGN)			texp -= e;		    else			texp += e;		    }		}	    }	}    if (endptr != NULL)	*endptr = CHAR_FROM_CONST ((conv_done) ? s : start);    return (__adjust (&accum, (int) texp, (int) (flags & SIGN)));    }/********************************************************************************* __ten_mul -** multiply 64 bit accumulator by 10 and add digit.* The KA/CA way to do this should be to use* a 64-bit integer internally and use "adjust" to* convert it to float at the end of processing.** AUTHOR:* Taken from cygnus.** RETURNS:* NOMANUAL*/LOCAL int __ten_mul     (    double *acc,    int     digit    )    {    *acc *= 10;    *acc += digit;    return (0);     /* no overflow */    }/********************************************************************************* __adjust -** return (*acc) scaled by 10**dexp.** AUTHOR:* Taken from cygnus.** RETURNS:* NOMANUAL*/LOCAL double __adjust     (    double *acc,	/* *acc    the 64 bit accumulator */    int     dexp,   	/* dexp    decimal exponent       */    int     sign	/* sign    sign flag              */    )    {    double  r;    if (dexp > MAXE)	{	errno = ERANGE;	return (sign) ? -HUGE_VAL : HUGE_VAL;

⌨️ 快捷键说明

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