replication operator with 0 - operators

I have RTL code in which the replication index in the replicator operator is 0.
inst (. in ( { {0{1'b0}}, 1'b1 })
I am not able to predict the behavior here.
I didn't find anything specified in LRM for 0 or negative replication index in replication operator.

This often occurs where you have a parametrised bit width and 1 bit wide is an allowed value.
parameter WIDTH = 1;
inst ( .in ( { {WIDTH-1{1'b0}}, 1'b1 }) );
Which results a warning along the lines of :
Warning: : Non-positive replication multiplier inside concat. Replication will be ignored.
To avoid the warning you could use generates:
parameter WIDTH = 1;
wire [WIDTH-1:0] connection;
generate
if (WIDTH > 1) begin
assign connection = { {WIDTH-1{1'b0}}, 1'b1 };
end
else begin
assign connection = 1'b1 ;
end
endgenerate
inst ( .in( connection ) );
Or over-sizing, by 1 bit.
parameter WIDTH = 1;
wire [WIDTH:0] connection = { {WIDTH{1'b0}}, 1'b1 } ;
inst ( .in ( connection[WIDTH-1:0] ) );

The 1800-2012 LRM says
A replication operation may have a replication constant with a value
of zero. This is useful in parameterized code. A replication with a
zero replication constant is considered to have a size of zero and is
ignored. Such a replication shall appear only within a concatenation
in which at least one of the operands of the concatenation has a
positive size.
So your expression will be treated as a 1-bit value 1'b1.

Related

use associate array total value count Lua

i want to count the data type of each redis key, I write following code, but run error, how to fix it?
local detail = {}
detail.hash = 0
detail.set = 0
detail.string = 0
local match = redis.call('KEYS','*')
for i,v in ipairs(match) do
local val = redis.call('TYPE',v)
detail.val = detail.val + 1
end
return detail
(error) ERR Error running script (call to f_29ae9e57b4b82e2ae1d5020e418f04fcc98ebef4): #user_script:10: user_script:10: attempt to perform arithmetic on field 'val' (a nil value)
The error tells you that detail.val is nil. That means that there is no table value for key "val". Hence you are not allowed to do any arithmetic operations on it.
Problem a)
detail.val is syntactic sugar for detail["val"]. So if you expect val to be a string the correct way to use it as a table key is detail[val].
Possible problem b)
Doing a quick research I found that this redis call might return a table, not a string. So if detail[val] doesn't work check val's type.

GO: Obtain various query results in if else block based on parameters number

I am using go-mysql-driver to make queries to my database.
I have a function In which I am passing id and warehouseId.
Now I am modifying my mysql query based on if warehouseId value is 0 or not.
The problem is the parameters that I pass in db.Query().
Following is my mysql query where I am appending additional query if warehouseId is not 0.
query := "select id,description from offers inner join offer_entities on offers.id = offer_entities.offer_id where offer_entities.entity_id = ?"
if warehouseId != 0 {
query += `and offer_entities.warehouse_id = ? `
}
query += `group by offer_id`
I parse it like this:
if warehouseId != 0 {
rows, err := db.Query(query, variantId, warehouseId)
} else {
rows, err := db.Query(query, variantId)
}
However, the problem is when I run it, I get an error undefined: rows. I know that it is because rows should be defined outside the if-else conditions. But I don't understand how to define rows outside if-else or
If there is any other way I could achieve my requirements.
How should I tackle this problem.
Thanks in advance.
the problem is because of variables are defined in the scope in which they are declared.
The difference between = and := is that = is just assignment and := is used for assignment and declaration
for example
rows := 1
//it basically means
var rows int
rows = 1
what you want is to declare rows outside the if-else and then use them, will solve the problem.
Scope visibility
Each block has itself visibility scope. A general rule is visible from inner, invisible from outer. It means you can see variables from superior scope in subject scope but not vise versa.
I believe you use rows declared in if statement out of the block, so it doesn't exist there.
var err error
var rows string
if warehouseId != 0 {
rows, err := db.Query(query, variantId, warehouseId)
} else {
rows, err := db.Query(query, variantId)
}
fmt.Println(rows) // It is now possible to use `rows` outside of the block
Declaration styles
var name <type> is a common declaration way. The variable with typed nil value will be declared.
name := <value of type> is a short declaration form. It will declare a variable of a type and assign a value to it at the same time. Allowed only in functions.

How to get floating point results from an SQL query?

I have successfully connected to my database, but when I select 2 cells from my SQL db to set the value for both of my variables it does nothing. I have no problem inserting data into the database. I have tried different approaches to this with no success. Could it be that I am trying to use a string format for doubles??
double userHeight;
double userWeight;
QSqlQuery query;
QString retreiveUserHeight =
QString("SELECT Height, Weight FROM SQL1 WHERE Username='joejoe'");
query.prepare(retreiveUserHeight);
query.bindValue(0,"Height");
query.bindValue(1, "Weight");
query.exec();
userHeight = query.value(0).toInt();
userWeight = query.value(1).toInt();
I'm pretty certain there is a small error in syntax that is causing this mishap but I have been unable to find it. Thanks for your help.
qDebug() << "calculated" << 703*(userWeight/(userHeight*userHeight))
<< userWeight << userHeight ;
Heres the debug output:
calculated nan 0 0
// Obtain username from somewhere
QString username = "joejoe";
// Check whether DB is open
if( db->isOpen( ) )
{
QSqlQuery query;
double userHeight;
double userWeight;
// Prepare select statement
query.prepare ( "SELECT Height , Weight FROM SQL1 WHERE Username = :username" );
query.bindValue ( ":username" , username );
query.exec ( );
// Check if something went wrong when executing your query
if( query.lastError( ).text( ).trimmed( ) == "" )
{
// Loop through all results and handle them accordingly
while( query.next( ) )
{
userHeight = query.value( 0 ).toDouble( );
userWeight = query.value( 1 ).toDouble( );
qDebug( ) << "calculated" << 703 * ( userWeight / ( userHeight * userHeight ) ) << userWeight << userHeight;
qDebug( ) << "---------------------------------";
}
}
else
{
// Display the error that occured
QMessageBox::critical( this , tr( "SQL Error" ) , query.lastError( ).text( ) );
}
}
I assume this is what you wanted it to look like.
I've included some error checking and corrected your query to use .bindValue( ) correctly, since it's not meant for using for return values rather than for input as seen in the WHERE.
Since I don't know anything about your sql table I've included a loop to go through all results of your query. That can obviously be changed.
Apart from that if you're using doubles you should cast the result .toDouble( ) rather than .toInt( )
There are a number of serious problems with this code. Let's start with the concept of prepared SQL queries. Wikipedia lists two reasons for using prepared statements:
The overhead of compiling and optimizing the statement is incurred
only once, although the statement is executed multiple times. [...]
Prepared statements are resilient against SQL injection, because
parameter values, which are transmitted later using a different
protocol, need not be correctly escaped.
Neither of these reasons apply in your code; you're only executing the query once, and you're not splicing any inputs into the string. In fact, the only input in your query at all is the username, which is hard-coded to "joejoe":
"SELECT Height, Weight FROM SQL1 WHERE Username='joejoe'"
Since there are no variable inputs, using a prepared query doesn't make much sense. Neither do the following lines:
query.bindValue(0,"Height");
query.bindValue(1, "Weight");
Height and Weight are outputs from this query, not inputs. See the section in the Qt docs for QSqlQuery titled "Approaches to Binding Values" for an explanation of how this is intended to work. Qt's API for binding prepared SQL queries is fairly typical among database libraries, there's nothing earth shattering here.
Then we get to this:
userHeight = query.value(0).toInt();
userWeight = query.value(1).toInt();
Both the variables you're reading into here were declared as doubles, but you're calling toInt() on the returned QVariant rather than toDouble(). I don't know what (if any!) values are in your database, but it's possible they're getting rounded down to zero during the conversion from double to int if the values are between -1.0 and 1.0.
That said, you aren't doing any error checking whatsoever. The methods prepare() and exec() return bools that indicate whether they succeeded or failed. Likewise, both toInt() and toDouble() tell you whether they've succeeded or failed if you pass in a pointer to a bool. It's worth noting that both methods also return a zero value on failure.

Passing a module name as parameter

I want to create this generic wrapper for a bunch of modules I am writing. The wrapper should provide the ability to connect these modules to different type of NoCs without having to change the behavior of the inner modules.
I thought that one way to do this would be the following.
Considering a very simple module to wrap:
module add #(
parameter COLUMN_WIDTH = 32
)
(
//data in
input logic [COLUMN_WIDTH-1:0] col_1,
input logic [COLUMN_WIDTH-1:0] col_2,
//data out
output logic [COLUMN_WIDTH-1:0] col_o
);
assign col_o = col_1 + col_2;
endmodule
The wrapper should be the following:
module wrapper #(
parameter COLUMN_WIDTH = 32,
parameter WRAPPED_MODULE = add
)
(
//data in
input logic [COLUMN_WIDTH-1:0] col_1,
input logic [COLUMN_WIDTH-1:0] col_2,
//data out
output logic [COLUMN_WIDTH-1:0] col_o,
/* std signals */
input logic clk,
input logic reset_i // reset everything
);
logic [COLUMN_WIDTH-1:0] max_result;
WRAPPED_MODULE #(.COLUMN_WDITH(COLUMN_WIDTH),
) a(
.*
);
always #(posedge clk) begin
if (reset_i)
max_result <= 0;
else
max_result <= (col_o > max_result) ? col_o : max_result;
end
endmodule
The error I get is the following:
Error-[IND] Identifier not declared
wrapper.sv, 4
Identifier 'add' has not been declared yet. If this error is not expected,
please check if you have set `default_nettype to none.
Which makes sense since a parameter is not the same as a macro.
A complete design should possibly instantiate a bunch of wrapped modules and I don't want to duplicate code by creating a wrapper for each inner module.
How can I do that?
A parameter cannot be a module name. It can be a data_type, implicit data_type, or type
IEEE Std 1800-2012 ยง A.2.1.1 Module parameter declarations:
parameter_declaration ::=
parameter data_type_or_implicit list_of_param_assignments
| parameter type list_of_type_assignments
A workaround is to use a generate block and compare the value of the parameter.
module wrapper #(
parameter COLUMN_WIDTH = 32,
parameter string WRAPPED_MODULE = "add"
)
(
// ...
);
// ...
generate
if (WRAPPED_MODULE=="add") begin
add #(.COLUMN_WDITH(COLUMN_WIDTH) ) a( .* );
end
else begin
// ...
end
endgenerate
// ...
endmodule
If you're using Vivado and SystemVerilog (only tested on 2017.4, likely would work on later versions too), I've been able to use something like parameter type WRAPPED_MODULE = add and it will synthesize as well.

neo4j cypher index query

I was used to use node_auto_index( condition ) to search for nodes using indexes, but now i used batch-import ( https://github.com/jexp/batch-import/ ) and it created indexes with specific names ( type, code, etc ).
So, how to do a cypher query using indexes on multiple properties ?
old query example :
START n = node : node_auto_index( 'type: NODE_TYPE AND code: NODE_CODE' ) RETURN n;
how to do the 'same' query but without node_auto_index and specific index names ?
START n = node : type( "type = NODE_TYPE" ) RETURN n;
Also, the next query does not work (no errors, but the result is empty and it shouldn't be) :
START n = node : type( 'type: NODE_TYPE AND code: NODE_CODE' ) RETURN n;
So, type is an index, code is an index. how to mix the two in the same query for a single node ?
Another question: whats the difference of node_auto_index and this indexes with specific names ?
Thank you.
You almost had it:
START n = node:type("type:NODE_TYPE") RETURN n;
or
START n = node:type(type="NODE_TYPE") RETURN n;