The permute command from AVX2 instructions needs a parameter from type imm8. This parameter controls how the permutation is performed. Unfortunately I do not understand how this imm8 parameter is "created". What value do I have to set or how can I determine what value I have to set for a specific permuation?
Example:
_mm256_permute_pd(vec2, 0x5);
Here the parameter 0x5 permutes the first and second double in vec2 and the third and fourth double in vec2. But how do I know that 0x5 does that?
It's 4x 1-bit indices that select one of the two elements from the corresponding lane of the source vector, for each destination element. Read the Operation section of the docs for the asm instruction: http://felixcloutier.com/x86/VPERMILPD.html.
Or look it up in Intel's intrinsics guide, which has similar pseudo-code that shows exactly how each bit selects the source for an element of the result.
It's not lane-crossing vpermpd, so it's not like the 2-bit indices that _MM_SHUFFLE is a helper macro for, so it's not quite like Convert _mm_shuffle_epi32 to C expression for the permutation?.
Related
How does the computer execute value equality comparisons? Does it compare the values bit by bit, starting from the smallest bit, and stop once two different bits are encountered? Or does it start from the highest bit? Does it go through all bits regardless of where/when two unlike bits are found?
When you write an equality comparison in a higher level language (e.g. c), it will be transformed to intermediary representation and then to the instructions of a particular platform this code will be executed upon. Compiler is free to implement the equality comparison using any of the instructions available on a target architecture. The idea is usually to make it faster.
Different architectures have different instruction sets. Different processors can have varying implementation strategies (again to make things faster), as long as they comply with the spec.
Below are a few examples
x86
CMP command is used to compare two values. Here's an excerpt from Instruction set reference.
Compares the first source operand with the second source operand and sets the status flags in the EFLAGS register according to the results. The comparison is performed by subtracting the second operand from the first operand and then setting the status flags in the same manner as the SUB instruction. When an immediate value is used as an operand, it is sign-extended to the length of the first operand.
This basically means all bits are examined. I guess it was implemented that way to allow non-equality(<,>) comparisons too.
So all bits are examined. In the simplest cases that can be done serially, but can be done faster. See wikibooks on add / subtract blocks.
ARM
TEQ command can be used to test two values for equality.
Here's an excerpt from the infocenter.arm.com
The TEQ instruction performs a bitwise Exclusive OR operation on the value in Rn and the value of Operand2. This is the same as the EORS instruction, except that it discards the result.
Use the TEQ instruction to test if two values are equal without affecting the V or C flags.
Again all bits are examined.
I am getting the user to input a function, e.g. y = 2x^2 + 3, as a string. What I am looking to do is to enter that string into TChart and for TChart to graph the function.
As far as I know, TChart/TeeChart will only accept X values that are assigned values, e.g. -10 to 10 for X, so the X value would need to be calculated each time - this isn't an issue.
The issue is getting each part of the inputted function and substituting the X-values into each part. The workaround I have found is to get the user to enter the degree for each part of the function, e.g. 2 for X^2, 3 for X^3, etc. but is there a cleaner way of doing this?
If I could convert the inputted string into a Mathematical formula which TeeChart would accept, that would be the ideal outcome.
Saying that you can't use external units effectively makes your question unanswerable in the SO format, because the topic is far broader (and deeper) that can comfortably be dealt with in SO's Q&A format. So the following is at best an outline:
If you want to, or have to, write a DIY expression evaluator, one way to do it is to proceed as follows:
Write yourself a class that takes a string as input and snips it up into a series of symbols, aka "tokens" which represent the component parts of the expression, e.g, numbers, operators, parentheses, names of functions, names of variables, etc; these tokens might themselves be records or class instances and need to include a mechanisms for storing values associated with particular symbols (e.g. the tokens that represent numbers in the input). This step is called "tokenisation" or "lexing". Store the resulting list of symbols in a list or similiar structure. This class needs to implement a mechanism to retrieve the next symbol from the list (usually, this method is called something like "NextToken") and indicate whether there are any symbols left. This class also needs a mechanism to "put back" a symbol (or, equivalently, "peek" the symbol following the current one).
Then, write yourself a s/ware machine which takes the tokenised symbols and "evaluates" the list of symbols to produce the (mathematical) result you're after. This step is an order of magnitude or two more difficult than the tokenisation step. There are numerous ways to do it. As I said an a comment earlier, a recursive descent parser is probably the most tractable approach if you've never done anything like this before. There are countless examples in textbooks, but here's a link to an article about a Delphi implementation that should be understandable as an intro:
http://www8.umoncton.ca/umcm-deslierres_michel/Calcs/ParsingMathExpr-1.html
That article begins by noting that there are numerous pre-existing Delphi expression evaluators but makes the point that they are not necessarily the best place to start for someone wanting to learn how to write an evaluator/parser rather than just use one. Instead it goes through the coding of an evaluator to implement this simple expression grammar:
expression : term | term + term | term β term
term : factor | factor * factor | factor / factor
factor : number | ( expression ) | + factor | β factor
(the vertical bar | denotes βorβ)
The article has a link to a second part which shows had to add exponentiation to the evaluator - this is trickier than it might sound and involves issues of ambiguity: e.g. how to evaluate - and what does it mean to write - an expression like
x^y^z
? This relates to the issue of "associativity": most operators are "left associative" which means that they bind more tightly to what's on the left of them than what's on their right. The exponentiation operator is an example of the reverse, where the operator binds more tightly to what's on its right.
Have fun!
By the way, you used to see suggestions to implement an evaluator using the "shunting yard algorithm"
http://en.wikipedia.org/wiki/Shunting-yard_algorithm
to convert an "infix" expression where the operators are between the operands, as in 1 + 3 * 4 to RPN (reverse Polish notation), as used on older HP calculators. The reason to do that was that RPN makes for much more efficient evaluation of an expression that the infix equivalent. Ymmv, but personally I found that implementing the SY algorithm properly was actually trickier than learning how to write an evaluator in the expression/term/factor style.
Fwiw, RPN is the basis of the Forth programming language, http://en.wikipedia.org/wiki/Forth_%28programming_language%29, so you could write a Forth implementation in Delphi if you wanted!
I'm trying to write a scheme parser as an exercise, but I'm not sure how to implement the vector syntax. The specification seems to indicate that the way to input a vector is '#(1 2 3) and such a vector should evaluate to #(1 2 3). However, when I experimented in several Scheme implementations, #(1 2 3) was accepted as valid input as well. Is this a universally implemented extension to the spec?
According to the R5RS spec, vectors must be quoted:
Note that this is the external representation of a vector, not an expression evaluating to a vector. Like list constants, vector constants must be quoted:
'#(0 (2 2 2 2) "Anna")
Some implementations have chosen to allow evaluation of unquoted vectors, but any such extensions are non-standard, and a "feature" of that particular implementation.
For what it's worth, the upcoming R7RS spec explicitly requires vectors to self-evaluate:
Vector constants are self-evaluating, so they do not need to be quoted in programs.
Not universal at all. Chicken, for example, will not allow you to evaluate an unquoted vector.
#;1> #(1 2 3)
Error: illegal non-atomic object: #(1 2 3)
#;1> '#(1 2 3)
#(1 2 3)
Funny things about standards and implementations. An implementation can give you lots of features as long as they don't collide with the spec. So one implementation wanted #(1 2 3) to be evaluated as if it was self evaluation and it's ok because that will never happen in a R5RS conforming program anyway. In R5RS, running a non R5RS conforming program like
(define test "hello")
(string-set! test 0 #\H)
Is undefined. It could fail, not change test silently or change test. Same goes for R6RS but an implementation might signal an error - but still don't have to.
AFAIK racket has the most strict R5RS, but even their implementation has som extras still. Eg. they allow symbols that are not allowed in the spec and they have symbols defined in their report-environment to boostrap R5RS in racket's module system. However, like all implementations there are little errors when given a R5RS conforming program.
Does F# have a built in cube root function? I know I can use exponentiation to compute cuberoots but it won't type check in my case since I want to take the cuberoot of a quantity of type float and get a float.
I don't think there is a built-in function to calculate the cube root with units of measure (I assume it would be in the primitive operators module where sqrt and others are), so I think the only option is to use exponentiation.
However, you can use exponentiation without units and wrap the unit-unsafe operation in a function that adds units, so you get function with correct units:
let cuberoot (f:float<'m^3>) : float<'m> =
System.Math.Pow(float f, 1.0/3.0) |> LanguagePrimitives.FloatWithMeasure
Note that F# does not support fractional units so you can write cuberoot (10.0<m^3>) or cuberoot (10.0<m^9>), but if you write cuberoot (10.0<m>) then it will not type-check, because the result would be meters to 1/3 (and that's a fractional unit).
This sample is only implementing cuberoot for float. If you wanted to write overloaded function that works with other numeric types (I guess you might need float32) then it gets a bit uglier (so I would not recommend that unless necessary), but you can use a trick with intermediate type with multiple overloads like, for example, in this answer.
In the Erlang shell, I can do the following:
A = 300.
300
<<A:32>>.
<<0, 0, 1, 44>>
But when I try the following:
B = term_to_binary({300}).
<<131,104,1,98,0,0,1,44>>
<<B:32>>
** exception error: bad argument
<<B:64>>
** exception error: bad argument
In the first case, I'm taking an integer and using the bitstring syntax to put it into a 32-bit field. That works as expected. In the second case, I'm using the term_to_binary BIF to turn the tuple into a binary, from which I attempt to unpack certain bits using the bitstring syntax. Why does the first example work, but the second example fail? It seems like they're both doing very similar things.
The difference between in a binary and a bitstring is that the length of a binary is evenly divisible by 8, i.e. it contains no 'partial' bytes; a bitstring has no such restriction.
This difference is not your problem here.
The problem you're facing is that your syntax is wrong. If you would like to extract the first 32 bits from the binary, you need to write a complete matching statement - something like this:
<<B1:32, _/binary>> = B.
Note that the /binary is important, as it will match the remnant of the binary regardless of its length. If omitted, the matched length defaults to 8 (i.e. one byte).
You can read more about binaries and working with them in the Erlang Reference Manual's section on bit syntax.
EDIT
To your comment, <<A:32>> isn't just for integers, it's for values. Per the link I gave, the bit syntax allows you to specify many aspects of binary matching, including data types of bound variables - while the default type is integer, you can also say float or binary (among others). The :32 part indicates that 32 bits are required for a match - that may or may not be meaningful depending on your data type, but that doesn't mean it's only valid for integers. You could, for example, say <<Bits:10/bitstring>> to describe a 10-bit bitstring. Hope that helps!
The <<A:32>> syntax constructs a binary. To deconstruct a binary, you need to use it as a pattern, instead of using it as an expression.
A = 300.
% Converts a number to a binary.
B = <<A:32>>.
% Converts a binary to a number.
<<A:32>> = B.