Saturday, 31 March 2018

c - Fastest way to count bits





Possible Duplicate:
How to count the number of set bits in a 32-bit integer?







Give a unsigned char type value,count the total bits in it.What's the fastest way?
I wrote three function as below,what's the best way,and can someone come up with a faster one?(I just want the extremely fast one)



const int tbl[] =
{
#define B2(n) n, n+1, n+1, n+2
#define B4(n) B2(n), B2(n+1), B2(n+1), B2(n+2)
#define B6(n) B4(n), B4(n+1), B4(n+1), B4(n+2)

B6(0), B6(1), B6(1), B6(2)
};

char naivecount (unsigned char val)
{
char cnt = 0;
while (val)
{
cnt += (val & 1);
val = val >> 1;

}
return cnt;
}

inline tableLookUp(int val)
{
assert(val >= 0 && val <= 255);
return tbl[val];
}


int asmCount(int val)
{
int res = 0;
asm volatile("xor %0, %0\n\t"
"begin:\n\t"
"cmp $0x0, %1\n\t"
"jle end\n\t"
"movl %1, %%ecx\n\t"
"and $0x1, %%ecx\n\t"
"addl %%ecx, %0\n\t"

"shrl %1\n\t"
"jmp begin\n\t"
"end:"
: "=r"(res)
: "r" (val));
return res;
}


EDIT:




I have test all the method,the fastest one is to use the popcntl instruction.In platform without the instruction,I will use table look-up.


Answer



If you want to code it by hand, try this:



#include 

int popcnt8(uint8_t x) {

x = (x & 0x55) + (x >> 1 & 0x55);

x = (x & 0x33) + (x >> 2 & 0x33);
x = (x & 0x0f) + (x >> 4 & 0x0f);

return x;
}


on x86, this compiles to (AT&T-syntax):



popcnt8:

movl %edi, %eax
shrb %dil
andl $85, %eax
andl $85, %edi
addl %eax, %edi
movl %edi, %eax
shrb $2, %dil
andl $51, %eax
andl $51, %edi
addl %eax, %edi

movl %edi, %eax
shrb $4, %dil
andl $15, %eax
addl %edi, %eax
movzbl %al, %eax
ret


Compare this to what gcc generates with the intrinsic:




#include 

int popcnt8_intrin(uint8_t x) { return __builtin_popcount(x); }


On x86 with SSE 4.2:



popcnt8_intrin:
movzbl %dil, %eax
popcntl %eax, %eax

ret


which is not optimal; clang generates:



popcnt8_intrin:
popcntl %edi,%eax
ret



reducing the calculation to one (!) instruction.



On x86 without SSE 4.2:



popcnt8_intrin:
subq $8, %rsp
movzbl %dil, %edi
call __popcountdi2
addq $8, %rsp
ret



gcc essentially calls its library here. Not quite optimal. clang does a little better:



popcnt8_intrin:                         # @popcnt8_intrin
movl %edi, %eax
shrl %eax
andl $85, %eax
subl %eax, %edi
movl %edi, %eax

andl $858993459, %eax # imm = 0x33333333
shrl $2, %edi
andl $858993459, %edi # imm = 0x33333333
addl %eax, %edi
movl %edi, %eax
shrl $4, %eax
addl %edi, %eax
andl $252645135, %eax # imm = 0xF0F0F0F
imull $16843009, %eax, %eax # imm = 0x1010101
shrl $24, %eax

ret


clang calculates popcnt for a whole 32 bit number. This is not optimal imho.


Friday, 30 March 2018

linux - Why do I use double quotes in shell scripts

I understand the usage single quote and double quote.



but I don't know situation need to double quotes in the script.




there is no diff that statements



$ echo hello world! $1
$ echo "hello world! $1"


please show me diff between normal and double quotes.

android - SAX parser crashes with "Permission Denied" exception message when retrieving google weather XML

I'm trying to get the current temperature via google weather with SAX Parser but I get a "permission denied" exception message when I try to get data:




Code:




/* Get what user typed to the
EditText. */



String cityParamString = ((EditText)
findViewById(R.id.edit_input)).getText().toString();




String queryString =
"http://www.google.com/ig/api?weather="+
cityParamString;



/* Replace blanks with
HTML-Equivalent. */



url = new URL(queryString.replace("
", "%20"));




/* Get a SAXParser from the
SAXPArserFactory. */



SAXParserFactory spf =
SAXParserFactory.newInstance();



SAXParser sp = spf.newSAXParser();



/* Get the XMLReader of the
SAXParser we created. */




XMLReader xr = sp.getXMLReader();



/* Create a new ContentHandler and
apply it to the XML-Reader */



GoogleWeatherHandler gwh = new
GoogleWeatherHandler();



xr.setContentHandler(gwh);




/* Parse the xml-data our URL-call
returned. */



xr.parse(new
InputSource(url.openStream())); <----
THIS CRASHES WITH PERMISION DENIED
EXCEPTION MESSAGE





The url seems to be fine, but url.openstream doesn't work.

php - how to redirect url after submitting a form?



It seems to be a simple but still it is difficult for me to achieve.
Let`s make a small example to clarify the situation:
There are two files: Form.PHP and Action.PHP



Form.PHP has one Input field with name = "ID"
and button SUBMIT




Action.PHP has a script of inserting data in MySQL
and in the end next line:



header("location:Form.PHP");


So after the submitting the form, I come back to Form.PHP...
This is easy...




Now, I want to achieve - after the submitting the form redirection to
Form.PHP?ID=$_POST['ID']



So please help me to modify
header("location:Form.PHP"); to redirect to ?ID=$_POST['ID']


Answer



header("Location: Form.PHP?ID=".$_POST['ID']);

apache - How to check if mod_rewrite is enabled in php?



I was wondering if it is possible to check if mod_rewrite is enabled on Apache AND IIS in PHP.



ModRewrite for IIS exists. Check it here.



So, I'm looking for a PHP script that checks for mod_rewrite on Apache and IIS.




Does anyone know such script or can write one?



Especially for Microsoft IIS.



Thanks!


Answer



If you're using mod_php, you can use apache_get_modules(). This will return an array of all enabled modules, so to check if mod_rewrite is enabled, you could simply do



in_array('mod_rewrite', apache_get_modules());



Unfortunately, you're most likely trying to do this with CGI, which makes it a little bit more difficult.



You can test it using the following, though



strpos(shell_exec('/usr/local/apache/bin/apachectl -l'), 'mod_rewrite') !== false


If the above condition evaluates to true, then mod_write is enabled.


php - syntax error unexpected T_BOOLEAN_OR




I'm pretty sure its probably ( or ) that is causing it maybe one too many of them.



$filechk1 = "/temp/files/" . $data[0] . ".doc";
$filechk2 = "/temp/files/" . $data[1] . ".doc";
$dirchk1 = "/temp/files/" . $batchid . "/" .$data[0] . ".doc";
$dirchk2 = "/temp/files/" . $batchid . "/" . $data[1] . ".doc";

if(is_file($filechk1) && (is_file($filechk2))) || (is_file($dirchk1) && (is_file($dirchk2))){
...

}

Answer



Incorrectly placed parentheses. Needs to be this.



if ((is_file($filechk1) && is_file($filechk2)) || (is_file($dirchk1) && is_file($dirchk2)))

javascript - jQuery Ajax File Upload




Can I use the following jQuery code to perform file upload using POST method of an ajax request ?



$.ajax({
type: "POST",
timeout: 50000,
url: url,
data: dataString,
success: function (data) {
alert('success');

return false;
}
});


If it is possible, do I need to fill data part? Is it the correct way? I only POST the file to the server side.



I have been googling around, but what I found was a plugin while in my plan I do not want to use it. At least for the moment.


Answer



file upload is not possible through ajax.

You can upload file, without refreshing page by using IFrame.
you can check further detail here



UPDATE:



With XHR2, File upload through AJAX is supported. E.g. through FormData object, but unfortunately it is not supported by all/old browsers.



FormData support starts from following desktop browsers versions.
IE 10+,
Firefox 4.0+,

Chrome 7+,
Safari 5+,
Opera 12+



For more detail, see MDN link


casting - Why wasn&#39;t Tobey Maguire in The Amazing Spider-Man? - Movies &amp; TV

In the Spider-Man franchise, Tobey Maguire is an outstanding performer as a Spider-Man and also reprised his role in the sequels Spider-Man...