|
|
Home » Developing U++ » U++ Developers corner » Some new functions
Some new functions [message #29820] |
Sat, 20 November 2010 01:01 |
|
koldo
Messages: 3394 Registered: August 2008
|
Senior Veteran |
|
|
Hello
Here there are a few functions that could be added to U++:
inline bool Odd(int val) {return val%2;}
inline bool Even(int val) {return !Odd(val);}
inline int RoundEven(int val) {return Even(val) ? val : val+1;}
template<class T>
inline int Sign(T a) {return (a > 0) - (a < 0);}
inline const RGBA *GetPixel(const Image &img, int x, int y) {
return img + x + y*img.GetWidth();
}
inline RGBA *GetPixel(ImageBuffer &img, int x, int y) {
return img + x + y*img.GetWidth();
}
Best regards
Iñaki
|
|
|
|
|
Re: Some new functions [message #29827 is a reply to message #29822] |
Sat, 20 November 2010 20:55 |
|
Fun fact: I tried to come up with optimal solution for Even() and all of the following appear to have same speed as the one from Koldo (with gcc optimal+speed flag):inline bool even1(int val) {return !(val&1);}
inline bool even2(int val) {return ~val&1;}
inline bool even3(int val) {return !(val%2);}
Without the speed flag even2() seems to be slightly faster.
Also val%2 and val&1 for Odd() yields the same speed in both cases
However, the proposed RoundEven() function is suboptimal thanks to the branching. Even though it won't probably be used often, I would suggest faster version:inline int roundeven(int val) {return ((1+val)>>1)<<1;}
//for completeness also rounding to odd numbers:
inline int roundodd(int val) {return ((val>>1)<<1)+1;}
Regarding the image access: The img[y][x] is great, but still it would be nice to have a wrapper that would allow to put the arguments in (imho) more natural order. For example something like RGBA* Image::Get(int x,int y){return (*this)[y][x];}
Best regards,
Honza
|
|
|
|
|
|
|
|
|
|
|
Goto Forum:
Current Time: Fri Sep 20 11:38:42 CEST 2024
Total time taken to generate the page: 0.03946 seconds
|
|
|