beta-0.89.2
[luatex.git] / source / libs / libpng / libpng-src / contrib / arm-neon / linux.c
blob94f9bb1e09fad75cd38f586882b0c499abbe7164
1 /* contrib/arm-neon/linux.c
3 * Copyright (c) 2014 Glenn Randers-Pehrson
4 * Written by John Bowler, 2014.
5 * Last changed in libpng 1.6.16 [December 22, 2014]
7 * This code is released under the libpng license.
8 * For conditions of distribution and use, see the disclaimer
9 * and license in png.h
11 * SEE contrib/arm-neon/README before reporting bugs
13 * STATUS: SUPPORTED
14 * BUG REPORTS: png-mng-implement@sourceforge.net
16 * png_have_neon implemented for Linux by reading the widely available
17 * pseudo-file /proc/cpuinfo.
19 * This code is strict ANSI-C and is probably moderately portable; it does
20 * however use <stdio.h> and it assumes that /proc/cpuinfo is never localized.
22 #include <stdio.h>
24 static int
25 png_have_neon(png_structp png_ptr)
27 FILE *f = fopen("/proc/cpuinfo", "rb");
29 if (f != NULL)
31 /* This is a simple state machine which reads the input byte-by-byte until
32 * it gets a match on the 'neon' feature or reaches the end of the stream.
34 static const char ch_feature[] = { 70, 69, 65, 84, 85, 82, 69, 83 };
35 static const char ch_neon[] = { 78, 69, 79, 78 };
37 enum
39 StartLine, Feature, Colon, StartTag, Neon, HaveNeon, SkipTag, SkipLine
40 } state;
41 int counter;
43 for (state=StartLine, counter=0;;)
45 int ch = fgetc(f);
47 if (ch == EOF)
49 /* EOF means error or end-of-file, return false; neon at EOF is
50 * assumed to be a mistake.
52 fclose(f);
53 return 0;
56 switch (state)
58 case StartLine:
59 /* Match spaces at the start of line */
60 if (ch <= 32) /* skip control characters and space */
61 break;
63 counter=0;
64 state = Feature;
65 /* FALL THROUGH */
67 case Feature:
68 /* Match 'FEATURE', ASCII case insensitive. */
69 if ((ch & ~0x20) == ch_feature[counter])
71 if (++counter == (sizeof ch_feature))
72 state = Colon;
73 break;
76 /* did not match 'feature' */
77 state = SkipLine;
78 /* FALL THROUGH */
80 case SkipLine:
81 skipLine:
82 /* Skip everything until we see linefeed or carriage return */
83 if (ch != 10 && ch != 13)
84 break;
86 state = StartLine;
87 break;
89 case Colon:
90 /* Match any number of space or tab followed by ':' */
91 if (ch == 32 || ch == 9)
92 break;
94 if (ch == 58) /* i.e. ':' */
96 state = StartTag;
97 break;
100 /* Either a bad line format or a 'feature' prefix followed by
101 * other characters.
103 state = SkipLine;
104 goto skipLine;
106 case StartTag:
107 /* Skip space characters before a tag */
108 if (ch == 32 || ch == 9)
109 break;
111 state = Neon;
112 counter = 0;
113 /* FALL THROUGH */
115 case Neon:
116 /* Look for 'neon' tag */
117 if ((ch & ~0x20) == ch_neon[counter])
119 if (++counter == (sizeof ch_neon))
120 state = HaveNeon;
121 break;
124 state = SkipTag;
125 /* FALL THROUGH */
127 case SkipTag:
128 /* Skip non-space characters */
129 if (ch == 10 || ch == 13)
130 state = StartLine;
132 else if (ch == 32 || ch == 9)
133 state = StartTag;
134 break;
136 case HaveNeon:
137 /* Have seen a 'neon' prefix, but there must be a space or new
138 * line character to terminate it.
140 if (ch == 10 || ch == 13 || ch == 32 || ch == 9)
142 fclose(f);
143 return 1;
146 state = SkipTag;
147 break;
149 default:
150 png_error(png_ptr, "png_have_neon: internal error (bug)");
155 #ifdef PNG_WARNINGS_SUPPORTED
156 else
157 png_warning(png_ptr, "/proc/cpuinfo open failed");
158 #endif
160 return 0;