LCOV - code coverage report
Current view: top level - base_comm/resources/ccu/ccu_representation/reps/arithmetic - ccu_rep_sub.cc (source / functions) Coverage Total Hit
Test: coverage.info Lines: 97.7 % 130 127
Test Date: 2026-08-17 10:19:35 Functions: 95.5 % 22 21

            Line data    Source code
       1              : /**
       2              :  * Copyright (c) 2026 Huawei Technologies Co., Ltd.
       3              :  * This program is free software, you can redistribute it and/or modify it under the terms and conditions of
       4              :  * CANN Open Software License Agreement Version 2.0 (the "License").
       5              :  * Please refer to the License for details. You may not use this file except in compliance with the License.
       6              :  * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, EITHER EXPRESS OR IMPLIED,
       7              :  * INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, MERCHANTABILITY, OR FITNESS FOR A PARTICULAR PURPOSE.
       8              :  * See LICENSE in the root of the software repository for the full text of the License.
       9              :  */
      10              : 
      11              : #include "ccu_rep_v1.h"
      12              : #include "ccu_ins_generator_v1.h"
      13              : #include "string_util.h"
      14              : #include "exception_util.h"
      15              : #include "ccu_api_exception.h"
      16              : #include "ccu_kernel.h"
      17              : 
      18              : namespace hcomm {
      19              : namespace CcuRep {
      20              : 
      21           35 :     void CcuRepSub::SetCommonInfo()
      22              :     {
      23           35 :         type = CcuRepType::SUB;
      24           35 :         instrCount = insGenPtr->GetInstrCount(type);
      25           35 :     }
      26              : 
      27            7 :     CcuRepSub::CcuRepSub(
      28            7 :         CcuInsGeneratorBase* insGenPtr, const Variable& varC, const Variable& varA, const Variable& varB)
      29            7 :         : insGenPtr(insGenPtr),
      30            7 :           subType(MinusSubType::VAR_MINUS_VAR_TO_VAR),
      31            7 :           varA(varA),
      32            7 :           varB(varB),
      33           14 :           varC(varC)
      34              :     {
      35            7 :         SetCommonInfo();
      36            7 :     }
      37              : 
      38            5 :     CcuRepSub::CcuRepSub(CcuInsGeneratorBase* insGenPtr, const Variable& varC, const Variable& varA, uint16_t immedB)
      39            5 :         : insGenPtr(insGenPtr),
      40            5 :           subType(MinusSubType::VAR_MINUS_IMMED_TO_VAR),
      41            5 :           varA(varA),
      42            5 :           varC(varC),
      43           10 :           immedB(immedB)
      44              :     {
      45            5 :         SetCommonInfo();
      46            5 :     }
      47              : 
      48            3 :     CcuRepSub::CcuRepSub(CcuInsGeneratorBase* insGenPtr, const Variable& varA, const Variable& varB)
      49            3 :         : insGenPtr(insGenPtr),
      50            3 :           subType(MinusSubType::SELF_SUB_VAR_VARIABLE),
      51            3 :           varA(varA),
      52            3 :           varB(varB)
      53              :     {
      54            3 :         SetCommonInfo();
      55            3 :     }
      56              : 
      57            3 :     CcuRepSub::CcuRepSub(CcuInsGeneratorBase* insGenPtr, const Variable& varA, uint16_t immedB)
      58            3 :         : insGenPtr(insGenPtr),
      59            3 :           subType(MinusSubType::SELF_SUB_IMMED_VARIABLE),
      60            3 :           varA(varA),
      61            3 :           immedB(immedB)
      62              :     {
      63            3 :         SetCommonInfo();
      64            3 :     }
      65              : 
      66            4 :     CcuRepSub::CcuRepSub(
      67            4 :         CcuInsGeneratorBase* insGenPtr, const Address& addrC, const Address& addrA, const Variable& varB)
      68            4 :         : insGenPtr(insGenPtr),
      69            4 :           subType(MinusSubType::ADDR_MINUS_VAR_TO_ADDR),
      70            4 :           varB(varB),
      71            4 :           addrA(addrA),
      72           12 :           addrC(addrC)
      73              :     {
      74            4 :         SetCommonInfo();
      75            4 :     }
      76              : 
      77            2 :     CcuRepSub::CcuRepSub(
      78            2 :         CcuInsGeneratorBase* insGenPtr, const Address& addrC, const Address& addrA, const uint16_t immedB)
      79            2 :         : insGenPtr(insGenPtr),
      80            2 :           subType(MinusSubType::ADDR_MINUS_IMMED_TO_ADDR),
      81            2 :           addrA(addrA),
      82            2 :           addrC(addrC),
      83            4 :           immedB(immedB)
      84              :     {
      85            2 :         SetCommonInfo();
      86            2 :     }
      87              : 
      88            3 :     CcuRepSub::CcuRepSub(CcuInsGeneratorBase* insGenPtr, const Address& addrA, const Variable& varB)
      89            3 :         : insGenPtr(insGenPtr),
      90            3 :           subType(MinusSubType::SELF_SUB_VAR_ADDRESS),
      91            3 :           varB(varB),
      92            6 :           addrA(addrA)
      93              :     {
      94            3 :         SetCommonInfo();
      95            3 :     }
      96              : 
      97            3 :     CcuRepSub::CcuRepSub(CcuInsGeneratorBase* insGenPtr, const Address& addrA, const uint16_t immedB)
      98            3 :         : insGenPtr(insGenPtr),
      99            3 :           subType(MinusSubType::SELF_SUB_IMMED_ADDRESS),
     100            3 :           addrA(addrA),
     101            6 :           immedB(immedB)
     102              :     {
     103            3 :         SetCommonInfo();
     104            3 :     }
     105              : 
     106            2 :     CcuRepSub::CcuRepSub(
     107            2 :         CcuInsGeneratorBase* insGenPtr, const Address& addrC, const Variable& varA, const uint16_t immedB)
     108            2 :         : insGenPtr(insGenPtr),
     109            2 :           subType(MinusSubType::VAR_MINUS_IMMED_TO_ADDR),
     110            2 :           varA(varA),
     111            2 :           addrC(addrC),
     112            2 :           immedB(immedB)
     113              :     {
     114            2 :         SetCommonInfo();
     115            2 :     }
     116            3 :     CcuRepSub::CcuRepSub(
     117            3 :         CcuInsGeneratorBase* insGenPtr, const Variable& varC, const Address& addrA, const uint16_t immedB)
     118            3 :         : insGenPtr(insGenPtr),
     119            3 :           subType(MinusSubType::ADDR_MINUS_IMMED_TO_VAR),
     120            3 :           varC(varC),
     121            3 :           addrA(addrA),
     122            6 :           immedB(immedB)
     123              :     {
     124            3 :         SetCommonInfo();
     125            3 :     }
     126              : 
     127            8 :     void CcuRepSub::ValidateInsGenPtrForSub()
     128              :     {
     129            8 :         CcuInsGeneratorV1* tmpPtrV1 = dynamic_cast<CcuInsGeneratorV1*>(insGenPtr);
     130            8 :         CHK_PRT_THROW(
     131              :             (tmpPtrV1 && !supportCcuV1),
     132              :             HCCL_ERROR("[CcuRepSub][%s]Cannot translate CcuRepSub for A5 when supportCcuV1 is false", __func__),
     133              :             Hccl::CcuApiException, "tmpPtrV1 does not match supportCcuV1");
     134            8 :     }
     135              : 
     136            8 :     bool CcuRepSub::Translate(CcuKernel* ccuKernel, CcuInstr*& instr, uint16_t& instrId, const TransDep& dep)
     137              :     {
     138            8 :         ValidateInsGenPtrForSub();
     139            8 :         Hccl::CHECK_NULLPTR(instr, "[CcuRepSub::Translate] instr is nullptr!");
     140            8 :         this->instrId = instrId;
     141            8 :         translated = true;
     142              : 
     143            8 :         CHK_PRT_THROW(
     144              :             insGenPtr->CcuRepSubTranslate(ccuKernel, instr, this) != HcclResult::HCCL_SUCCESS,
     145              :             HCCL_ERROR("[CcuRepSub][Translate] failed to translate for instrId[%u]", instrId), Hccl::CcuApiException,
     146              :             "CcuRepSub translate failed");
     147              : 
     148            8 :         CHK_PRT_THROW(
     149              :             (instrId > UINT16_MAX - instrCount),
     150              :             HCCL_ERROR(
     151              :                 "[CcuRepSub::Translate]uint16 integer overflow occurs, instrId = [%hu], instrCount = [%hu]", instrId,
     152              :                 instrCount),
     153              :             Hccl::InternalException, "integer overflow");
     154            8 :         instrId += instrCount;
     155              : 
     156            8 :         return translated;
     157              :     }
     158              : 
     159           14 :     std::string CcuRepSub::Describe()
     160              :     {
     161           14 :         switch (subType) {
     162            3 :             case MinusSubType::VAR_MINUS_VAR_TO_VAR: {
     163              :                 return Hccl::StringFormat(
     164            3 :                     "Variable[%u] = Variable[%u] - Variable[%u]", varC.Id(), varA.Id(), varB.Id());
     165              :             }
     166            3 :             case MinusSubType::VAR_MINUS_IMMED_TO_VAR: {
     167            3 :                 return Hccl::StringFormat("Variable[%u] = Variable[%u] - Immed[%u]", varC.Id(), varA.Id(), immedB);
     168              :             }
     169            1 :             case MinusSubType::SELF_SUB_VAR_VARIABLE: {
     170            1 :                 return Hccl::StringFormat("Variable[%u] -= Variable[%u]", varA.Id(), varB.Id());
     171              :             }
     172            1 :             case MinusSubType::SELF_SUB_IMMED_VARIABLE: {
     173            1 :                 return Hccl::StringFormat("Variable[%u] -= Immed[%u]", varA.Id(), immedB);
     174              :             }
     175            1 :             case MinusSubType::ADDR_MINUS_VAR_TO_ADDR: {
     176              :                 return Hccl::StringFormat(
     177            1 :                     "address[%u] = address[%u] - Variable[%u]", addrC.Id(), addrA.Id(), varB.Id());
     178              :             }
     179            1 :             case MinusSubType::ADDR_MINUS_IMMED_TO_ADDR: {
     180            1 :                 return Hccl::StringFormat("address[%u] = address[%u] - Immed[%u]", addrC.Id(), addrA.Id(), immedB);
     181              :             }
     182            1 :             case MinusSubType::SELF_SUB_VAR_ADDRESS: {
     183            1 :                 return Hccl::StringFormat("address[%u] -= Variable[%u]", addrA.Id(), varB.Id());
     184              :             }
     185            1 :             case MinusSubType::SELF_SUB_IMMED_ADDRESS: {
     186            1 :                 return Hccl::StringFormat("address[%u] -= Immed[%u]", addrA.Id(), immedB);
     187              :             }
     188            1 :             case MinusSubType::VAR_MINUS_IMMED_TO_ADDR: {
     189            1 :                 return Hccl::StringFormat("address[%u] = Variable[%u] - Immed[%u]", addrC.Id(), varA.Id(), immedB);
     190              :             }
     191            1 :             case MinusSubType::ADDR_MINUS_IMMED_TO_VAR: {
     192            1 :                 return Hccl::StringFormat("Variable[%u] = address[%u] - Immed[%u]", varC.Id(), addrA.Id(), immedB);
     193              :             }
     194            0 :             default: {
     195            0 :                 return Hccl::StringFormat("Invalid Sub");
     196              :             }
     197              :         }
     198              :     }
     199              : 
     200            5 :     Address CcuRepSub::GetAddrA() { return addrA; }
     201              : 
     202            0 :     Address CcuRepSub::GetAddrB() { return addrB; }
     203              : 
     204            2 :     Address CcuRepSub::GetAddrC() { return addrC; }
     205              : 
     206            6 :     Variable CcuRepSub::GetVarA() { return varA; }
     207              : 
     208            7 :     Variable CcuRepSub::GetVarB() { return varB; }
     209              : 
     210            7 :     Variable CcuRepSub::GetVarC() { return varC; }
     211              : 
     212            4 :     uint16_t CcuRepSub::GetImmedB() { return immedB; }
     213              : 
     214           18 :     MinusSubType CcuRepSub::GetSubType() { return subType; }
     215              : }; // namespace CcuRep
     216              : }; // namespace hcomm
        

Generated by: LCOV version 2.0-1