@@ -444,6 +444,82 @@ extension UnsafeRawPointer: Strideable {
444444 }
445445}
446446
447+ extension UnsafeRawPointer {
448+ /// Obtain the next pointer properly aligned to store a value of type `T`.
449+ ///
450+ /// If `self` is properly aligned for accessing `T`,
451+ /// this function returns `self`.
452+ ///
453+ /// - Parameters:
454+ /// - type: the type to be stored at the returned address.
455+ /// - Returns: a pointer properly aligned to store a value of type `T`.
456+ @inlinable
457+ @_alwaysEmitIntoClient
458+ public func alignedUp< T> ( for type: T . Type ) -> Self {
459+ let mask = UInt ( Builtin . alignof ( T . self) ) &- 1
460+ let bits = ( UInt ( Builtin . ptrtoint_Word ( _rawValue) ) &+ mask) & ~ mask
461+ return . init( Builtin . inttoptr_Word ( bits. _builtinWordValue) )
462+ }
463+
464+ /// Obtain the preceding pointer properly aligned to store a value of type `T`.
465+ ///
466+ /// If `self` is properly aligned for accessing `T`,
467+ /// this function returns `self`.
468+ ///
469+ /// - Parameters:
470+ /// - type: the type to be stored at the returned address.
471+ /// - Returns: a pointer properly aligned to store a value of type `T`.
472+ @inlinable
473+ @_alwaysEmitIntoClient
474+ public func alignedDown< T> ( for type: T . Type ) -> Self {
475+ let mask = UInt ( Builtin . alignof ( T . self) ) &- 1
476+ let bits = UInt ( Builtin . ptrtoint_Word ( _rawValue) ) & ~ mask
477+ return . init( Builtin . inttoptr_Word ( bits. _builtinWordValue) )
478+ }
479+
480+ /// Obtain the next pointer whose bit pattern is a multiple of `alignment`.
481+ ///
482+ /// If the bit pattern of `self` is a multiple of `alignment`,
483+ /// this function returns `self`.
484+ ///
485+ /// - Parameters:
486+ /// - alignment: the alignment of the returned pointer, in bytes.
487+ /// `alignment` must be a whole power of 2.
488+ /// - Returns: a pointer aligned to `alignment`.
489+ @inlinable
490+ @_alwaysEmitIntoClient
491+ public func alignedUp( toMultipleOf alignment: Int ) -> Self {
492+ let mask = UInt ( alignment. _builtinWordValue) &- 1
493+ _debugPrecondition (
494+ alignment > 0 && UInt ( alignment. _builtinWordValue) & mask == 0 ,
495+ " alignment must be a whole power of 2. "
496+ )
497+ let bits = ( UInt ( Builtin . ptrtoint_Word ( _rawValue) ) &+ mask) & ~ mask
498+ return . init( Builtin . inttoptr_Word ( bits. _builtinWordValue) )
499+ }
500+
501+ /// Obtain the preceding pointer whose bit pattern is a multiple of `alignment`.
502+ ///
503+ /// If the bit pattern of `self` is a multiple of `alignment`,
504+ /// this function returns `self`.
505+ ///
506+ /// - Parameters:
507+ /// - alignment: the alignment of the returned pointer, in bytes.
508+ /// `alignment` must be a whole power of 2.
509+ /// - Returns: a pointer aligned to `alignment`.
510+ @inlinable
511+ @_alwaysEmitIntoClient
512+ public func alignedDown( toMultipleOf alignment: Int ) -> Self {
513+ let mask = UInt ( alignment. _builtinWordValue) &- 1
514+ _debugPrecondition (
515+ alignment > 0 && UInt ( alignment. _builtinWordValue) & mask == 0 ,
516+ " alignment must be a whole power of 2. "
517+ )
518+ let bits = UInt ( Builtin . ptrtoint_Word ( _rawValue) ) & ~ mask
519+ return . init( Builtin . inttoptr_Word ( bits. _builtinWordValue) )
520+ }
521+ }
522+
447523/// A raw pointer for accessing and manipulating
448524/// untyped data.
449525///
@@ -1145,6 +1221,82 @@ extension UnsafeMutableRawPointer: Strideable {
11451221 }
11461222}
11471223
1224+ extension UnsafeMutableRawPointer {
1225+ /// Obtain the next pointer properly aligned to store a value of type `T`.
1226+ ///
1227+ /// If `self` is properly aligned for accessing `T`,
1228+ /// this function returns `self`.
1229+ ///
1230+ /// - Parameters:
1231+ /// - type: the type to be stored at the returned address.
1232+ /// - Returns: a pointer properly aligned to store a value of type `T`.
1233+ @inlinable
1234+ @_alwaysEmitIntoClient
1235+ public func alignedUp< T> ( for type: T . Type ) -> Self {
1236+ let mask = UInt ( Builtin . alignof ( T . self) ) &- 1
1237+ let bits = ( UInt ( Builtin . ptrtoint_Word ( _rawValue) ) &+ mask) & ~ mask
1238+ return . init( Builtin . inttoptr_Word ( bits. _builtinWordValue) )
1239+ }
1240+
1241+ /// Obtain the preceding pointer properly aligned to store a value of type `T`.
1242+ ///
1243+ /// If `self` is properly aligned for accessing `T`,
1244+ /// this function returns `self`.
1245+ ///
1246+ /// - Parameters:
1247+ /// - type: the type to be stored at the returned address.
1248+ /// - Returns: a pointer properly aligned to store a value of type `T`.
1249+ @inlinable
1250+ @_alwaysEmitIntoClient
1251+ public func alignedDown< T> ( for type: T . Type ) -> Self {
1252+ let mask = UInt ( Builtin . alignof ( T . self) ) &- 1
1253+ let bits = UInt ( Builtin . ptrtoint_Word ( _rawValue) ) & ~ mask
1254+ return . init( Builtin . inttoptr_Word ( bits. _builtinWordValue) )
1255+ }
1256+
1257+ /// Obtain the next pointer whose bit pattern is a multiple of `alignment`.
1258+ ///
1259+ /// If the bit pattern of `self` is a multiple of `alignment`,
1260+ /// this function returns `self`.
1261+ ///
1262+ /// - Parameters:
1263+ /// - alignment: the alignment of the returned pointer, in bytes.
1264+ /// `alignment` must be a whole power of 2.
1265+ /// - Returns: a pointer aligned to `alignment`.
1266+ @inlinable
1267+ @_alwaysEmitIntoClient
1268+ public func alignedUp( toMultipleOf alignment: Int ) -> Self {
1269+ let mask = UInt ( alignment. _builtinWordValue) &- 1
1270+ _debugPrecondition (
1271+ alignment > 0 && UInt ( alignment. _builtinWordValue) & mask == 0 ,
1272+ " alignment must be a whole power of 2. "
1273+ )
1274+ let bits = ( UInt ( Builtin . ptrtoint_Word ( _rawValue) ) &+ mask) & ~ mask
1275+ return . init( Builtin . inttoptr_Word ( bits. _builtinWordValue) )
1276+ }
1277+
1278+ /// Obtain the preceding pointer whose bit pattern is a multiple of `alignment`.
1279+ ///
1280+ /// If the bit pattern of `self` is a multiple of `alignment`,
1281+ /// this function returns `self`.
1282+ ///
1283+ /// - Parameters:
1284+ /// - alignment: the alignment of the returned pointer, in bytes.
1285+ /// `alignment` must be a whole power of 2.
1286+ /// - Returns: a pointer aligned to `alignment`.
1287+ @inlinable
1288+ @_alwaysEmitIntoClient
1289+ public func alignedDown( toMultipleOf alignment: Int ) -> Self {
1290+ let mask = UInt ( alignment. _builtinWordValue) &- 1
1291+ _debugPrecondition (
1292+ alignment > 0 && UInt ( alignment. _builtinWordValue) & mask == 0 ,
1293+ " alignment must be a whole power of 2. "
1294+ )
1295+ let bits = UInt ( Builtin . ptrtoint_Word ( _rawValue) ) & ~ mask
1296+ return . init( Builtin . inttoptr_Word ( bits. _builtinWordValue) )
1297+ }
1298+ }
1299+
11481300extension OpaquePointer {
11491301 @_transparent
11501302 public init ( @_nonEphemeral _ from: UnsafeMutableRawPointer ) {
0 commit comments