问题:有一种方法可以在一台机器上运行相应的测试用例而在另一台机器上运行失败(详情如下).我认为代码有问题,导致它在一台机器上偶然工作.不幸的是我找不到问题.
请注意,std :: string和utf-8编码的使用是我没有实际影响的要求.使用C++方法会很好,但遗憾的是我找不到任何东西.因此使用C函数.
方法:
std::string firstCharToUpperUtf8(const string& orig) {
std::string retVal;
retVal.reserve(orig.size());
std::mbstate_t state = std::mbstate_t();
char buf[MB_CUR_MAX + 1];
size_t i = 0;
if (orig.size() > 0) {
if (orig[i] > 0) {
retVal += toupper(orig[i]);
++i;
} else {
wchar_t wChar;
int len = mbrtowc(&wChar, &orig[i], MB_CUR_MAX, &state);
// If this assertion fails, there is an invalid multi-byte character.
// However, this usually means that the locale is not utf8.
// Note that the default locale is …Run Code Online (Sandbox Code Playgroud) 一般来说,我在网上看到的与SSE/MMX相关的一切都是向量和数据的数学东西.但是,我正在寻找SSE优化的"标准函数"库,如Agner Fog提供的库,或GCC中的一些基于SSE的字符串扫描算法.
作为一个简单的概述:这些将是像memset,memcpy,strstr,memcmp BSR/BSF,即从SSE intrsuctions构建的stdlib-esque
我最好喜欢它们用于SSE1(正式MMX2)使用内在函数而不是汇编,但两者都很好.希望这不是一个广泛的范围.
更新1
经过一番搜索,我发现了一些有前途的东西,一个图书馆引起了我的注意
我还看到一篇关于一些矢量化字符串函数的文章(strlen,strstr strcmp).然而SSE4.2是这样我够不着的地方(如前面所说的,我想坚持到SSE1/MMX).
更新2
Paul R激励我做一些基准测试,遗憾的是,由于我的SSE汇编编码经验接近于zip,我使用了别人的(http://www.mindcontrol.org/~hplus/)基准测试代码并添加到其中.所有测试(不包括原始版本,即VC6 SP5),在VC9 SP1下编译,具有完整/自定义优化/arch:SSE功能.
第一次测试是我的家用机器(AMD Sempron 2200+ 512mb DDR 333),上限为SSE1(因此没有MSVC memcpy的矢量化):
comparing P-III SIMD copytest (blocksize 4096) to memcpy
calculated CPU speed: 1494.0 MHz
size SSE Cycles thru-sse memcpy Cycles thru-memcpy asm Cycles thru-asm
1 kB 2879 506.75 MB/s 4132 353.08 MB/s 2655 549.51 MB/s
2 kB 4877 598.29 MB/s 7041 414.41 MB/s …Run Code Online (Sandbox Code Playgroud) 是否可以将函数指针作为 const 参数传递?
我收到以下 gcc 错误:
警告:'f' [-Wimplicit-int] void execute(void (const *f)(void));
...编译此代码时:
#include <stdio.h>
void print(void);
void execute(void (const *f)(void));
int main(void)
{
execute(print); // sends address of print
return 0;
}
void print(void)
{
printf("const!");
}
void execute(void (const *f)(void)) // receive address of print
{
f();
}
Run Code Online (Sandbox Code Playgroud)
我想解析一个字符串,找到一(character, n)组中找到的前N个重复字符.
例如,for "ozzllluu"和sets("u"=> 2),("d"=> 2),("l"=> 3)和("r"=> 3)......我想找到"lll",因为它是3个字符,发生在两个"你"之前.
程序式解决方案:
Rebol []
seq-set: [#"u" 2 #"d" 2 #"l" 3 #"r" 3]
str: "ozzllluu"
lastchar: ""
cnt: 1
seq-char: ""
foreach char str [
either char = lastchar [
cnt: cnt + 1
if (select seq-set char) = cnt [
seq-char: char
break
]
][
cnt: 1
]
lastchar: char
]
either seq-char = "" [
print "no seq-char"
][
print join "seq-char " seq-char
]
Run Code Online (Sandbox Code Playgroud)
我如何使用parse规则做同样的事情? …
在我的计算机上,编译的可执行文件省略了在循环顶部执行"mov%2,%% ax"
当"添加%1,%% ax"取消注释时.
有人要进行双重检查或评论?
#include <stdio.h>
int main() {
short unsigned result, low ,high;
low = 0;
high = 1;
__asm__ (
"movl $10, %%ecx \n\t"
"loop: mov %2, %%ax \n\t"
// "add %1, %%ax \n\t" // uncomment and result = 10
"mov %%ax, %0 \n\t"
"subl $1, %%ecx \n\t"
"jnz loop"
: "=r" (result)
: "r" (low) , "r" (high)
: "%ecx" ,"%eax" );
printf("%d\n", result);
return 0;
}
Run Code Online (Sandbox Code Playgroud)
跟随生成的程序集
movl $1, %esi
xorl %edx, %edx
/APP
movl …Run Code Online (Sandbox Code Playgroud) 我编写了一个不可知函数,char以十六进制格式打印字符串。
void print_char2hex(void* cp) {
const char *arr = (char *) cp;
size_t i;
for(i = 0; i < strlen( arr ); i++) {
printf("%02X ", 0xFF & arr[i]);
}
}
Run Code Online (Sandbox Code Playgroud)
为什么以下代码无法编译?
void print_char2hex(const void *cp) {
size_t i;
for (i = 0; i < strlen( * (const char *) cp ); i++) {
printf("%02X ", 0xFF & cp[i]);
}
}
Run Code Online (Sandbox Code Playgroud)
这是错误消息:
passing argument 1 of strlen makes pointer from integer without a cast [-Wint-conversion]
Run Code Online (Sandbox Code Playgroud)