<!--
{
  "availability" : [
    "iOS: 14.0.0 - 18.0.0",
    "iPadOS: 14.0.0 - 18.0.0",
    "macCatalyst: 14.0.0 - 18.0.0",
    "macOS: 11.0.0 - 15.0.0",
    "tvOS: 14.0.0 - 18.0.0",
    "visionOS: 1.0.0 - 2.0.0",
    "watchOS: 7.0.0 - 11.0.0"
  ],
  "documentType" : "symbol",
  "framework" : "Accelerate",
  "identifier" : "/documentation/Accelerate/BNNSArithmeticTernary",
  "metadataVersion" : "0.1.0",
  "role" : "Structure",
  "symbol" : {
    "kind" : "Structure",
    "modules" : [
      "Accelerate"
    ],
    "preciseIdentifier" : "c:@SA@BNNSArithmeticTernary"
  },
  "title" : "BNNSArithmeticTernary"
}
-->

# BNNSArithmeticTernary

A structure that contains the inputs and output of an arithmetic operation with three inputs.

```
struct BNNSArithmeticTernary
```

## Overview

Use a [`BNNSArithmeticTernary`](/documentation/Accelerate/BNNSArithmeticTernary) structure to pass the descriptions and types of the inputs and output of a binary arithmetic operation to [`BNNSLayerParametersArithmetic`](/documentation/Accelerate/BNNSLayerParametersArithmetic).

The following code shows how to calculate the element-wise multiply-add of two vectors:

```swift
static func ternaryArithmetic() {
    
    let aData = UnsafeMutableBufferPointer<Float>.allocate(capacity: 1)
    _ = aData.initialize(from: [10])
    let aDescriptor = BNNSNDArrayDescriptor(flags: BNNSNDArrayFlags(0),
                                            layout: BNNSDataLayoutVector,
                                            size: (1, 0, 0, 0, 0, 0, 0, 0),
                                            stride: (0, 0, 0, 0, 0, 0, 0, 0),
                                            data: aData.baseAddress!,
                                            data_type: BNNSDataType.float,
                                            table_data: nil,
                                            table_data_type: BNNSDataType.float,
                                            data_scale: 1, data_bias: 0)
    
    let bData = UnsafeMutableBufferPointer<Float>.allocate(capacity: 1)
    _ = bData.initialize(from: [20])
    let bDescriptor = BNNSNDArrayDescriptor(flags: BNNSNDArrayFlags(0),
                                            layout: BNNSDataLayoutVector,
                                            size: (1, 0, 0, 0, 0, 0, 0, 0),
                                            stride: (0, 0, 0, 0, 0, 0, 0, 0),
                                            data: bData.baseAddress!,
                                            data_type: BNNSDataType.float,
                                            table_data: nil,
                                            table_data_type: BNNSDataType.float,
                                            data_scale: 1, data_bias: 0)
    
    let cData = UnsafeMutableBufferPointer<Float>.allocate(capacity: 1)
    _ = cData.initialize(from: [100])
    let cDescriptor = BNNSNDArrayDescriptor(flags: BNNSNDArrayFlags(0),
                                            layout: BNNSDataLayoutVector,
                                            size: (1, 0, 0, 0, 0, 0, 0, 0),
                                            stride: (0, 0, 0, 0, 0, 0, 0, 0),
                                            data: cData.baseAddress!,
                                            data_type: BNNSDataType.float,
                                            table_data: nil,
                                            table_data_type: BNNSDataType.float,
                                            data_scale: 1, data_bias: 0)
    
    let outputData = UnsafeMutableBufferPointer<Float>.allocate(capacity: 1)
    let outputDescriptor = BNNSNDArrayDescriptor(flags: BNNSNDArrayFlags(0),
                                                 layout: BNNSDataLayoutVector,
                                                 size: (1, 0, 0, 0, 0, 0, 0, 0),
                                                 stride: (0, 0, 0, 0, 0, 0, 0, 0),
                                                 data: outputData.baseAddress!,
                                                 data_type: BNNSDataType.float,
                                                 table_data: nil,
                                                 table_data_type: BNNSDataType.float,
                                                 data_scale: 1, data_bias: 0)
    
    let fields = BNNSArithmeticTernary(in1: aDescriptor, in1_type: BNNSSample,
                                       in2: bDescriptor, in2_type: BNNSSample,
                                       in3: cDescriptor, in3_type: BNNSSample,
                                       out: outputDescriptor, out_type: BNNSSample)
    
    let arithmeticLayer: BNNSFilter? = withUnsafePointer(to: fields) { fieldsPtr in
        
        var layerParameters = BNNSLayerParametersArithmetic(
            arithmetic_function: BNNSArithmeticMultiplyAdd,
            arithmetic_function_fields: UnsafeMutableRawPointer(mutating: fieldsPtr),
            activation: .identity)
        
        return BNNSFilterCreateLayerArithmetic(&layerParameters, nil)
    }
    
    var rawInputPointer = [ UnsafeRawPointer(aDescriptor.data!),
                            UnsafeRawPointer(bDescriptor.data!),
                            UnsafeRawPointer(cDescriptor.data!)]
    
    BNNSArithmeticFilterApplyBatch(arithmeticLayer,
                                   1,
                                   3,
                                   &rawInputPointer,
                                   [1, 1, 1],
                                   outputDescriptor.data!,
                                   1)
    
    // Prints `10 * 20 + 100 (300)`
    print(Array(outputData))
    
    aData.deallocate()
    bData.deallocate()
    cData.deallocate()
    outputData.deallocate()
}
```

## Topics

### Creating an Arithmetic Structure

[`init(in1:in1_type:in2:in2_type:in3:in3_type:out:out_type:)`](/documentation/Accelerate/BNNSArithmeticTernary/init(in1:in1_type:in2:in2_type:in3:in3_type:out:out_type:))

Returns a new arithmetic structure that takes three inputs from the specified parameters.

[`init()`](/documentation/Accelerate/BNNSArithmeticTernary/init())

Returns a new arithmetic structure that takes three inputs.

### Inspecting the Properties of an Arithmetic Structure

[`in1`](/documentation/Accelerate/BNNSArithmeticTernary/in1)

The descriptor of the first input.

[`in1_type`](/documentation/Accelerate/BNNSArithmeticTernary/in1_type)

The descriptor type of the first input.

[`in2`](/documentation/Accelerate/BNNSArithmeticTernary/in2)

The descriptor of the second input.

[`in2_type`](/documentation/Accelerate/BNNSArithmeticTernary/in2_type)

The descriptor type of the second input.

[`in3`](/documentation/Accelerate/BNNSArithmeticTernary/in3)

The descriptor of the third input.

[`in3_type`](/documentation/Accelerate/BNNSArithmeticTernary/in3_type)

The descriptor type of the third input.

[`out`](/documentation/Accelerate/BNNSArithmeticTernary/out)

The descriptor of the output.

[`out_type`](/documentation/Accelerate/BNNSArithmeticTernary/out_type)

The descriptor type of the output.



---

Copyright &copy; 2026 Apple Inc. All rights reserved. | [Terms of Use](https://www.apple.com/legal/internet-services/terms/site.html) | [Privacy Policy](https://www.apple.com/privacy/privacy-policy)