Carry and overflow flags after addition. Are they accessible?

Tip / Sign in to post questions, reply, level up, and achieve exciting badges. Know more

cross mob
lock attach
Attachments are accessible only for community members.
GaRo_3769926
Level 2
Level 2
First like given

Good afternoon

I'm trying to add two int32 variables on PSoC 4 as fast as possible. Since floats are very slow operation-wise, and apparently int64s are emulated and slow as well, I've considered adding both int32s into two separate variables: a least significant int32 that gets the 32 last bits of the result of the sum (since overflows work as the sum needs them to work), and a most significant int32 that gets summed/diminished by the carry/overflow bit.

I've tried to have access to those flags, since according to the PSoC 4 Architecture Technical Reference Manual (page 21 of the attached file) they exist on Cortex-M0.

I think I've come close to finding the solution, since I've found that in the generated source files of any PSoC4 files, inside the cy_boot, there's a "core_cm0.h" header file in which there's a struct inside a union type, called "APSR_Type" (lines 195-213 of the core-cm0 file) in which the carry and overflow flags appear. I've also attached the core-cm0 file just in case.

I'd love any information on whether it's possible to actually have access to those flags after making an addition, since it would be the perfect solution to my issue with slow addition. Any other ideas on how to add those int32s as fast as possible would also be welcome.

Thanks for your time reading this post, have a good day

Gabriel

0 Likes
1 Solution
lock attach
Attachments are accessible only for community members.
MotooTanaka
Level 9
Level 9
Distributor - Marubun (Japan)
First comment on blog Beta tester First comment on KBA

Hi,

So we need to access xPSR register.

At first I thought that I need to use "msr" asm but playing with PSoC Creator

I found that __get_xPSR() will do what we need.

So I wrote a function

====================

void do_test(void)

{

    int32_t a, b, sum ;

    uint32_t reg_value ;

  

    sscanf(str, "%d %d", &a, &b) ;

    sum = a + b ;

    reg_value = __get_xPSR() ;

    snprintf(str, STR_BUF_LEN, "%d + %d = %d : ", a, b, sum) ;

    print(str) ;

    snprintf(str, STR_BUF_LEN, "0x%08X : ", reg_value) ;

    print(str) ;

    if (reg_value & NEGATIVE_BIT) {

        print("N") ;

    }

    if (reg_value & ZERO_BIT) {

        print("Z") ;

    }

    if (reg_value & CARRY_BIT) {

        print("C") ;

    }

    if (reg_value & OVER_FLOW_BIT) {

        print("O") ;

    }

    print("\n") ;

}

====================

TeraTerm log

000-TeraTerm-log.JPG

Attached is my test sample for CY8CKIT-044.

moto

View solution in original post

3 Replies
lock attach
Attachments are accessible only for community members.
MotooTanaka
Level 9
Level 9
Distributor - Marubun (Japan)
First comment on blog Beta tester First comment on KBA

Hi,

So we need to access xPSR register.

At first I thought that I need to use "msr" asm but playing with PSoC Creator

I found that __get_xPSR() will do what we need.

So I wrote a function

====================

void do_test(void)

{

    int32_t a, b, sum ;

    uint32_t reg_value ;

  

    sscanf(str, "%d %d", &a, &b) ;

    sum = a + b ;

    reg_value = __get_xPSR() ;

    snprintf(str, STR_BUF_LEN, "%d + %d = %d : ", a, b, sum) ;

    print(str) ;

    snprintf(str, STR_BUF_LEN, "0x%08X : ", reg_value) ;

    print(str) ;

    if (reg_value & NEGATIVE_BIT) {

        print("N") ;

    }

    if (reg_value & ZERO_BIT) {

        print("Z") ;

    }

    if (reg_value & CARRY_BIT) {

        print("C") ;

    }

    if (reg_value & OVER_FLOW_BIT) {

        print("O") ;

    }

    print("\n") ;

}

====================

TeraTerm log

000-TeraTerm-log.JPG

Attached is my test sample for CY8CKIT-044.

moto

Hello Motoo

I didn't know those functions even existed. That's so useful! Thank you so much! Where did you find the documentation in which those functions appear? I'd love to see the document and the description of the functions, and maybe even find more useful functions.

Thanks again

Gabriel

0 Likes
MotooTanaka
Level 9
Level 9
Distributor - Marubun (Japan)
First comment on blog Beta tester First comment on KBA

Dear Gabriel-san,

I referenced the Arm's cortex-m0 manual, since those flags are in the core.

But in your attached header, xPSR was defined as bit-field type struct

so I just tried to find if there is/are function or definition of xPSR in the .h file,

then PSoC Creator showed that function in the suggestion pull-down menu.

Otherwise I was trying to use "asm {" directive

and use msr (Move from Special Register).

Anyway, I'm glad hearing that the information is useful 😉

Best Regards,

12-Nov-2019

Motoo Tanaka

0 Likes