File: dtostre.h

package info (click to toggle)
avr-libc 1%3A1.6.2.cvs20080610-2
  • links: PTS
  • area: main
  • in suites: lenny
  • size: 14,848 kB
  • ctags: 55,619
  • sloc: ansic: 92,267; asm: 6,692; sh: 4,131; makefile: 2,481; python: 976; pascal: 426; perl: 116
file content (108 lines) | stat: -rw-r--r-- 2,304 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
/* Header+source file to test the dtostre() function.
   $Id: dtostre.h,v 1.1 2007/02/06 12:36:58 dmix Exp $
 */

#ifndef	PATTERN_SIZE
# define PATTERN_SIZE	15
#endif

struct dtostre_s {
    union {
	long lo;
	float fl;
    };
    unsigned char prec;
    unsigned char flags;
    char pattern[PATTERN_SIZE];
};

#ifndef	__AVR__

# include <stdio.h>
# include <string.h>

# define DTOSTR_ALWAYS_SIGN 0x01	/* put '+' or ' ' for positives */
# define DTOSTR_PLUS_SIGN   0x02	/* put '+' rather than ' ' */
# define DTOSTR_UPPERCASE   0x04	/* put 'E' rather 'e' */

/* This function is intended for run of test cases on the host computer.
 */
char * dtostre (double val, char *s, unsigned char prec, unsigned char flags)
{
    char fmt[8];
    char sign[2];
    char e;
    
    if (prec > 7) prec = 7;
    
    strcpy(sign, "");
    if (flags & DTOSTR_ALWAYS_SIGN)
	strcpy(sign, " ");
    if (flags & DTOSTR_PLUS_SIGN)
	strcpy(sign, "+");

    e = (flags & DTOSTR_UPPERCASE) ? 'E' : 'e';

    sprintf(fmt, "%%%s.%d%c", sign, prec, e);
    sprintf(s, fmt, val);
    return s;
}
#endif	/* !__AVR__ */

#define	PZLEN	5	/* protected zone length	*/

void run_dtostre (const struct dtostre_s *pt, int testno)
{
    union {
	long lo;
	float fl;
    } val;
    unsigned char prec, flags;
    static char s[2*PZLEN + sizeof(pt->pattern)];
    char c, *ps;
    void *pv;
    
    memset(s, testno, sizeof(s));

#ifdef	__AVR__
    val.lo = pgm_read_dword (& pt->lo);
    prec   = pgm_read_byte (& pt->prec);
    flags  = pgm_read_byte (& pt->flags);
#else
    val.lo = pt->lo;
    prec   = pt->prec;
    flags  = pt->flags;
#endif
    ps = dtostre (val.fl, s + PZLEN, prec, flags);

    if (ps != s + PZLEN)
	exit (testno + 0x1000);
    for (ps = s; ps != s + PZLEN; ps++) {
	if ((unsigned char)*ps != (testno & 0377))
	    exit (testno + 0x2000);
    }

    pv = & pt->pattern;
#ifdef	__AVR__
    do {
	c = pgm_read_byte(pv++);
	if (*ps++ != c) {
	    exit (testno + 0x3000);
	}
    } while (c);
#else
    do {
	c = *(char *)(pv++);
	if (*ps++ != c) {
	    printf ("*** testno= %d:  must= %s  was= %s\n",
		testno, pt->pattern, s + PZLEN);
	    exit (testno + 0x3000);
	}
    } while (c);
#endif

    for (; ps != s + sizeof(s); ps++) {
	if ((unsigned char)*ps != (testno & 0377))
	    exit (testno + 0x4000);
    }
}