1#!/usr/bin/env python 2# Copyright 2019 The Chromium Authors. All rights reserved. 3# Use of this source code is governed by a BSD-style license that can be 4# found in the LICENSE file. 5 6from __future__ import division 7from __future__ import print_function 8 9from array import array 10import os 11import random 12import shutil 13import sys 14 15MAX_INPUT_SIZE = 5000 # < 1 MB so we don't blow up fuzzer build sizes. 16MAX_FLOAT32 = 3.4028235e+38 17 18 19def IsValidSize(n): 20 if n == 0: 21 return False 22 # PFFFT only supports transforms for inputs of length N of the form 23 # N = (2^a)*(3^b)*(5^c) where a >= 5, b >=0, c >= 0. 24 FACTORS = [2, 3, 5] 25 factorization = [0, 0, 0] 26 for i, factor in enumerate(FACTORS): 27 while n % factor == 0: 28 n = n // factor 29 factorization[i] += 1 30 return factorization[0] >= 5 and n == 1 31 32 33def WriteFloat32ArrayToFile(file_path, size, generator): 34 """Generate an array of float32 values and writes to file.""" 35 with open(file_path, 'wb') as f: 36 float_array = array('f', [generator() for _ in range(size)]) 37 float_array.tofile(f) 38 39 40def main(): 41 if len(sys.argv) < 2: 42 print('Usage: %s <path to output directory>' % sys.argv[0]) 43 sys.exit(1) 44 45 output_path = sys.argv[1] 46 # Start with a clean output directory. 47 if os.path.exists(output_path): 48 shutil.rmtree(output_path) 49 os.makedirs(output_path) 50 51 # List of valid input sizes. 52 N = [n for n in range(MAX_INPUT_SIZE) if IsValidSize(n)] 53 54 # Set the seed to always generate the same random data. 55 random.seed(0) 56 57 # Generate different types of input arrays for each target length. 58 for n in N: 59 # Zeros. 60 WriteFloat32ArrayToFile( 61 os.path.join(output_path, 'zeros_%d' % n), n, lambda: 0) 62 # Max float 32. 63 WriteFloat32ArrayToFile( 64 os.path.join(output_path, 'max_%d' % n), n, lambda: MAX_FLOAT32) 65 # Random values in the s16 range. 66 rnd_s16 = lambda: 32768.0 * 2.0 * (random.random() - 0.5) 67 WriteFloat32ArrayToFile( 68 os.path.join(output_path, 'rnd_s16_%d' % n), n, rnd_s16) 69 70 sys.exit(0) 71 72 73if __name__ == '__main__': 74 main() 75