<commit_before>// Copyright (c) 2014 Primate Labs Inc.
// Use of this source code is governed by the 2-clause BSD license that
// can be found in the LICENSE file.
import Foundation

struct Complex : Printable {
    internal var real : Float32
    internal var imaginary : Float32
    var description: String {
        return String(format: "(%.5f, %.5fi)", real, imaginary)
    }
    
    init() {
        self.real = 0
        self.imaginary = 0
    }
    
    init(real : Float32, imaginary : Float32) {
        self.real = real
        self.imaginary = imaginary
    }
    
    mutating func assign(real : Float32, imaginary : Float32) {
        self.real = real
        self.imaginary = imaginary
    }
    
    mutating func assign(rhs : Complex) {
        self.real = rhs.real
        self.imaginary = rhs.imaginary
    }
    
    mutating func add(rhs : Complex) {
        self.real += rhs.real
        self.imaginary += rhs.imaginary
    }
    
    mutating func subtract(rhs : Complex) {
        self.real -= rhs.real
        self.imaginary -= rhs.imaginary
    }
}

func * (left : Complex, right : Complex) -> Complex {
    return Complex(real: left.real * right.real - left.imaginary * right.imaginary,
        imaginary: left.real * right.imaginary + left.imaginary * right.real)
}

func + (left : Complex, right : Complex) -> Complex {
    return Complex(real: left.real + right.real, imaginary: left.imaginary + right.imaginary)
}

func - (left : Complex, right : Complex) -> Complex {
    return Complex(real: left.real - right.real, imaginary: left.imaginary - right.imaginary)
}


final class SFFTWorkload : Workload {
  let pi = Float32(acos(-1.0))

  let size : Int
  let chunkSize : Int
  var input : [Complex]
  var output : UnsafeMutablePointer<Complex>
  var wFactors : [Complex] = []

  init(size : Int, chunkSize : Int) {
    self.size = size
    self.chunkSize = chunkSize

    self.input = [Complex](count: size, repeatedValue: Complex())
    self.output = UnsafeMutablePointer<Complex>.alloc(size)
    
    

    // Precompute w factors
    self.wFactors.reserveCapacity(chunkSize)
    var theta : Float32 = 0
    var mult : Float32 = -2.0 * self.pi / Float32(chunkSize)
    for i in 0..<chunkSize {
      self.wFactors.append(Complex(real: cos(theta) as Float32, imaginary: sin(theta) as Float32))
      theta += mult
    }

  }
    deinit {
        self.output.dealloc(size)
    }

  override func worker() {
    for chunkOrigin in stride(from: 0, to: self.size, by: self.chunkSize) {
      reorderInputIntoOutput(chunkOrigin)
      executeInplaceFFTOnOutput(chunkOrigin)
    }

  }

  func reorderInputIntoOutput(chunkOrigin : Int) {
    let chunkSize = UInt32(self.chunkSize)

    // Right shift requred to account for unused leading zeros in the UInt32
    let shiftCorrection = countLeadingZeros(chunkSize) + 1

    for i in 0..<chunkSize {
      var o = i
      // Reverse the bits of o
      o = (o & 0x55555555) << 1 | (o & 0xAAAAAAAA) >> 1
      o = (o & 0x33333333) << 2 | (o & 0xCCCCCCCC) >> 2
      o = (o & 0x0F0F0F0F) << 4 | (o & 0xF0F0F0F0) >> 4
      o = (o & 0x00FF00FF) << 8 | (o & 0xFF00FF00) >> 8
      o = (o & 0x0000FFFF) << 16 | (o & 0xFFFF0000) >> 16

      o >>= shiftCorrection<commit_msg>Allocate `chunkSize` instead of `size` for `output` in FFT<commit_after>// Copyright (c) 2014 Primate Labs Inc.
// Use of this source code is governed by the 2-clause BSD license that
// can be found in the LICENSE file.
import Foundation

struct Complex : Printable {
    internal var real : Float32
    internal var imaginary : Float32
    var description: String {
        return String(format: "(%.5f, %.5fi)", real, imaginary)
    }
    
    init() {
        self.real = 0
        self.imaginary = 0
    }
    
    init(real : Float32, imaginary : Float32) {
        self.real = real
        self.imaginary = imaginary
    }
    
    mutating func assign(real : Float32, imaginary : Float32) {
        self.real = real
        self.imaginary = imaginary
    }
    
    mutating func assign(rhs : Complex) {
        self.real = rhs.real
        self.imaginary = rhs.imaginary
    }
    
    mutating func add(rhs : Complex) {
        self.real += rhs.real
        self.imaginary += rhs.imaginary
    }
    
    mutating func subtract(rhs : Complex) {
        self.real -= rhs.real
        self.imaginary -= rhs.imaginary
    }
}

func * (left : Complex, right : Complex) -> Complex {
    return Complex(real: left.real * right.real - left.imaginary * right.imaginary,
        imaginary: left.real * right.imaginary + left.imaginary * right.real)
}

func + (left : Complex, right : Complex) -> Complex {
    return Complex(real: left.real + right.real, imaginary: left.imaginary + right.imaginary)
}

func - (left : Complex, right : Complex) -> Complex {
    return Complex(real: left.real - right.real, imaginary: left.imaginary - right.imaginary)
}


final class SFFTWorkload : Workload {
  let pi = Float32(acos(-1.0))

  let size : Int
  let chunkSize : Int
  var input : [Complex]
  var output : UnsafeMutablePointer<Complex>
  var wFactors : [Complex] = []

  init(size : Int, chunkSize : Int) {
    self.size = size
    self.chunkSize = chunkSize

    self.input = [Complex](count: size, repeatedValue: Complex())
    self.output = UnsafeMutablePointer<Complex>.alloc(chunkSize)
    
    

    // Precompute w factors
    self.wFactors.reserveCapacity(chunkSize)
    var theta : Float32 = 0
    var mult : Float32 = -2.0 * self.pi / Float32(chunkSize)
    for i in 0..<chunkSize {
      self.wFactors.append(Complex(real: cos(theta) as Float32, imaginary: sin(theta) as Float32))
      theta += mult
    }

  }
    deinit {
      self.output.dealloc(chunkSize)
    }

  override func worker() {
    for chunkOrigin in stride(from: 0, to: self.size, by: self.chunkSize) {
      reorderInputIntoOutput(chunkOrigin)
      executeInplaceFFTOnOutput(chunkOrigin)
    }

  }

  func reorderInputIntoOutput(chunkOrigin : Int) {
    let chunkSize = UInt32(self.chunkSize)

    // Right shift requred to account for unused leading zeros in the UInt32
    let shiftCorrection = countLeadingZeros(chunkSize) + 1

    for i in 0..<chunkSize {
      var o = i
      // Reverse the bits of o
      o = (o & 0x55555555) << 1 | (o & 0xAAAAAAAA) >> 1
      o = (o & 0x33333333) << 2 | (o & 0xCCCCCCCC) >> 2
      o = (o & 0x0F0F0F0F) << 4 | (o & 0xF0F0F0F0) >> 4
      o = (o & 0x00FF00FF) << 8 | (o & 0xFF00FF00) >> 8
      o = (o & 0x0000FFFF) << 16 | (o & 0xFFFF0000) >> 16

      o >>= shiftCorrection
{'fraction_non_alphanumeric': 0.11462631820265932, 'fraction_numerical': 0.033012379642365884, 'mean_word_length': 2.4864144912093766, 'pattern_counts': {'":': 0, '<': 31, '<?xml version=': 0, '>': 37, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 8, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2192299', 'n_tokens_mistral': 2282, 'n_tokens_neox': 2105, 'n_words': 589}