printing out floating point value - printf

Trying to print out floating point number from st0 register.
Currently I have to pass the value through xmm0 register then call c function printf.
is there any way to pass the value from the st0 register straight into printf?

the x86-64 ABI's require that you use XMM registers to pass around floating point values, so unless you write a custom printf that uses your own ABI (calling convention), there is really no way around this.

Related

Kotlin Primitives: How to reinterpret ByteArrays as the bits of primitives in Common Multiplatform code?

TL;DR is there an equivalent of C++'s reinterpret_cast<[primitive]>(bytes) for Kotlin Multiplatform?
Basically, what I am looking for is the following functionality:
You have a ByteArray with length, let's say, 4 bytes. The contents are
00 00 00 2A
Then there should be some function or operator to reinterpret these 4 bytes as an Int:
asInt(byteArrayOf(0x00, 0x00, 0x00, 0x2A))
Ideally, there would also be a way to control the Endian-ness of this operation. And most importantly, I'd want this to work on all available platforms (JVM, JS, Native). The question is: Is there such an operation?
Currently, I am doing the following:
For the integral types (Byte, Short, Int, Long), I use SHL / OR to construct the actual primitive. But this is of course not as efficient as just reinterpreting (maybe also copying) the value as the primitive value, since the bits are already in the right configuration
For the floating-point values, I'm still struggling: So far, I have not found a platform-independent solution, so I use expect / actual. I've found solutions for JVM and JS already (although not that satisfying), but I still don't know how to do it on Native. If it turns out that there is no common solution to the whole problem, I'd be very thankful if someone could point me towards a solution just for Float / Double converting on Native.
Thank you very much!
Your existing solution for integral types is likely to be ideal for multiplatform. For floating point values, you can convert them to and from int/long with
Double/Float.fromBits and Double/Float.toRawBits, and then use your existing solution for integral types.

Not getting Tan (90) Value as undefined in objective-c

While running below statement it prints the wrong value in the console. I got stuck as I am a beginner.
NSLog(#"tan(90)=%f",tan(90*M_PI/180.0);
Output displayed as : tan(90)=16331239353195370.000000
The result is correct. M_PI / 2.0 is a double that is quite close to π/2, but is not precisely π/2 (π cannot be precisely represented by a double). Therefore its tangent is very large, but not infinite.
schmitdt9's link to the tan docs are useful, but the important note is this one:
The function has mathematical poles at π(1/2 + n); however no common floating-point representation is able to represent π/2 exactly, thus there is no value of the argument for which a pole error occurs.
"Pole" means "input for which the function is infinite."
To your question "how i should print tan(90) as undefined in console," the answer is you'll need to special-case it. Normalize whatever you've been passed to 0-360, and check if it's 90 or 270. If so, print infinite, otherwise call tan.
tan is a C function, please refer to this page http://en.cppreference.com/w/cpp/numeric/math/tan
Especially it is said:
If a domain error occurs, an implementation-defined value is returned
(NaN where supported)
So I suppose the number 16331239353195370.000000 means Infinity/error in this case
Edit
About Infinity printing
There is a special macro INFINITY and if you do
NSLog(#"%f", INFINITY);
the output will be
inf

how to perform floating point calculation in tmote sky (contiki)

I have the following code snippet:
#include "contiki.h"
#include <stdio.h> /* For printf() */
PROCESS(calc_process, "calc process");
AUTOSTART_PROCESSES(&calc_process);
PROCESS_THREAD(calc_process, ev, data)
{
double dec=13.2, res=0, div=3.2;
PROCESS_BEGIN();
res=dec+div;
printf("%f",res);
PROCESS_END();
}
After uploading the above code in Tmote sky platform using the command
make TARGET=sky calc.upload, the program will be loaded to the mote (there is no error). Then login to the mote using make login TARGET=sky, the following output is displayed....
OUPUT:
**Rime started with address 4.0
MAC 04:00:00:00:00:00:00:00 Contiki 2.7 started. Node id is set to 4.
CSMA ContikiMAC, channel check rate 8 Hz, radio channel 26
Starting 'calc process'
%f**
How can I get the correct value?
Thanks
It is not floating point calculation support that you need - you have that already. What is missing is floating point support within printf(). That is to say that res will be calculated correctly, but printf() does not support its display.
Because it requires a relatively large amount of code, many microcontroller targeted libraries omit floating point support in stdio. There may be a library build option to include floating point support - refer to the library documentation.
You might do well to ask a question about the specific calculation necessary, and how it might be done using integer or fixed point arithmetic. Alternatively you might write your own floating point display as described here: How to print floating point value using putchar? for example.

Assembly code for optimized bitshifting of a vector

i'm trying to write a routine that will logically bitshift by n positions to the right all elements of a vector in the most efficient way possible for the following vector types: BYTE->BYTE, WORD->WORD, DWORD->DWORD and WORD->BYTE (assuming that only 8 bits are present in the result). I would like to have three routines for each type depending on the type of processor (SSE2 supported, only MMX suppported, only standard instruction se supported). Therefore i need 12 functions in total.
I have already found by myself how to backup and restore the registers that i need, how to make a loop, how to copy data into regular registers or MMX registers and how to shift by 1 position logically.
Because i'm not familiar with assembly language that's about it.
Which registers should i use for each instruction set?
How will the availability of the large vector (an image) in L1 cache be optimized?
How do i find the next element of the vector (a pointer kind of thing), i know i can make a mov by address and i assume i have to increment the address by 1, 2 or 4 depending on my type of data?
Although i have all the ideas, writing the code is a bit difficult at this point.
Thank you.
Arnaud.
Edit:
Here is what i'm trying to do for MMX for a shift by 1 on a DWORD:
__asm("push mm"); // backup register
__asm("push cx"); // backup register
__asm("mov %cx, length"); // initialize loop
__asm("loopstart_shift1:"); // start label
__asm("movd %xmm0, r/m32"); // get 32 bits data
__asm("psrlq %xmm0, 1"); // right shift 32 bits data logically (stuffs 0 on the left) by 1
__asm("mov r/m32,%xmm0"); // set 32 bits data
__asm("dec %cx"); // decrement index
__asm("cmp %cx,0");
__asm("jnz loopstart_shift1");
__asm("pop cx"); // restore register
__asm("pop mm"); // restore register
__asm("emms"); // leave MMX state
I strongly suggest you pause and take a look at using intrinsics with C or C++ instead of trying to write raw asm - that way the C/C++ compiler will take care of all the register allocation, instruction scheduling and general housekeeping tasks and you can just focus on the important parts, e.g. instead of using psrlq see _m_psrlq in mmintrin.h. (Better yet, look at using 128 bit SSE intrinsics.)
Sounds like you'd benefit from either using or looking into BitMagic's source. its entirely intrinsics based too, which makes its far more portable (though from the looks of it your using GCC, so it might have to get an MSVC to GCC intrinics mapping).

inline assembly output register declaration

i'm just about to learn inline assembly.the GCC inline assembly cookbook http://www.ethernut.de/en/documents/arm-inline-asm.html
says:
A strict rule is: Never ever write to an input operand.
can someone tell me whether - and if so why - this rule is true?
let's say i get the value of an input operand through some register. Am I not allowed to reuse this register within the same assembly block if i don't inted to declare it also as output operand?
example:
asm volatile("add %[value], %[value], %[value] \n\t"
"mov %[result], %[value] \n\t"
: [result]"=r" (y)
: [value]"r" (x)
: //no clober
);
I know the example doesn't make much sense - but is it invalid?
I ask because i'm writing some assembly function that takes many input operands, each taking a general purpose register. since there are only 12 GPR's available on my architecture, with each input operand i get less "free" registers to work with. so do I really have to declare the input registers also as output in order to use them to "work" with them inside the function (even though i don't need theyr value outside the inline-assembly body? If so - can someone explain why?
hope the question is clear
thanks!
The compiler doesn't know x is clobbered (and I think there is no way to clobber an input register in a valid way). So it might reuse the register holding x later in the code assuming it still holds an unaltered value which isn't true since you changed it.