• AVR Freaks

Helpful ReplyHot!C18 48bit addition to form kWh counter. (from page 3)

Page: < 12345.. > >> Showing page 3 of 6
Author
ric
Super Member
  • Total Posts : 28361
  • Reward points : 0
  • Joined: 2003/11/07 12:41:26
  • Location: Australia, Melbourne
  • Status: online
Re: 64bit addition. 2020/06/13 01:13:45 (permalink)
+2 (2)
So do you want to add two arbitrary 40 bit numbers,
add a 16 bit value to a 40 bit accumulator,
or just increment a 40 bit counter?

I also post at: PicForum
Links to useful PIC information: http://picforum.ric323.co...opic.php?f=59&t=15
NEW USERS: Posting images, links and code - workaround for restrictions.
To get a useful answer, always state which PIC you are using!
#41
andy_n
Senior Member
  • Total Posts : 155
  • Reward points : 0
  • Status: offline
Re: 64bit addition. 2020/06/13 01:27:10 (permalink)
0
 
How do I get a 40 bit accumulator with a PIC18?
#42
ric
Super Member
  • Total Posts : 28361
  • Reward points : 0
  • Joined: 2003/11/07 12:41:26
  • Location: Australia, Melbourne
  • Status: online
Re: 64bit addition. 2020/06/13 01:32:24 (permalink)
+2 (2)
You have to create it in code yourself.
You had the right idea in one of the first few posts that you deleted. i.e. a structure with a 32 bit and an 8 bit.
I know you hate answering questions, but could you please answer my last query, about what you actually want to add?
 

I also post at: PicForum
Links to useful PIC information: http://picforum.ric323.co...opic.php?f=59&t=15
NEW USERS: Posting images, links and code - workaround for restrictions.
To get a useful answer, always state which PIC you are using!
#43
Ken_Pergola
Super Member
  • Total Posts : 2245
  • Reward points : 0
  • Joined: 2003/11/07 12:48:48
  • Status: offline
Re: 64bit addition. 2020/06/13 01:33:25 (permalink)
+1 (1)
Hi Andy,
 
It's late in this part of the world so my last post (sleepy) -- I'm not bailing out. But I don't get in these forums as often as I used to due to time pressures.
 
I agree with Ric that we need some more details from you.
Let's forget about how to solve it right now and focus on the requirements as Ric is alluding to.
I apologize that these questions are essentially reiterating what Ric is asking:
1) What are you keeping track of?
2) Every second what specifically are you adding?
3) What is the maximum years you must keep track of what you are tracking? 
4) What is the highest sum that is acceptable?
 
Any specific examples you can provide would probably better allow all of us to help you out.
 
Best regards,
 
Ken
 
P.S. Thanks Ric for your help.
 
 
 
#44
andy_n
Senior Member
  • Total Posts : 155
  • Reward points : 0
  • Status: offline
Re: 64bit addition. 2020/06/13 01:43:55 (permalink)
0
 
ok. Understand:
 
With a PIC18 and C18 I want to add 16bit uint to 48bit uint (40 bit is not big enough)
 
 
#45
1and0
Access is Denied
  • Total Posts : 11129
  • Reward points : 0
  • Joined: 2007/05/06 12:03:20
  • Location: Harry's Gray Matter
  • Status: offline
Re: 64bit addition. 2020/06/13 01:55:47 (permalink) ☄ Helpfulby andy_n 2020/06/13 02:04:36
+3 (3)
andy_n
With a PIC18 and C18 I want to add 16bit uint to 48bit uint (40 bit is not big enough)

Something likes this:
typedef struct {
    uint16_t lo;
    uint32_t hi;
} uint48_t;

 

uint48_t add48(uint48_t addend1, uint16_t addend2)
{
    addend1.lo += addend2;
    if (addend1.lo < addend2)
        addend1.hi++;
    return addend1;
}


 
#46
andy_n
Senior Member
  • Total Posts : 155
  • Reward points : 0
  • Status: offline
Re: 64bit addition. 2020/06/13 02:02:38 (permalink)
0
Thanks 1and0. 
#47
ric
Super Member
  • Total Posts : 28361
  • Reward points : 0
  • Joined: 2003/11/07 12:41:26
  • Location: Australia, Melbourne
  • Status: online
Re: 64bit addition. 2020/06/13 02:04:22 (permalink)
+1 (1)
I think you've almost got it, although I would pass a pointer to the structure to the function.
void add48(uint48_t * addend1, uint16_t addend2)
{
    addend1->lo += addend2;
    if (addend1->lo < addend2)
        addend1->hi++;
}

or, if there was only ever one of these structures, just make it global and access it directly

I also post at: PicForum
Links to useful PIC information: http://picforum.ric323.co...opic.php?f=59&t=15
NEW USERS: Posting images, links and code - workaround for restrictions.
To get a useful answer, always state which PIC you are using!
#48
1and0
Access is Denied
  • Total Posts : 11129
  • Reward points : 0
  • Joined: 2007/05/06 12:03:20
  • Location: Harry's Gray Matter
  • Status: offline
Re: 64bit addition. 2020/06/13 02:11:25 (permalink)
0
ric
I think you've almost got it, although I would pass a pointer to the structure to the function.

That works too and maybe even more efficient. It's small am hours here. ;)
 
#49
ric
Super Member
  • Total Posts : 28361
  • Reward points : 0
  • Joined: 2003/11/07 12:41:26
  • Location: Australia, Melbourne
  • Status: online
Re: 64bit addition. 2020/06/13 02:16:08 (permalink)
0
Early evening here.
I'm just about to go out to dinner with some friends, now restrictions have finally been reduced to the point where we are allowed to again.

I also post at: PicForum
Links to useful PIC information: http://picforum.ric323.co...opic.php?f=59&t=15
NEW USERS: Posting images, links and code - workaround for restrictions.
To get a useful answer, always state which PIC you are using!
#50
marcov
Super Member
  • Total Posts : 283
  • Reward points : 0
  • Joined: 2006/10/08 01:59:40
  • Location: Eindhoven, NL.
  • Status: offline
Re: 64bit addition. 2020/06/13 11:50:25 (permalink)
+1 (1)
Addition/subtraction is usually a simple case.  If the architecture defines it for wordsize "n", just look at what the compiler does for any size larger than n and unroll this a few times extra.
#51
andy_n
Senior Member
  • Total Posts : 155
  • Reward points : 0
  • Status: offline
Re: 64bit addition. 2020/06/13 15:26:05 (permalink)
0
Thanks marcov for answer.
Multibyte addition in assembler is simple but I couldn't find a solution for C.
 
Wouldn't it be more elegant to use STATUSbits?
 
    addend1.lo += addend2;
    if (STATUSbits.C) addend1.hi++;
 
I tried it. It seems to work and saves 32 bytes of ROM.
Thanks for inspiration.
 
#52
ric
Super Member
  • Total Posts : 28361
  • Reward points : 0
  • Joined: 2003/11/07 12:41:26
  • Location: Australia, Melbourne
  • Status: online
Re: 64bit addition. 2020/06/13 15:49:49 (permalink)
+1 (1)
andy_n
Wouldn't it be more elegant to use STATUSbits?

STATUS is a CPU register reserved for use byt the compiler.
There is no guarantee it still contains the result of the addition when you do the test.
It might work now, and fail in a future compiler release.
(Edit: Granted, if you're sticking with an ancient copy of C18, then "a future compiler release" isn't a variable...)
 

I tried it. It seems to work and saves 32 bytes of ROM.

32 bytes, so 16 instructions on a PIC16F?
Which version of the function are you using? Passing the value as a parameter, or making it a global variable?
 
post edited by ric - 2020/06/13 15:53:36

I also post at: PicForum
Links to useful PIC information: http://picforum.ric323.co...opic.php?f=59&t=15
NEW USERS: Posting images, links and code - workaround for restrictions.
To get a useful answer, always state which PIC you are using!
#53
Ken_Pergola
Super Member
  • Total Posts : 2245
  • Reward points : 0
  • Joined: 2003/11/07 12:48:48
  • Status: offline
Re: 64bit addition. 2020/06/13 16:00:13 (permalink)
+2 (2)
Hello Andy and good day,
 
Going back to requirements:
 
1) Do you need to detect overflow on the 48-bit sum? (Given how many years the addition will take place every second.)
 
I don't have C18 installed on my machine. And a quick PDF search of a C18 User's Guide I found came up empty.
2) Does C18 support <stdint.h>? Can you find this file -- stdint.h?
 
It would still be helpful knowing what you are keeping track of. It may lead to different solutions the overall high-level problem.
 
Best regards,
 
Ken
 
P.S. Thanks to everyone for helping out and driving this thread towards a more positive direction.
 
 
#54
1and0
Access is Denied
  • Total Posts : 11129
  • Reward points : 0
  • Joined: 2007/05/06 12:03:20
  • Location: Harry's Gray Matter
  • Status: offline
Re: 64bit addition. 2020/06/13 16:06:09 (permalink)
+2 (2)
andy_n
Wouldn't it be more elegant to use STATUSbits?
 
    addend1.lo += addend2;
    if (STATUSbits.C) addend1.hi++;
 
I tried it. It seems to work and saves 32 bytes of ROM.

On a PIC18 device, this
if (STATUSbits.C)

takes two instructions
btfss   STATUS,C
bra     end_if

and this
if (addend1.lo < addend2)

takes six instructions
movf    addend2+0,w
subwf   addend1.lo+0,w
movf    addend2+1,w
subwfb  addend1.lo+1,w
btfsc   STATUS,C
bra     end_if

so that is a difference of only 4 instruction words (or 8 bytes), not 32 bytes. Besides, as Ric has described, there is no guarantee testing the Carry bit will work in C.
 
Edit:  Actually BTFSS STATUS,C/BRA and BTFSC STATUS,C/BRA can be one instruction BNC and BC, respectively.
 
post edited by 1and0 - 2020/06/13 16:40:25
#55
1and0
Access is Denied
  • Total Posts : 11129
  • Reward points : 0
  • Joined: 2007/05/06 12:03:20
  • Location: Harry's Gray Matter
  • Status: offline
Re: 64bit addition. 2020/06/13 16:27:25 (permalink)
+1 (1)
Ken_Pergola
I don't have C18 installed on my machine. And a quick PDF search of a C18 User's Guide I found came up empty.
2) Does C18 support <stdint.h>? Can you find this file -- stdint.h?

I've never used C18 but if I recall correctly, C18 does not have <stdint.h> but it does have <GenericTypeDefs.h> which typedefs UINT16, UINT32, etc.
 
 
#56
jtemples
عُضْوٌ جَدِيد
  • Total Posts : 11982
  • Reward points : 0
  • Joined: 2004/02/13 12:31:19
  • Location: Southern California
  • Status: offline
Re: 64bit addition. 2020/06/13 17:52:55 (permalink)
+1 (1)
Does C18 support <stdint.h>

 
It doesn't support any C99 features.  But of course it's easy to roll your own stdint.h (or just copy one from another compiler).
#57
Ken_Pergola
Super Member
  • Total Posts : 2245
  • Reward points : 0
  • Joined: 2003/11/07 12:48:48
  • Status: offline
Re: 64bit addition. 2020/06/13 18:29:42 (permalink)
0
Thanks guys. I should have elaborated. I'm heading down the road of pointing out the limitations of C18 as others in this thread have brought up earlier.
 
Unless this chart is old and stale it C18 neither supports 'long long' (64-bits) nor any native type greater than 32-bits so Andy is constrained by staying with C18 versus migrating to XC8 -- so he can't use those snippets posted earlier which used 'uint48_t' as C18 does not have 48-bit native type support (but I don't have visibility into the <GenericTypeDefs.h> file).
 
 
 
I understand the reluctance for him to migrate as will require resources (maybe little, maybe a lot) to "port" from C18 to XC8 but it might be worth the effort -- sometimes there is a huge return on investment.
 
Unless I am off-base, Andy will need to write some brand new code to "spoof" larger data types if he is to stay with C18. John, please correct me if I am giving bad information here.
 
Best regards,
 
Ken
 
P.S. Yikes, sorry about the raw image mishap. Uploading a .png...
post edited by Ken_Pergola - 2020/06/13 18:34:01

Attached Image(s)

#58
1and0
Access is Denied
  • Total Posts : 11129
  • Reward points : 0
  • Joined: 2007/05/06 12:03:20
  • Location: Harry's Gray Matter
  • Status: offline
Re: 64bit addition. 2020/06/13 21:08:15 (permalink)
+1 (1)
Ken_Pergola
Unless this chart is old and stale it C18 neither supports 'long long' (64-bits) nor any native type greater than 32-bits so Andy is constrained by staying with C18 versus migrating to XC8 -- so he can't use those snippets posted earlier which used 'uint48_t' as C18 does not have 48-bit native type support (but I don't have visibility into the <GenericTypeDefs.h> file).

Well, I knew that when I posted the snippet and hoped OP has the knowledge to do this:

typedef unsigned int uint16_t;
typedef unsigned long uint32_t;

or replace with C18 data types. Otherwise, OP would complaint errors instead commented the snippets work in his Post #52. ;)
#59
Ken_Pergola
Super Member
  • Total Posts : 2245
  • Reward points : 0
  • Joined: 2003/11/07 12:48:48
  • Status: offline
Re: 64bit addition. 2020/06/13 21:43:57 (permalink)
0
Hello and thank you 1and0,
 
I apologize -- I "fell asleep at the wheel" and did not study the snippets closely enough -- my mistake.
 
(I try to help with the risk of making mistakes. I'd rather try to help and make mistakes than to not try to help at all. I have been working with 32-bit parts for a while and the last 8-bit project I used XC8 so I have been spoiled by not working with resource-constrained MCUs as of late. It's been ages since I used C18 to remember some of its limitations.)
 
Thank you very much 1and0 for your time chipping in to help Andy, and also for your patience with me.
 
Best regards and have a great rest of the weekend,
 
Ken
post edited by Ken_Pergola - 2020/06/13 21:46:00
#60
Page: < 12345.. > >> Showing page 3 of 6
Jump to:
© 2020 APG vNext Commercial Version 4.5