大内存区域上的按位或 [英] Bitwise OR on large memory regions

查看:123
本文介绍了大内存区域上的按位或的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

亲爱的读者,


或两个大内存区域的有效方式是什么?


我对申请特别感兴趣此操作为两个大的
字符数组。我知道memset和memcopy让你初始化或复制

这个数组的速度非常快,但有没有相似的函数用于按位OR?


感谢您的帮助!


Best,

Oswald

[见 http://www.gotw.ca/resources/clcm.htm 有关的信息]

[comp.lang。 C ++。主持。第一次海报:做到这一点! ]

解决方案

os**********@web.de 写道:


有两种大内存区域的有效方法是什么?


我特别感兴趣的是将这个操作应用于两个大的
字符数组。我知道memset和memcopy可以让你初始化或者b / b
非常快速地复制这个数组,但是

是否有类似的函数按位OR?



我会用


模板<类Tstruct binOR:std :: binary_function< T,T,T {

T operator()(const T& x,const T& y)const {

return x | y;

}

};

...

std :: transform(begin1,end1,begin2 ,输出,二进制);


并希望编译器能够优化这些东西。


别忘了包括< ; functionaland< algorithm> ;.


V

-

请在回复时删除资本''A'电子邮件

我没有回复最热门的回复,请不要问


Oswald发布:
< blockquote class =post_quotes>
有两种大内存区域的有效方法是什么?



这里有一个想法:

如果是unsigned int没有陷阱表示,那么或者记忆就好像你正在使用一组无符号整数。


#include< cstddef>

#include< cassert>


unsigned * ArrayOR(unsigned const * p1,unsigned const * p2,std :: size_t len)

{

断言(p1);

断言(p2);

断言(len);


unsigned * const retval = new unsigned [len];


unsigned * p = retval;


do * p ++ = * p1 ++ | * P2 ++; while( - len);


返回retval;

}

然后执行以下操作:


void * MemOR(void const * const p1arg,

void const * const p2arg,

std :: size_t const bytes)

{

unsigned char const * p1 = p1arg;

unsigned char const * p2 = p2arg;


/ *正确对齐指针,然后

稍后清理任何多余的字节。 * /

ArrayOR(p1,p2,a,amount_int); / *必要的演员* /

/ *现在处理开头的额外字节

和结尾。 * /

}


-


Frederick Gotham



" Frederick Gotham" < fg ******* @ SPAM.com在留言中写道

新闻:eK ******************* @ news.indigo .ie ...


Oswald发布:


> OR有两种有效的方法大内存区域?




这里有一个想法:


如果是unsigned int没有陷阱表示,那么或者记忆就好像你正在使用一组无符号整数。


#include< cstddef>

#include< cassert>


unsigned * ArrayOR(unsigned const * p1,unsigned const * p2,std :: size_t len)

{

断言(p1);

断言(p2);

断言(len);


unsigned * const retval = new unsigned [len];


unsigned * p = retval;


do * p ++ = * p1 ++ | * P2 ++;而( - LEN);



Yuk。这是我讨厌在C ++中看到的东西。一切都在一个

行,充满了++和 - 。为什么不制作一个for循环,或者至少可读的循环?b
?优化器可能会将它解析为

结尾的相同代码。


>

返回retval ;

}


然后做类似的事情:


void * MemOR(void const * const p1arg,

void const * const p2arg,

std :: size_t const bytes)

{

unsigned char const * p1 = p1arg;

unsigned char const * p2 = p2arg;


/ *正确对齐指针,然后

清理任何稍后的额外字节。 * /


ArrayOR(p1,p2,a,amount_int); / *必要的演员* /


/ *现在处理开始时的额外字节

和结尾。 * /

}



你在ArrayOR中有新内存,但没有把它分配给任何东西。

除了没有使用结果,它是内存泄漏,呃?


如果海报想要就地OR,一个简单的循环就足够了,而不是

a函数:


for(int i = 0; i< len; ++ i)

a [i] | = b [i];


快速,简单,明显。我的那种C ++。 :-)


-Howard


Dear Reader,

what efficient ways are there to OR two large memory regions?

I''m especially interested in applying this operation to two large
character arrays. I know memset and memcopy lets you initialise or copy
this arrays very fast, but are there similar functions for bitwise OR?

Thanks for your help!

Best,
Oswald
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

解决方案

os**********@web.de wrote:

what efficient ways are there to OR two large memory regions?

I''m especially interested in applying this operation to two large
character arrays. I know memset and memcopy lets you initialise or
copy this arrays very fast, but are there similar functions for
bitwise OR?

I would use

template <class Tstruct binOR : std::binary_function<T,T,T{
T operator()(const T& x, const T& y) const {
return x | y;
}
};
...
std::transform(begin1, end1, begin2, output, binOR);

And hope that the compiler is capable of optimizing that stuff.

Don''t forget to include <functionaland <algorithm>.

V
--
Please remove capital ''A''s when replying by e-mail
I do not respond to top-posted replies, please don''t ask


Oswald posted:

what efficient ways are there to OR two large memory regions?


Here''s one idea:
If an "unsigned int" has no trap representations, then OR the memory as if
you''re OR-ing an array of unsigned integers.

#include <cstddef>
#include <cassert>

unsigned *ArrayOR(unsigned const *p1,unsigned const *p2,std::size_t len)
{
assert(p1);
assert(p2);
assert(len);

unsigned *const retval = new unsigned[len];

unsigned *p = retval;

do *p++ = *p1++ | *p2++; while(--len);

return retval;
}
Then do something like:

void *MemOR(void const *const p1arg,
void const *const p2arg,
std::size_t const bytes)
{
unsigned char const *p1 = p1arg;
unsigned char const *p2 = p2arg;

/* Align the pointers correctly, then
clean up any extra bytes later on. */
ArrayOR(p1,p2,a,amount_int); /* Casts necessary */
/* Now deal with the extra bytes at the start
and at the end. */
}

--

Frederick Gotham



"Frederick Gotham" <fg*******@SPAM.comwrote in message
news:eK*******************@news.indigo.ie...

Oswald posted:

>what efficient ways are there to OR two large memory regions?



Here''s one idea:
If an "unsigned int" has no trap representations, then OR the memory as if
you''re OR-ing an array of unsigned integers.

#include <cstddef>
#include <cassert>

unsigned *ArrayOR(unsigned const *p1,unsigned const *p2,std::size_t len)
{
assert(p1);
assert(p2);
assert(len);

unsigned *const retval = new unsigned[len];

unsigned *p = retval;

do *p++ = *p1++ | *p2++; while(--len);

Yuk. This is the kind of thing I hate to see in C++. Everything on one
line, full of ++ and --. Why not make a for loop, or a loop that''s more
readable at least? The optimizer will likely resolve it to the same code in
the end.

>
return retval;
}
Then do something like:

void *MemOR(void const *const p1arg,
void const *const p2arg,
std::size_t const bytes)
{
unsigned char const *p1 = p1arg;
unsigned char const *p2 = p2arg;

/* Align the pointers correctly, then
clean up any extra bytes later on. */
ArrayOR(p1,p2,a,amount_int); /* Casts necessary */
/* Now deal with the extra bytes at the start
and at the end. */
}

You''ve new''d memory there in ArrayOR, but not assigned it to anything.
Besides not using the results, it''s a memory leak, eh?

If the poster wanted an in-place OR, a simple loop would suffice, instead of
a function:

for (int i = 0; i < len; ++i)
a[i] |= b[i];

Quick, easy, and obvious. My kind of C++. :-)

-Howard


这篇关于大内存区域上的按位或的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆