How to make MPASM (et al.) convert a real number to a number format? e.g. Qm.f Fixed-pointhttps://www.microchip.com/forums/(c) Microchip30Re: How to make MPASM (et al.) convert a real number to a number format? e.g. Qm.f Fixed-point (HolyPaps)Hello,<br/>I don't know if this still could be useful for you, but coincidentally a month ago I made a MPASM macro for carrying out "Real numbers to IEEE574" conversion. Using this macro, you can initialize, for example, the variable myvar with the number 3.14159265 as @FLOAT 3,14159265 ,8,myvar.... Does all the calculations in compiling time, generating only 8 lines of code max.<br/> <br/>@FLOAT MACRO int,frac,nfrac,f ;Single Precision Float initializer<br> VARIABLE float = 0 ;Stores IEEE754 representation of int.frac in f:f+3<br> IF int || frac ;nfrac = number of decimal digits. Rounds up LS bit.<br> VARIABLE a = int, c = frac, d = nfrac <br> VARIABLE n = 1, i = 0, exp = 127<br> IF a < 0<br> VARIABLE a = -a <br> VARIABLE exp = exp | (1 << 8)<br> ENDIF<br> IF c && d<br> WHILE d<br> VARIABLE n = n*10<br> VARIABLE d = d - 1<br> ENDW<br> IF !a<br> VARIABLE exp = exp - 1<br> WHILE !((c * 2) / n)<br> VARIABLE exp = exp - 1<br> VARIABLE c = c*2<br> ENDW<br> VARIABLE c = (c * 2) % n<br> ENDIF<br> WHILE c && i < 23<br> IF (c * 2)/ n<br> VARIABLE float = float | (1 << (22 - i))<br> ENDIF<br> VARIABLE c = (c * 2) % n<br> VARIABLE i = i + 1<br> ENDW<br> VARIABLE i = ((c * 2) / n != 0)<br> ENDIF <br> WHILE a > 1<br> VARIABLE i = float & 1<br> VARIABLE float = float >> 1 | (a & 1) << 22<br> VARIABLE a = a >> 1<br> VARIABLE exp = exp + 1<br> ENDW<br> VARIABLE float = (exp << 23 | float) + i ;Round-up<br> ENDIF<br> @MOVLF float,f,3<br> ENDM<br/> <br/>@MOVLF MACRO k,f,n ;Moves up to 32-bit literal to little-endian RAM addr.<br> VARIABLE i = n ;"k" cannot be an address label on relocatable mode.<br> WHILE i+1<br> IF (k >> 8*i) & 0xFF<br> IF i == n || (((k >> 8*i) & 0xFF) != ((k >> 8*(i+1)) & 0xFF))<br> MOVLW ((k >> 8*i) & 0xFF)<br> ENDIF<br> MOVWF f+i<br> ELSE<br> CLRF f+i<br> ENDIF<br> VARIABLE i = i - 1<br> ENDW<br> ENDM<br/> <br/>P.D.: A "@" is in front of the macros' labels so I don't get to mix them with CPU instructions. <br/>https://www.microchip.com/forums/FindPost/1133075Mon, 23 Mar 2020 11:21:31 GMTRe: How to make MPASM (et al.) convert a real number to a number format? e.g. Qm.f Fixed-point (Oblivion)"... or you can try Olin's MPASM preprocessor PREPIC:"<br><br>Interesting. Seems like this very problem is exactly why Olin made that tool. <br><br>Yeah, I figured I can't be the only one who ran into this problem. Using that tool would probably make real numbers in ASM possible. ... however ... At that point I'm accepting the use of community/third party tools. To admit that, I would also have to accept other tools as viable. Then, a vary large door has been opened...<br><br>GPASM exists, and is open source. So why not rewrite that to do what I REALLY want? But, if I'm going to use GPASM, why not use the whole GPUTILS suite? At that point, gotta' figure that SDCC would also be on the table, and is an ostensibly better choice. (That project sphere desperately needs maintainers/curators, BTW.) <br><br>All in all, I'd ultimately end up slipping all the way down the slope, and falling on using C. In which case, I'd almost for sure also be saying bye bye to PIC, and hello to STM32 family. (The NUCLEO boards, in particular, are looking mighty tasty right now.)<br/>https://www.microchip.com/forums/FindPost/1119447Wed, 20 Nov 2019 02:28:21 GMTRe: How to make MPASM (et al.) convert a real number to a number format? e.g. Qm.f Fixed-point (1and0)<blockquote class="quote"><span class="original">Gort2015</span>Anything greater than 1.0 won't be compatible with the C compiler that uses Q1.15 and Q1.31 formats for fixed floats.<br><br> Keeping to that format, it's easy to make a sine table stored in Q1.15 format. <br><br>The 16bit assembler supports double, fixed and float directives btw. <br></blockquote><br>This is the MPASM forum, where anything and everything that are C compiler or 16-bit assembler are not helpful and not relevant advices.https://www.microchip.com/forums/FindPost/1119410Tue, 19 Nov 2019 16:32:21 GMTRe: How to make MPASM (et al.) convert a real number to a number format? e.g. Qm.f Fixed-point (Gort2015)<blockquote class="quote"><span class="original">1and0</span><br/><blockquote class="quote"><span class="original">Gort2015</span><br/>A Q number is smaller than 1.0 so you cannot have 16.16<br/></blockquote><br/>Of course you can have Q numbers in Qm.n format with m integer bits and n fractional bits.<br/> <br/></blockquote><br/>Anything greater than 1.0 won't be compatible with the C compiler that uses Q1.15 and Q1.31 formats for fixed floats.<br/> <br/>Keeping to that format, it's easy to make a sine table stored in Q1.15 format.<br/> <br/>The 16bit assembler supports double, fixed and float directives btw.<br/> <br/> <br/><br><br><br/> <br/>https://www.microchip.com/forums/FindPost/1119397Tue, 19 Nov 2019 14:31:21 GMTRe: How to make MPASM (et al.) convert a real number to a number format? e.g. Qm.f Fixed-point (1and0)... or you can try Olin's MPASM preprocessor PREPIC: <a href="https://www.microchip.com/forums/FindPost/378769" target="_blank" rel="nofollow">https://www.microchip.com/forums/FindPost/378769</a><br/> <br/>Disclaimer: I have not used it before.<br/>https://www.microchip.com/forums/FindPost/1119385Tue, 19 Nov 2019 12:24:47 GMTRe: How to make MPASM (et al.) convert a real number to a number format? e.g. Qm.f Fixed-point (Oblivion)" [...] I'm afraid it will fall on deaf ears"<br><br>As I'm sure you can guess, I am quite painfully aware. Not worth the email paper. :(<br><br>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.<br><br>(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?)<br><br>BTW: Thanks for all the help friends.<br/>https://www.microchip.com/forums/FindPost/1119381Tue, 19 Nov 2019 12:13:43 GMTRe: How to make MPASM (et al.) convert a real number to a number format? e.g. Qm.f Fixed-point (1and0)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.<br/>https://www.microchip.com/forums/FindPost/1119378Tue, 19 Nov 2019 11:39:13 GMTRe: How to make MPASM (et al.) convert a real number to a number format? e.g. Qm.f Fixed-point (Oblivion)"MPASM does NOT support floating point assembly variables and operators."<br><br>That's really really really too bad.<br><br><placeholder for even sadder face than before.><br><br>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.<br><br>:(<br/>https://www.microchip.com/forums/FindPost/1119376Tue, 19 Nov 2019 11:00:12 GMTRe: How to make MPASM (et al.) convert a real number to a number format? e.g. Qm.f Fixed-point (1and0)<blockquote class="quote"><span class="original">Gort2015</span><br/>A Q number is smaller than 1.0 so you cannot have 16.16<br/></blockquote><br/>Of course you can have Q numbers in Qm.n format with m integer bits and n fractional bits.<br/> <br/>https://www.microchip.com/forums/FindPost/1119367Tue, 19 Nov 2019 09:57:54 GMTRe: How to make MPASM (et al.) convert a real number to a number format? e.g. Qm.f Fixed-point (1and0)<blockquote class="quote"><span class="original">Oblivion</span><br/>e.g. All these would be the same number....<br><pre class="prettyprint">MOVLW (31.5 * 2)<br />MOVLW d'63'<br />MOVLW 0x3F</pre><br>... but MPASM won't assemble that first one. "error 208 : Missing operator"<br/></blockquote><br>That is because the '.' is a prefix for decimal number in MPASM. ;) You'd have to use this<br/><pre class="prettyprint">MOVLW (.315 * 2 / .10)</pre><br/> <br/>https://www.microchip.com/forums/FindPost/1119366Tue, 19 Nov 2019 09:53:48 GMTRe: How to make MPASM (et al.) convert a real number to a number format? e.g. Qm.f Fixed-point (1and0)MPASM does NOT support floating point assembly variables and operators.<br/> <br/>Quoted from the MPASM User's Guide:<br><blockquote class="quote"><span class="original"></span><br>Intermediate values in constant expressions are treated as 32-bit unsigned<br>integers. Whenever an attempt is made to place a constant in a field for<br>which it is too large, a truncation warning will be issued.<br></blockquote><br/>https://www.microchip.com/forums/FindPost/1119365Tue, 19 Nov 2019 09:50:26 GMTRe: How to make MPASM (et al.) convert a real number to a number format? e.g. Qm.f Fixed-point (Oblivion)... linear algebra.<br/>https://www.microchip.com/forums/FindPost/1119361Tue, 19 Nov 2019 09:20:47 GMTRe: How to make MPASM (et al.) convert a real number to a number format? e.g. Qm.f Fixed-point (Gort2015)Trig:<br/>It's not that bad, calculate the 1st quadrant, the other 3 are derived from the first.<br/>Use a spreadsheet.<br/>https://www.microchip.com/forums/FindPost/1119359Tue, 19 Nov 2019 09:19:15 GMTRe: How to make MPASM (et al.) convert a real number to a number format? e.g. Qm.f Fixed-point (Oblivion)<img src="http://www.quickmeme.com/img/14/1445432a424063893f1714f42778762764957a53df955b269d417d94d5b77c39.jpg" /><br/>https://www.microchip.com/forums/FindPost/1119358Tue, 19 Nov 2019 09:09:27 GMTRe: How to make MPASM (et al.) convert a real number to a number format? e.g. Qm.f Fixed-point (NorthGuy)<blockquote class="quote"><span class="original">Oblivion</span><br/>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.<br></blockquote><br><br><br/>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.<br/>https://www.microchip.com/forums/FindPost/1119355Tue, 19 Nov 2019 09:02:42 GMTRe: How to make MPASM (et al.) convert a real number to a number format? e.g. Qm.f Fixed-point (Gort2015)65536 would be too many powers of 2.<br/> <br/>"3.14159265"<br/> <br/>.14159265 x (2 ^ 15) = 0.001, 0010 : 0010, 0000<br/>.14159265 x (2 ^ 31) = 0.001, 0010 : 0001, 1111 : 1011, 0101 : 0011, 1101<br/> <br/>DSC pic has 40bit accumulators to store the 3 otherwise the integer part would be seperate.<br/>https://www.microchip.com/forums/FindPost/1119351Tue, 19 Nov 2019 08:42:05 GMTRe: How to make MPASM (et al.) convert a real number to a number format? e.g. Qm.f Fixed-point (Oblivion)@Gort2015. We crossed posts here, so ... new info ... all is forgiven.<br><br>"Use the common Q1.15 and Q1.31 formats."<br/><br>Format isn't the issue here, it's getting MPASM to accept a decimal with fractional part, period.<br/>https://www.microchip.com/forums/FindPost/1119348Tue, 19 Nov 2019 08:26:35 GMTRe: How to make MPASM (et al.) convert a real number to a number format? e.g. Qm.f Fixed-point (Oblivion)<blockquote>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.<br/></blockquote> <br/>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.<br/><blockquote> <br/>But isn't it able to create something useful from "3.14159265 * 65536" ?<br/></blockquote> <br/>No, and this is precisely the problem.<br><br> 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.<br><br>e.g. All these would be the same number....<br><pre class="prettyprint">MOVLW (31.5 * 2)<br />MOVLW d'63'<br />MOVLW 0x3F</pre><br> ... but MPASM won't assemble that first one. "error 208 : Missing operator"<br/>https://www.microchip.com/forums/FindPost/1119347Tue, 19 Nov 2019 08:23:14 GMTRe: How to make MPASM (et al.) convert a real number to a number format? e.g. Qm.f Fixed-point (Gort2015)Use the common Q1.15 and Q1.31 formats.<br/> <br/>Align the float with Q1.15<br/>A Q number is smaller than 1.0 so you cannot have 16.16<br/> <br/>-0.75<br/> <br/>1.110, 0000 : 0000, 0000<br/> <br/>Float to Q1.5<br/>0.5 * (2^15)<br/>0.100, 0000 : 0000, 0000<br/> <br/> <br/>https://www.microchip.com/forums/FindPost/1119346Tue, 19 Nov 2019 08:21:46 GMTRe: How to make MPASM (et al.) convert a real number to a number format? e.g. Qm.f Fixed-point (du00000001)I don't work with MPASM.<br/>But isn't it able to create something useful from "3.14159265 * 65536" ?<br/>https://www.microchip.com/forums/FindPost/1119344Tue, 19 Nov 2019 08:01:06 GMT