File: log-02.c

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 (100 lines) | stat: -rw-r--r-- 2,551 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
/* Test of log() function. Check some values and the monotone.
   $Id: log-02.c,v 1.1 2007/02/05 21:35:58 dmix Exp $
 */
#define	ULPMAX	6	/* Max possible error, "units at last place".	*/

#include <math.h>
#include <stdio.h>
#include <stdlib.h>
#include "progmem.h"

union lofl_u {
    long lo;
    float fl;
};

volatile union lofl_u v = { .lo = 1 };

PROGMEM const struct {		/* Table of test cases:  x, log(x)	*/
    union lofl_u x, z;
} t[] = {

    { { 0x00000001 }, { 0xc2ce8ed0 } },
    { { 0x00000002 }, { 0xc2cd2bec } },
    { { 0x00000003 }, { 0xc2cc5c53 } },

    { { 0x000000ff }, { 0xc2c379af } },
    { { 0x00000100 }, { 0xc2c377ae } },
    { { 0x00000101 }, { 0xc2c375af } },

    { { 0x0000ffff }, { 0xc2b8608f } },
    { { 0x00010000 }, { 0xc2b8608d } },
    { { 0x00010001 }, { 0xc2b8608b } },

    { { 0x007fffff }, { 0xc2aeac50 } },
    { { 0x00800000 }, { 0xc2aeac50 } },
    { { 0x00800001 }, { 0xc2aeac50 } },
    
    /* 0.5	*/
    { { 0x3effffff }, { 0xbf317219 } },
    { { 0x3f000000 }, { 0xbf317218 } },
    { { 0x3f000001 }, { 0xbf317216 } },
    
    /* 1.0	*/
    { { 0x3f7ffffe }, { 0xb4000001 } },
    { { 0x3f7fffff }, { 0xb3800000 } },
    { { 0x3f800000 }, { 0x00000000 } },
    { { 0x3f800001 }, { 0x33ffffff } },
    { { 0x3f800002 }, { 0x347ffffe } },

    /* fraction 0x0.98  --  switch between tables	*/
    { { 0x3f97fffe }, { 0x3e2ff976 } },
    { { 0x3f97ffff }, { 0x3e2ff97d } },
    { { 0x3f980000 }, { 0x3e2ff984 } },
    { { 0x3f980001 }, { 0x3e2ff98a } },

    /* fraction 0x0.E0  --  switch between tables	*/
    { { 0x3fdffffe }, { 0x3f0f42f9 } },
    { { 0x3fdfffff }, { 0x3f0f42fa } },
    { { 0x3fe00000 }, { 0x3f0f42fb } },
    { { 0x3fe00001 }, { 0x3f0f42fc } },

    /* 2.0	*/
    { { 0x3fffffff }, { 0x3f317217 } },
    { { 0x40000000 }, { 0x3f317218 } },
    { { 0x40000001 }, { 0x3f31721a } },
    
    { { 0x7f7ffffd }, { 0x42b17218 } },
    { { 0x7f7ffffe }, { 0x42b17218 } },
    { { 0x7f7fffff }, { 0x42b17218 } },
};

void x_exit (int index)
{
#ifndef	__AVR__
    fprintf (stderr, "t[%d]:  %#lx\n", index - 1, v.lo);
#endif
    exit (index ? index : -1);
}

int main ()
{
    union lofl_u x, z, v1;
    int i;
    
    v.fl = -INFINITY;
    for (i = 0; i < (int) (sizeof(t) / sizeof(t[0])); i++) {
	x.lo = pgm_read_dword (& t[i].x);
	z.lo = pgm_read_dword (& t[i].z);
	v1.fl = log (x.fl);
	if (   ((v1.lo ^ z.lo) & 0x80000000)	/* signbit(v1) != signbit(z) */
	    || labs (v1.lo - z.lo) > ULPMAX
	    || v1.fl < v.fl)
	{
	    v = v1;
	    x_exit (i+1);
	}
	v = v1;
    }
    return 0;
}