annotate fft/fftw/fftw-3.3.4/kernel/scan.c @ 40:223f770b5341 kissfft-double tip

Try a double-precision kissfft
author Chris Cannam
date Wed, 07 Sep 2016 10:40:32 +0100
parents 26056e866c29
children
rev   line source
Chris@19 1 /*
Chris@19 2 * Copyright (c) 2003, 2007-14 Matteo Frigo
Chris@19 3 * Copyright (c) 2003, 2007-14 Massachusetts Institute of Technology
Chris@19 4 *
Chris@19 5 * This program is free software; you can redistribute it and/or modify
Chris@19 6 * it under the terms of the GNU General Public License as published by
Chris@19 7 * the Free Software Foundation; either version 2 of the License, or
Chris@19 8 * (at your option) any later version.
Chris@19 9 *
Chris@19 10 * This program is distributed in the hope that it will be useful,
Chris@19 11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
Chris@19 12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
Chris@19 13 * GNU General Public License for more details.
Chris@19 14 *
Chris@19 15 * You should have received a copy of the GNU General Public License
Chris@19 16 * along with this program; if not, write to the Free Software
Chris@19 17 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
Chris@19 18 *
Chris@19 19 */
Chris@19 20
Chris@19 21
Chris@19 22 #include "ifftw.h"
Chris@19 23 #include <string.h>
Chris@19 24 #include <stddef.h>
Chris@19 25 #include <stdarg.h>
Chris@19 26 #include <stdio.h>
Chris@19 27
Chris@19 28 #ifdef USE_CTYPE
Chris@19 29 #include <ctype.h>
Chris@19 30 #else
Chris@19 31 /* Screw ctype. On linux, the is* functions call a routine that gets
Chris@19 32 the ctype map in the current locale. Because this operation is
Chris@19 33 expensive, the map is cached on a per-thread basis. I am not
Chris@19 34 willing to link this crap with FFTW. Not over my dead body.
Chris@19 35
Chris@19 36 Sic transit gloria mundi.
Chris@19 37 */
Chris@19 38 #undef isspace
Chris@19 39 #define isspace(x) ((x) >= 0 && (x) <= ' ')
Chris@19 40 #undef isdigit
Chris@19 41 #define isdigit(x) ((x) >= '0' && (x) <= '9')
Chris@19 42 #undef isupper
Chris@19 43 #define isupper(x) ((x) >= 'A' && (x) <= 'Z')
Chris@19 44 #undef islower
Chris@19 45 #define islower(x) ((x) >= 'a' && (x) <= 'z')
Chris@19 46 #endif
Chris@19 47
Chris@19 48 static int mygetc(scanner *sc)
Chris@19 49 {
Chris@19 50 if (sc->ungotc != EOF) {
Chris@19 51 int c = sc->ungotc;
Chris@19 52 sc->ungotc = EOF;
Chris@19 53 return c;
Chris@19 54 }
Chris@19 55 return(sc->getchr(sc));
Chris@19 56 }
Chris@19 57
Chris@19 58 #define GETCHR(sc) mygetc(sc)
Chris@19 59
Chris@19 60 static void myungetc(scanner *sc, int c)
Chris@19 61 {
Chris@19 62 sc->ungotc = c;
Chris@19 63 }
Chris@19 64
Chris@19 65 #define UNGETCHR(sc, c) myungetc(sc, c)
Chris@19 66
Chris@19 67 static void eat_blanks(scanner *sc)
Chris@19 68 {
Chris@19 69 int ch;
Chris@19 70 while (ch = GETCHR(sc), isspace(ch))
Chris@19 71 ;
Chris@19 72 UNGETCHR(sc, ch);
Chris@19 73 }
Chris@19 74
Chris@19 75 static void mygets(scanner *sc, char *s, size_t maxlen)
Chris@19 76 {
Chris@19 77 char *s0 = s;
Chris@19 78 int ch;
Chris@19 79
Chris@19 80 A(maxlen > 0);
Chris@19 81 while ((ch = GETCHR(sc)) != EOF && !isspace(ch)
Chris@19 82 && ch != ')' && ch != '(' && s < s0 + maxlen)
Chris@19 83 *s++ = ch;
Chris@19 84 *s = 0;
Chris@19 85 UNGETCHR(sc, ch);
Chris@19 86 }
Chris@19 87
Chris@19 88 static long getlong(scanner *sc, int base, int *ret)
Chris@19 89 {
Chris@19 90 int sign = 1, ch, count;
Chris@19 91 long x = 0;
Chris@19 92
Chris@19 93 ch = GETCHR(sc);
Chris@19 94 if (ch == '-' || ch == '+') {
Chris@19 95 sign = ch == '-' ? -1 : 1;
Chris@19 96 ch = GETCHR(sc);
Chris@19 97 }
Chris@19 98 for (count = 0; ; ++count) {
Chris@19 99 if (isdigit(ch))
Chris@19 100 ch -= '0';
Chris@19 101 else if (isupper(ch))
Chris@19 102 ch -= 'A' - 10;
Chris@19 103 else if (islower(ch))
Chris@19 104 ch -= 'a' - 10;
Chris@19 105 else
Chris@19 106 break;
Chris@19 107 x = x * base + ch;
Chris@19 108 ch = GETCHR(sc);
Chris@19 109 }
Chris@19 110 x *= sign;
Chris@19 111 UNGETCHR(sc, ch);
Chris@19 112 *ret = count > 0;
Chris@19 113 return x;
Chris@19 114 }
Chris@19 115
Chris@19 116 /* vscan is mostly scanf-like, with our additional format specifiers,
Chris@19 117 but with a few twists. It returns simply 0 or 1 indicating whether
Chris@19 118 the match was successful. '(' and ')' in the format string match
Chris@19 119 those characters preceded by any whitespace. Finally, if a
Chris@19 120 character match fails, it will ungetchr() the last character back
Chris@19 121 onto the stream. */
Chris@19 122 static int vscan(scanner *sc, const char *format, va_list ap)
Chris@19 123 {
Chris@19 124 const char *s = format;
Chris@19 125 char c;
Chris@19 126 int ch = 0;
Chris@19 127 size_t fmt_len;
Chris@19 128
Chris@19 129 while ((c = *s++)) {
Chris@19 130 fmt_len = 0;
Chris@19 131 switch (c) {
Chris@19 132 case '%':
Chris@19 133 getformat:
Chris@19 134 switch ((c = *s++)) {
Chris@19 135 case 's': {
Chris@19 136 char *x = va_arg(ap, char *);
Chris@19 137 mygets(sc, x, fmt_len);
Chris@19 138 break;
Chris@19 139 }
Chris@19 140 case 'd': {
Chris@19 141 int *x = va_arg(ap, int *);
Chris@19 142 *x = (int) getlong(sc, 10, &ch);
Chris@19 143 if (!ch) return 0;
Chris@19 144 break;
Chris@19 145 }
Chris@19 146 case 'x': {
Chris@19 147 int *x = va_arg(ap, int *);
Chris@19 148 *x = (int) getlong(sc, 16, &ch);
Chris@19 149 if (!ch) return 0;
Chris@19 150 break;
Chris@19 151 }
Chris@19 152 case 'M': {
Chris@19 153 md5uint *x = va_arg(ap, md5uint *);
Chris@19 154 *x = (md5uint)
Chris@19 155 (0xffffffffUL & getlong(sc, 16, &ch));
Chris@19 156 if (!ch) return 0;
Chris@19 157 break;
Chris@19 158 }
Chris@19 159 case '*': {
Chris@19 160 if ((fmt_len = va_arg(ap, int)) <= 0) return 0;
Chris@19 161 goto getformat;
Chris@19 162 }
Chris@19 163 default:
Chris@19 164 A(0 /* unknown format */);
Chris@19 165 break;
Chris@19 166 }
Chris@19 167 break;
Chris@19 168 default:
Chris@19 169 if (isspace(c) || c == '(' || c == ')')
Chris@19 170 eat_blanks(sc);
Chris@19 171 if (!isspace(c) && (ch = GETCHR(sc)) != c) {
Chris@19 172 UNGETCHR(sc, ch);
Chris@19 173 return 0;
Chris@19 174 }
Chris@19 175 break;
Chris@19 176 }
Chris@19 177 }
Chris@19 178 return 1;
Chris@19 179 }
Chris@19 180
Chris@19 181 static int scan(scanner *sc, const char *format, ...)
Chris@19 182 {
Chris@19 183 int ret;
Chris@19 184 va_list ap;
Chris@19 185 va_start(ap, format);
Chris@19 186 ret = vscan(sc, format, ap);
Chris@19 187 va_end(ap);
Chris@19 188 return ret;
Chris@19 189 }
Chris@19 190
Chris@19 191 scanner *X(mkscanner)(size_t size, int (*getchr)(scanner *sc))
Chris@19 192 {
Chris@19 193 scanner *s = (scanner *)MALLOC(size, OTHER);
Chris@19 194 s->scan = scan;
Chris@19 195 s->vscan = vscan;
Chris@19 196 s->getchr = getchr;
Chris@19 197 s->ungotc = EOF;
Chris@19 198 return s;
Chris@19 199 }
Chris@19 200
Chris@19 201 void X(scanner_destroy)(scanner *sc)
Chris@19 202 {
Chris@19 203 X(ifree)(sc);
Chris@19 204 }