Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Dynamic arrays are used in FixedWidthInteger.bitRangesCoalesced #126

Open
kubamracek opened this issue Oct 3, 2024 · 2 comments
Open

Dynamic arrays are used in FixedWidthInteger.bitRangesCoalesced #126

kubamracek opened this issue Oct 3, 2024 · 2 comments
Labels
bug Something isn't working mmio Related to the MMIO library
Milestone

Comments

@kubamracek
Copy link

kubamracek commented Oct 3, 2024

Dynamic arrays are used in FixedWidthInteger.bitRangesCoalesced. Swift MMIO should really be allocations free. This was identified while investigating https://forums.swift.org/t/building-swift-mmio-for-stm32c011-with-no-allocations-results-in-compilation-error/75124.

extension FixedWidthInteger {
  @inlinable @inline(__always)
  static func bitRangesCoalesced(bits bitRanges: [Range<Int>]) -> Bool {   // << receives an arrays
    let bitRanges = bitRanges.sorted { $0.lowerBound < $1.lowerBound } // << sorting is not constant-foldable
    var lowerBound = -1
    for bitRange in bitRanges {
      // Specifically ensure that the bit ranges dont overlap, e.g. the
      // following ranges are not valid: 0..<1, 0..<2. This is to ensure ranges
      // are coalesced before iterating reduce the number of mask and shift
      // operations needed.
      guard lowerBound <= bitRange.lowerBound else { return false }
      lowerBound = bitRange.upperBound
    }
    return true
  }
@rauhul rauhul added bug Something isn't working mmio Related to the MMIO library labels Oct 3, 2024
@rauhul rauhul added this to the 0.1.0 milestone Oct 3, 2024
@rauhul
Copy link
Collaborator

rauhul commented Oct 7, 2024

cc @xtremekforever

@rauhul
Copy link
Collaborator

rauhul commented Oct 7, 2024

I already had done a lot of the work to move these checks into the macro and out of the runtime, with nice diagnostics. Ill try to land this soon:

  /// Walk the bit ranges forming error diagnostics for overlapping of ranges.
  ///
  /// Given the example bit field:
  /// ```
  /// @BitField(bits: 0..<24, 8..<32, 16..<48, 36..<44)
  /// var field: Field
  /// ```
  ///
  /// The ranges visually look like:
  /// ```
  /// 0       8       16      24      32  36      44  48
  /// ╎       ╎       ╎       ╎       ╎   ╎       ╎   ╎
  /// •───────────────────────◦       ╎   ╎       ╎   ╎
  /// ╎       •───────────────────────◦   ╎       ╎   ╎
  /// ╎       ╎       •───────────────────────────────◦
  /// ╎       ╎       ╎       ╎       ╎   •───────◦   ╎
  /// ╎       ╎       ╎       ╎       ╎   ╎       ╎   ╎
  /// 0       8       16      24      32  36      44  48
  /// ```
  ///
  /// The following diagnostics will be emitted:
  /// ```
  /// <location> error: overlapping subranges in '@BitField(bits: 0..<24, 8..<40, 16..<48, 36..<44)'
  /// @BitField(bits: 0..<24, 8..<40, 16..<48, 36..<44)
  ///  ^~~~~~~~
  ///
  /// <location> note: subrange '8..<24' of bit range '0..<24' overlaps bit ranges '8..<32' and '16..<48'
  /// @BitField(bits: 0..<24, 8..<40, 16..<48, 36..<44)
  ///                 ^~~~~~
  ///
  /// <location> note: subrange '8..<32' of bit range '8..<32' overlaps bit ranges '0..<24' and '16..<48'
  /// @BitField(bits: 0..<24, 8..<32, 16..<48, 36..<44)
  ///                         ^~~~~~
  ///
  /// <location> note: subranges '16..<32' and '36..<44' of bit range '16..<48' overlap bit ranges '0..<24', '8..<32', and '36..<44'
  /// @BitField(bits: 0..<24, 8..<40, 16..<48, 36..<44)
  ///                                 ^~~~~~~
  ///
  /// <location> note: subrange '36..<44' of bit range '36..<44' overlaps bit range '16..<48'
  /// @BitField(bits: 0..<24, 8..<40, 16..<48, 36..<44)
  ///                                          ^~~~~~~
  /// ```

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working mmio Related to the MMIO library
Projects
None yet
Development

No branches or pull requests

2 participants