michael@0: /* contrib/arm-neon/linux.c michael@0: * michael@0: * Copyright (c) 2014 Glenn Randers-Pehrson michael@0: * Written by John Bowler, 2014. michael@0: * Last changed in libpng 1.6.10 [March 6, 2014] michael@0: * michael@0: * This code is released under the libpng license. michael@0: * For conditions of distribution and use, see the disclaimer michael@0: * and license in png.h michael@0: * michael@0: * SEE contrib/arm-neon/README before reporting bugs michael@0: * michael@0: * STATUS: SUPPORTED michael@0: * BUG REPORTS: png-mng-implement@sourceforge.net michael@0: * michael@0: * png_have_neon implemented for Linux by reading the widely available michael@0: * pseudo-file /proc/cpuinfo. michael@0: * michael@0: * This code is strict ANSI-C and is probably moderately portable, it does michael@0: * however use and assumes that /proc/cpuinfo is never localized. michael@0: */ michael@0: #include michael@0: michael@0: static int michael@0: png_have_neon(png_structp png_ptr) michael@0: { michael@0: FILE *f = fopen("/proc/cpuinfo", "rb"); michael@0: michael@0: if (f != NULL) michael@0: { michael@0: /* This is a simple state machine which reads the input byte-by-byte until michael@0: * it gets a match on the 'neon' feature or reaches the end of the stream. michael@0: */ michael@0: static const char ch_feature[] = { 70, 69, 65, 84, 85, 82, 69, 83 }; michael@0: static const char ch_neon[] = { 78, 69, 79, 78 }; michael@0: michael@0: enum michael@0: { michael@0: StartLine, Feature, Colon, StartTag, Neon, HaveNeon, SkipTag, SkipLine michael@0: } state; michael@0: int counter; michael@0: michael@0: for (state=StartLine, counter=0;;) michael@0: { michael@0: int ch = fgetc(f); michael@0: michael@0: if (ch == EOF) michael@0: { michael@0: /* EOF means error or end-of-file, return false; neon at EOF is michael@0: * assumed to be a mistake. michael@0: */ michael@0: fclose(f); michael@0: return 0; michael@0: } michael@0: michael@0: switch (state) michael@0: { michael@0: case StartLine: michael@0: /* Match spaces at the start of line */ michael@0: if (ch <= 32) /* skip control characters and space */ michael@0: break; michael@0: michael@0: counter=0; michael@0: state = Feature; michael@0: /* FALL THROUGH */ michael@0: michael@0: case Feature: michael@0: /* Match 'FEATURE', ASCII case insensitive. */ michael@0: if ((ch & ~0x20) == ch_feature[counter]) michael@0: { michael@0: if (++counter == (sizeof ch_feature)) michael@0: state = Colon; michael@0: break; michael@0: } michael@0: michael@0: /* did not match 'feature' */ michael@0: state = SkipLine; michael@0: /* FALL THROUGH */ michael@0: michael@0: case SkipLine: michael@0: skipLine: michael@0: /* Skip everything until we see linefeed or carriage return */ michael@0: if (ch != 10 && ch != 13) michael@0: break; michael@0: michael@0: state = StartLine; michael@0: break; michael@0: michael@0: case Colon: michael@0: /* Match any number of space or tab followed by ':' */ michael@0: if (ch == 32 || ch == 9) michael@0: break; michael@0: michael@0: if (ch == 58) /* i.e. ':' */ michael@0: { michael@0: state = StartTag; michael@0: break; michael@0: } michael@0: michael@0: /* Either a bad line format or a 'feature' prefix followed by michael@0: * other characters. michael@0: */ michael@0: state = SkipLine; michael@0: goto skipLine; michael@0: michael@0: case StartTag: michael@0: /* Skip space characters before a tag */ michael@0: if (ch == 32 || ch == 9) michael@0: break; michael@0: michael@0: state = Neon; michael@0: counter = 0; michael@0: /* FALL THROUGH */ michael@0: michael@0: case Neon: michael@0: /* Look for 'neon' tag */ michael@0: if ((ch & ~0x20) == ch_neon[counter]) michael@0: { michael@0: if (++counter == (sizeof ch_neon)) michael@0: state = HaveNeon; michael@0: break; michael@0: } michael@0: michael@0: state = SkipTag; michael@0: /* FALL THROUGH */ michael@0: michael@0: case SkipTag: michael@0: /* Skip non-space characters */ michael@0: if (ch == 10 || ch == 13) michael@0: state = StartLine; michael@0: michael@0: else if (ch == 32 || ch == 9) michael@0: state = StartTag; michael@0: break; michael@0: michael@0: case HaveNeon: michael@0: /* Have seen a 'neon' prefix, but there must be a space or new michael@0: * line character to terminate it. michael@0: */ michael@0: if (ch == 10 || ch == 13 || ch == 32 || ch == 9) michael@0: { michael@0: fclose(f); michael@0: return 1; michael@0: } michael@0: michael@0: state = SkipTag; michael@0: break; michael@0: michael@0: default: michael@0: png_error(png_ptr, "png_have_neon: internal error (bug)"); michael@0: } michael@0: } michael@0: } michael@0: michael@0: else michael@0: png_warning(png_ptr, "/proc/cpuinfo open failed"); michael@0: michael@0: return 0; michael@0: }