Skip to content

Commit 8f9f747

Browse files
authored
added bitwise inversion operations, extension and freestanding (#288)
1 parent 072b684 commit 8f9f747

File tree

3 files changed

+28
-0
lines changed

3 files changed

+28
-0
lines changed

Source/MLX/Documentation.docc/Organization/arithmetic.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,7 @@ Note: the `-` and `/` operators are not able to be linked here.
9898
- ``MLXArray/.>=(_:_:)-6zxj9``
9999
- ``MLXArray/.&&(_:_:)``
100100
- ``MLXArray/.||(_:_:)``
101+
- ``MLXArray/~(_:)``
101102
- ``MLXArray/&(_:_:)-9in7a``
102103
- ``MLXArray/&(_:_:)-8js41``
103104
- ``MLXArray/&(_:_:)-7iu4h``
@@ -146,6 +147,7 @@ Note: the `-` and `/` operators are not able to be linked here.
146147
- ``atan2(_:_:stream:)``
147148
- ``atanh(_:stream:)``
148149
- ``bitwiseAnd(_:_:stream:)``
150+
- ``bitwiseInvert(_:stream:)``
149151
- ``bitwiseOr(_:_:stream:)``
150152
- ``bitwiseXOr(_:_:stream:)``
151153
- ``ceil(_:stream:)``

Source/MLX/MLXArray+Ops.swift

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -681,6 +681,20 @@ extension MLXArray {
681681
return MLXArray(result)
682682
}
683683

684+
/// Unary element-wise bitwise invert.
685+
///
686+
/// Apply bitwise inversion to the values in the array.
687+
///
688+
/// ### See Also
689+
/// - <doc:arithmetic>
690+
/// - ``bitwiseInvert(_:stream:)``
691+
public static prefix func ~ (lhs: MLXArray) -> MLXArray {
692+
let s = StreamOrDevice.default
693+
var result = mlx_array_new()
694+
mlx_bitwise_invert(&result, lhs.ctx, s.ctx)
695+
return MLXArray(result)
696+
}
697+
684698
/// Element-wise bitwise and.
685699
///
686700
/// Take the bitwise and of two arrays with numpy-style broadcasting

Source/MLX/Ops+Array.swift

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -358,6 +358,18 @@ public func arrayEqual<T: ScalarOrArray>(
358358
return MLXArray(result)
359359
}
360360

361+
/// Unary element-wise bitwise invert.
362+
///
363+
/// Apply bitwise inversion to the values in the array.
364+
///
365+
/// ### See Also
366+
/// - <doc:arithmetic>
367+
public func bitwiseInvert(_ array: MLXArray, stream: StreamOrDevice = .default) -> MLXArray {
368+
var result = mlx_array_new()
369+
mlx_bitwise_invert(&result, array.ctx, stream.ctx)
370+
return MLXArray(result)
371+
}
372+
361373
/// Element-wise bitwise and.
362374
///
363375
/// Take the bitwise and of two arrays with numpy-style broadcasting

0 commit comments

Comments
 (0)