File: ffs-1.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 (62 lines) | stat: -rw-r--r-- 1,242 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
/* $Id: ffs-1.c,v 1.1 2007/02/06 12:29:21 dmix Exp $	*/

#include <stdlib.h>
#include <string.h>

int main ()
{
    static struct t_s {
	int val;
	unsigned char pattern;
    } t[] = {
	{ 0, 0 },
	{ 1, 1 },
	{ 0xffff, 1 },
	{ 0x0002, 2 },
	{ 0x8002, 2 },
	{ 0xfff2, 2 },
	{ 0x0040, 7 },
	{ 0x0140, 7 },
	{ 0x0080, 8 },
	{ 0xff80, 8 },
	{ 0x0100, 9 },
	{ 0xff00, 9 },
	{ 0x8000, 16 },
    };
    volatile int vx;
    int i;
    
    /* Possibly, this is an inline substitution.	*/
    for (i = 0; i != (int)(sizeof(t)/sizeof(t[0])); i++) {
	if (ffs(t[i].val) != t[i].pattern)
	    exit (1 + i);
    }
    
    /* Force a function call (against inline substitution).	*/
    for (i = 0; i != (int)(sizeof(t)/sizeof(t[0])); i++) {
	int (* volatile vffs)(int) = ffs;
	if (vffs(t[i].val) != t[i].pattern)
	    exit (101 + i);
    }
    
    /* Side effects?	*/
    vx = 7;
    if (ffs(++vx) != 4 || vx != 8)
	exit (__LINE__);
    vx = 16;
    if (ffs(vx++) != 5 || vx != 17)
	exit (__LINE__);

    vx = 127;
    if (ffs(ffs(++vx)) != 4 || vx != 128)
	exit (__LINE__);

    /* Implicit check	*/
    vx = 1;
    if (__builtin_constant_p(++vx) || __builtin_constant_p(vx = -1))
	exit (__LINE__);
    if (vx != 1)    
	exit (__LINE__);

    return 0;
}