Files
SDK_SG200x_V2/flatbuffers/swift/Sources/FlatBuffers/Int+extension.swift
carbon 93b1517102 add flatbuffers
commit 6da1cf79d90eb242e7da5318241d42279a3df3ba
Author: Max Burke <max@urbanlogiq.com>
Date:   Sun Jan 19 14:47:28 2020 -0800

    [rust] Add use declarations to Rust-generated bindings for imported FB definitions (#5645)

    * Bugfix for Rust generation of union fields named with language keywords
2024-05-31 14:12:12 +08:00

32 lines
722 B
Swift

import Foundation
extension Int {
/// Moves the current int into the nearest power of two
///
/// This is used since the UnsafeMutableRawPointer will face issues when writing/reading
/// if the buffer alignment exceeds that actual size of the buffer
var convertToPowerofTwo: Int {
guard self > 0 else { return 1 }
var n = UOffset(self)
#if arch(arm) || arch(i386)
let max = UInt32(Int.max)
#else
let max = UInt32.max
#endif
n -= 1
n |= n >> 1
n |= n >> 2
n |= n >> 4
n |= n >> 8
n |= n >> 16
if n != max {
n += 1
}
return Int(n)
}
}