• AVR Freaks

AnsweredHot!How to make MPASM (et al.) convert a real number to a number format? e.g. Qm.f Fixed-point

Page: 12 > Showing page 1 of 2
Author
Oblivion
Starting Member
  • Total Posts : 54
  • Reward points : 0
  • Joined: 2008/10/05 00:08:00
  • Location: 0
  • Status: offline
2019/11/19 05:05:48 (permalink)
0

How to make MPASM (et al.) convert a real number to a number format? e.g. Qm.f Fixed-point

Hola,

TL;DR: How does one convince MPASM to take "3.14159265" in the code editor, and make 0x00 0x03 0x24 0x3F in PIC memory, in accordance with a known number format? (Q16.16 fixed-point here) (That is, of course, without involving C language, pencil and paper, or red candles and goat's blood.)
 
You can figure.
  1. I have the format. As shown, it's nothing more fancy than Q16.16 fixed-point.
  2. I can manually convert numbers in the real set to said format. This is very tedious and brittle.
  3. I can get the PIC to correctly smash said numbers together.
 
So then, how do I convince the ASM preprocessor to do part 2 for me, and save my sanity points? There has to be a way. My DuckDuckGo-kwondo failed me.
 
I could do this very easily if the preprocessor could work with real numbers. If I could even simply use [0.0,1.0) Then I'd already have a macro that would work, which I could call like so... (assuming f'nn' = float or fraction)
 
 u16q16_const var, D'3', f'0.14159265'


Please don't tell me I have to write some third party script/tool that does this for me. That would be silly. I would end up with a metric $#!7 ton of obtuse magic numbers in my source that would be quite hard to keep track of.
#1
NorthGuy
Super Member
  • Total Posts : 5817
  • Reward points : 0
  • Joined: 2014/02/23 14:23:23
  • Location: Northern Canada
  • Status: offline
Re: How to make MPASM (et al.) convert a real number to a number format? e.g. Qm.f Fixed-p 2019/11/19 07:55:56 (permalink) ☄ Helpfulby Oblivion 2019/11/19 11:02:56
3 (1)
Write a small program on PC to generate the desired numbers. I do this all the time. Or you can use Excel if you prefer.
#2
du00000001
Just Some Member
  • Total Posts : 3244
  • Reward points : 0
  • Joined: 2016/05/03 13:52:42
  • Location: Germany
  • Status: offline
Re: How to make MPASM (et al.) convert a real number to a number format? e.g. Qm.f Fixed-p 2019/11/19 08:01:06 (permalink)
0
I don't work with MPASM.
But isn't it able to create something useful from "3.14159265 * 65536" ?

PEBKAC / EBKAC / POBCAK / PICNIC (eventually see en.wikipedia.org)
#3
Gort2015
Klaatu Barada Nikto
  • Total Posts : 3370
  • Reward points : 0
  • Joined: 2015/04/30 10:49:57
  • Location: 0
  • Status: offline
Re: How to make MPASM (et al.) convert a real number to a number format? e.g. Qm.f Fixed-p 2019/11/19 08:21:46 (permalink)
0
Use the common Q1.15 and Q1.31 formats.
 
Align the float with Q1.15
A Q number is smaller than 1.0 so you cannot have 16.16
 
-0.75
 
1.110,  0000 : 0000, 0000
 
Float to Q1.5
0.5 * (2^15)
0.100,  0000 : 0000, 0000
 
 

MPLab X playing up, bug in your code? Nevermind, Star Trek:Discovery will be with us soon.
https://www.youtube.com/watch?v=Iu1qa8N2ID0
+ ST:Continues, "What Ships are Made for", Q's back.
#4
Oblivion
Starting Member
  • Total Posts : 54
  • Reward points : 0
  • Joined: 2008/10/05 00:08:00
  • Location: 0
  • Status: offline
Re: How to make MPASM (et al.) convert a real number to a number format? e.g. Qm.f Fixed-p 2019/11/19 08:23:14 (permalink)
0
Write a small program on PC to generate the desired numbers. I do this all the time. Or you can use Excel if you prefer.
 
If I did, I'd probably use Libre Calc; but is this seriously the only way? ... :sigh: ... I really don't want to do that.
 
But isn't it able to create something useful from "3.14159265 * 65536" ?
 
No, and this is precisely the problem.

I don't think you can, in any way, use numbers in the real set in macros/directives/preprocessor, even if the result is an integer. Even if the result is a 8-bit value.

e.g. All these would be the same number....
MOVLW    (31.5 * 2)
MOVLW    d'63'
MOVLW    0x3F

... but MPASM won't assemble that first one. "error 208 : Missing operator"
post edited by Oblivion - 2019/11/19 11:07:09
#5
Oblivion
Starting Member
  • Total Posts : 54
  • Reward points : 0
  • Joined: 2008/10/05 00:08:00
  • Location: 0
  • Status: offline
Re: How to make MPASM (et al.) convert a real number to a number format? e.g. Qm.f Fixed-p 2019/11/19 08:26:35 (permalink)
0
@Gort2015. We crossed posts here, so ... new info ... all is forgiven.

"Use the common Q1.15 and Q1.31 formats."

Format isn't the issue here, it's getting MPASM to accept a decimal with fractional part, period.
#6
Gort2015
Klaatu Barada Nikto
  • Total Posts : 3370
  • Reward points : 0
  • Joined: 2015/04/30 10:49:57
  • Location: 0
  • Status: offline
Re: How to make MPASM (et al.) convert a real number to a number format? e.g. Qm.f Fixed-p 2019/11/19 08:42:05 (permalink)
0
65536 would be too many powers of 2.
 
"3.14159265"
 
.14159265 x (2 ^ 15) = 0.001, 0010 : 0010, 0000
.14159265 x (2 ^ 31) = 0.001, 0010 : 0001, 1111 : 1011, 0101 : 0011, 1101
 
DSC pic has 40bit accumulators to store the 3 otherwise the integer part would be seperate.
post edited by Gort2015 - 2019/11/19 08:47:01

MPLab X playing up, bug in your code? Nevermind, Star Trek:Discovery will be with us soon.
https://www.youtube.com/watch?v=Iu1qa8N2ID0
+ ST:Continues, "What Ships are Made for", Q's back.
#7
NorthGuy
Super Member
  • Total Posts : 5817
  • Reward points : 0
  • Joined: 2014/02/23 14:23:23
  • Location: Northern Canada
  • Status: offline
Re: How to make MPASM (et al.) convert a real number to a number format? e.g. Qm.f Fixed-p 2019/11/19 09:02:42 (permalink) ☄ Helpfulby Oblivion 2019/11/19 10:38:48
3 (1)
Oblivion
If I did, I'd probably use Libre Calc; but is this seriously the only way? ... :sigh: ... I really don't want to do that.



Certainly the best one. If you have a bunch of numbers, they're probably generated by some sort of program already. You just modify the program to get the format you need.
#8
Oblivion
Starting Member
  • Total Posts : 54
  • Reward points : 0
  • Joined: 2008/10/05 00:08:00
  • Location: 0
  • Status: offline
Re: How to make MPASM (et al.) convert a real number to a number format? e.g. Qm.f Fixed-p 2019/11/19 09:09:27 (permalink)
5 (1)

#9
Gort2015
Klaatu Barada Nikto
  • Total Posts : 3370
  • Reward points : 0
  • Joined: 2015/04/30 10:49:57
  • Location: 0
  • Status: offline
Re: How to make MPASM (et al.) convert a real number to a number format? e.g. Qm.f Fixed-p 2019/11/19 09:19:15 (permalink)
0
Trig:
It's not that bad, calculate the 1st quadrant, the other 3 are derived from the first.
Use a spreadsheet.

MPLab X playing up, bug in your code? Nevermind, Star Trek:Discovery will be with us soon.
https://www.youtube.com/watch?v=Iu1qa8N2ID0
+ ST:Continues, "What Ships are Made for", Q's back.
#10
Oblivion
Starting Member
  • Total Posts : 54
  • Reward points : 0
  • Joined: 2008/10/05 00:08:00
  • Location: 0
  • Status: offline
Re: How to make MPASM (et al.) convert a real number to a number format? e.g. Qm.f Fixed-p 2019/11/19 09:20:47 (permalink)
0
... linear algebra.
#11
1and0
Access is Denied
  • Total Posts : 10005
  • Reward points : 0
  • Joined: 2007/05/06 12:03:20
  • Location: Harry's Gray Matter
  • Status: offline
Re: How to make MPASM (et al.) convert a real number to a number format? e.g. Qm.f Fixed-p 2019/11/19 09:50:26 (permalink) ☼ Best Answerby Oblivion 2019/11/19 10:25:16
5 (1)
MPASM does NOT support floating point assembly variables and operators.
 
Quoted from the MPASM User's Guide:

Intermediate values in constant expressions are treated as 32-bit unsigned
integers. Whenever an attempt is made to place a constant in a field for
which it is too large, a truncation warning will be issued.

#12
1and0
Access is Denied
  • Total Posts : 10005
  • Reward points : 0
  • Joined: 2007/05/06 12:03:20
  • Location: Harry's Gray Matter
  • Status: offline
Re: How to make MPASM (et al.) convert a real number to a number format? e.g. Qm.f Fixed-p 2019/11/19 09:53:48 (permalink) ☄ Helpfulby Oblivion 2019/11/19 10:28:10
5 (1)
Oblivion
e.g. All these would be the same number....
MOVLW    (31.5 * 2)
MOVLW    d'63'
MOVLW    0x3F

... but MPASM won't assemble that first one. "error 208 : Missing operator"

That is because the '.' is a prefix for decimal number in MPASM. ;)  You'd have to use this
MOVLW    (.315 * 2 / .10)

 
post edited by 1and0 - 2019/11/19 10:01:37
#13
1and0
Access is Denied
  • Total Posts : 10005
  • Reward points : 0
  • Joined: 2007/05/06 12:03:20
  • Location: Harry's Gray Matter
  • Status: offline
Re: How to make MPASM (et al.) convert a real number to a number format? e.g. Qm.f Fixed-p 2019/11/19 09:57:54 (permalink) ☄ Helpfulby Oblivion 2019/11/19 10:28:12
5 (1)
Gort2015
A Q number is smaller than 1.0 so you cannot have 16.16

Of course you can have Q numbers in Qm.n format with m integer bits and n fractional bits.
 
#14
Oblivion
Starting Member
  • Total Posts : 54
  • Reward points : 0
  • Joined: 2008/10/05 00:08:00
  • Location: 0
  • Status: offline
Re: How to make MPASM (et al.) convert a real number to a number format? e.g. Qm.f Fixed-p 2019/11/19 11:00:12 (permalink)
0
"MPASM does NOT support floating point assembly variables and operators."

That's really really really too bad.

<placeholder for even sadder face than before.>

Any kind of real number representation during the preprocessing stage would be unbelievably powerful for crafting macros that deal with float/fixed on the PIC. There are tons of tricks you can do to those numbers that general purpose math libs can't even dream of doing. Getting at the various fields internal to the number representation in a concise way is absolutely critical for setting up fast math, well.... fast.

:(
#15
1and0
Access is Denied
  • Total Posts : 10005
  • Reward points : 0
  • Joined: 2007/05/06 12:03:20
  • Location: Harry's Gray Matter
  • Status: offline
Re: How to make MPASM (et al.) convert a real number to a number format? e.g. Qm.f Fixed-p 2019/11/19 11:39:13 (permalink)
0
You can try submitting an enhancement request to Microchip, but I'm afraid it will fall on deaf ears, as you're not the first user who wanted this among greater than 32-bit integer supports.
#16
Oblivion
Starting Member
  • Total Posts : 54
  • Reward points : 0
  • Joined: 2008/10/05 00:08:00
  • Location: 0
  • Status: offline
Re: How to make MPASM (et al.) convert a real number to a number format? e.g. Qm.f Fixed-p 2019/11/19 12:13:43 (permalink)
5 (1)
" [...] I'm afraid it will fall on deaf ears"

As I'm sure you can guess, I am quite painfully aware. Not worth the email paper. :(

To be fair, it's not like this is/was the final straw ... it's been getting closer and closer to time to toss out my MC dev stuff and get some ST dev stuff instead.

(It was time to jump ship long ago. After 12+ years with MC, it's gotten hard to change, but that's totally my fault. I've just had this naive wish/hope that MC would pull the head out. But, that's just not going to happen. Boy does that grass get greener and greener all the time. I foresee myself porting a lot of code in my near future. Job security?)

BTW: Thanks for all the help friends.
post edited by Oblivion - 2019/11/19 12:14:53
#17
1and0
Access is Denied
  • Total Posts : 10005
  • Reward points : 0
  • Joined: 2007/05/06 12:03:20
  • Location: Harry's Gray Matter
  • Status: offline
Re: How to make MPASM (et al.) convert a real number to a number format? e.g. Qm.f Fixed-p 2019/11/19 12:24:47 (permalink) ☄ Helpfulby Oblivion 2019/11/20 00:06:18
0
... or you can try Olin's MPASM preprocessor PREPIC:  https://www.microchip.com/forums/FindPost/378769
 
Disclaimer: I have not used it before.
#18
Gort2015
Klaatu Barada Nikto
  • Total Posts : 3370
  • Reward points : 0
  • Joined: 2015/04/30 10:49:57
  • Location: 0
  • Status: offline
Re: How to make MPASM (et al.) convert a real number to a number format? e.g. Qm.f Fixed-p 2019/11/19 14:31:21 (permalink)
0
1and0
Gort2015
A Q number is smaller than 1.0 so you cannot have 16.16

Of course you can have Q numbers in Qm.n format with m integer bits and n fractional bits.
 

Anything greater than 1.0 won't be compatible with the C compiler that uses Q1.15 and Q1.31 formats for fixed floats.
 
Keeping to that format, it's easy to make a sine table stored in Q1.15 format.
 
The 16bit assembler supports double, fixed and float directives btw.
 
 



 

MPLab X playing up, bug in your code? Nevermind, Star Trek:Discovery will be with us soon.
https://www.youtube.com/watch?v=Iu1qa8N2ID0
+ ST:Continues, "What Ships are Made for", Q's back.
#19
1and0
Access is Denied
  • Total Posts : 10005
  • Reward points : 0
  • Joined: 2007/05/06 12:03:20
  • Location: Harry's Gray Matter
  • Status: offline
Re: How to make MPASM (et al.) convert a real number to a number format? e.g. Qm.f Fixed-p 2019/11/19 16:32:21 (permalink) ☄ Helpfulby Oblivion 2019/11/20 00:05:49
4 (1)
Gort2015Anything greater than 1.0 won't be compatible with the C compiler that uses Q1.15 and Q1.31 formats for fixed floats.

 Keeping to that format, it's easy to make a sine table stored in Q1.15 format. 

The 16bit assembler supports double, fixed and float directives btw.  

This is the MPASM forum, where anything and everything that are C compiler or 16-bit assembler are not helpful and not relevant advices.
#20
Page: 12 > Showing page 1 of 2
Jump to:
© 2019 APG vNext Commercial Version 4.5