Return Styles: Pseud0ch, Terminal, Valhalla, NES, Geocities, Blue Moon. Entire thread

Broadcom Open-Sources Wireless Drivers

Name: Anonymous 2010-09-09 15:26

The ternary operator is dissapointed.


/*
Description: This function saturate input 32 bit number into a 16 bit number.
If input number is greater than 0x7fff then output is saturated to 0x7fff.
else if input number is less than 0xffff8000 then output is saturated to 0xffff8000
else output is same as input.
*/
int16 qm_sat32(int32 op)
{
    int16 result;
    if (op > (int32) 0x7fff) {
        result = 0x7fff;
    } else if (op < (int32) 0xffff8000) {
        result = (int16) (0x8000);
    } else {
        result = (int16) op;
    }
    return result;
}

Name: SoFreshAndSoClean 2010-09-09 15:30


/*
Description: This function saturate input 32 bit number into a 16 bit number.
If input number is greater than 0x7fff then output is saturated to 0x7fff.
else if input number is less than 0xffff8000 then output is saturated to 0xffff8000
else output is same as input.
*/
int16 qm_sat32(int32 op) {
 return (op > (int32) 0x7fff) ? 0x7fff : (op < (int32) 0xffff8000) ? (int16) (0x8000) : (int16) op;
}

Name: Anonymous 2010-09-09 15:32

Isn't (int16) 0x8000 just 0?

Name: Anonymous 2010-09-09 15:33

hahaha fags

But anyway, I feel that the conditions don't make sense.

Name: Anonymous 2010-09-09 17:12

ENTERPRISE QUALITY.

I can't be bothered to b.i.o.u it.

Name: Anonymous 2010-09-09 17:18

>>5
Seems to be conforming to the standard for OSS quality.

Name: Anonymous 2010-09-09 18:11

>>3
no.

(int16)0x8000 is 0x8000

Name: Anonymous 2010-09-10 10:11

>>2
How is that better? The first form is easier to read and the object code will be the same in both cases.

Name: mage 2010-09-11 7:40

>>8
The first form is easier to read and the object code will be the same in both cases.

The object code will be the same for both if your compiler is smart. Do you really need variable assigning? As for readability, I will answer this as soon as I stop laughing.

Name: Anonymous 2010-09-11 8:56

>>9
Idiot.

Name: Anonymous 2010-09-11 11:04

>>9
Code is for programmers to read and only incidentally for computers to execute. >>1 is written in C rather than assembly for the sake of the programmer. >>2 doesn't take much effort to read, but I would that most programmers that have a stake in reading the driver code would have require a larger cognitive effort to comprehend it.

Name: sage 2010-09-11 11:35

The optimized code of >>1 and >>2 will be the same.

Name: Anonymous 2010-09-11 13:31

The ternary operator ultimately ends up being pretty useless. It's always the case when using the ternary operator that you lose readability for the sake of reducing the number of lines written, while gaining sometimes literally nothing in terms of efficiency and certainly not clarity. It's one of those nice programming ideas that ends up not being very useful in practice. It seems more suitable for functional programming languages than for imperative languages like C.

Name: Anonymous 2010-09-11 14:24

>>1,2
Your code is needlessly complicated.

int16 qm_sat32(int32 op)
{
    return (int16)x == x ? x : (x >> 31) ^ 0x7fff;
}

Name: Anonymous 2010-09-11 14:41

>>14
Your code is too obfuscated for enterprise synergistic use.

Name: Anonymous 2010-09-11 15:40

>>13
Better to just get rid of statements and have everything be expressions, including conditionals, which isn't necessarily tied to functional programming.

Name: Anonymous 2010-09-11 18:36

>>13
It's always the case when using the ternary operator that you lose readability
Only if you habitually underuse it. Once you get over the surprise of seeing it, the ternary operator nearly always provides a net readability gain by reducing the number of obfuscatory temporary variables needed.

Name: Anonymous 2010-09-11 19:47

expressions > statements

Name: Anonymous 2010-09-11 20:05

This seems to be clearer:

int16_t qm_sat32(int32_t n) {
  if      (n < INT16_MIN) return INT16_MIN;
  else if (n > INT16_MAX) return INT16_MAX;
  else                    return n;
}

Name: Anonymous 2010-09-11 21:31

>>19
You know what's even more clear?

[code]int16_t qm_sat32(int32_t n) {
    if (n < INT16_MIN) return INT16_MIN;
    if (n > INT16_MAX) return INT16_MAX;
    return n;
}[code]

Name: Anonymous 2010-09-11 21:32

>>20
int16_t qm_sat32(int32_t n) {
    if (n < INT16_MIN) return INT16_MIN;
    if (n > INT16_MAX) return INT16_MAX;
    return n;
}


FUCK!!

Name: Anonymous 2010-09-11 22:36

>>21
Even better, as most are not accustomed to reading inline if statements!

int16_t qm_sat32(int32_t n) {
    if (n < INT16_MIN)
        return INT16_MIN;
    if (n > INT16_MAX)
        return INT16_MAX;
    return n;
}

Name: Anonymous 2010-09-11 23:35

>>22
IMHO, >>21 is the ``clearest'' of them all.

Name: Anonymous 2010-09-12 11:10

int16 qm_sat32(int32 op)
{
  if op > int16.MAX_VALUE:
    return int16.MAX_VALUE
  elif op < int16.MIN_VALUE:
    return int16.MIN_VALUE
  else:
    return (int16) op
}

Name: Anonymous 2010-09-12 13:45

>>24
Terrible!

Name: Anonymous 2010-09-12 16:40

Short qm_sat32(Integer op)
{
    if      (op.compareTo(new Integer((int)Short.MAX_VALUE)) > 0)
        return Short.MAX_VALUE;
    else if (op.compareTo(new Integer((int)Short.MIN_VALUE)) < 0)
        return Short.MIN_VALUE;
    else
        return new Short((short)op.intValue());
}

Name: Anonymous 2010-09-12 18:48

>>26

!BEAUTIFUL

Name: Anonymous 2010-09-12 23:50

>>24
EXPERT CYTHON PROGRAMMER

Name: Anonymous 2010-09-13 1:12

>>28
Oh God, it actually exists!...

Name: Anonymous 2010-09-13 4:30

int16_t qm_sat32(int32_t op)
{ return (int16_t)MIN(MAX(op, INT16_MIN), INT16_MAX); }

Name: Anonymous 2010-09-13 10:47

#define CLAMP(x, low, high)  (((x) > (high)) ? (high) : (((x) < (low)) ? (low) : (x)))
#define qm_sat32(op) ((int16_t) CLAMP(op, INT16_MIN, INT16_MAX))

Name: Anonymous 2010-09-13 13:25

CLAMP MY ANUS

Name: Anonymous 2010-09-13 13:40

>>31

int w = CLAMP(x++, y++, z++);


You are a bad programmer and you should feel bad.

Name: Anonymous 2010-09-13 13:44

>>33
Isn't that what documentation is for. ( ≖‿≖)

Name: Anonymous 2010-09-13 14:47

>>33
You're stupid for using an unsafe macro.

Name: Anonymous 2010-09-13 15:33

>>35
You're unsafe for using a stupid macro

Name: Anonymous 2010-09-13 15:52

You're macro for using an unsafe stupid.

Name: Anonymous 2010-09-13 16:28

>>35
You're stupid for using a macro in an unsafe way.
The fact that you're stupid enough to insert a chainsaw into your anus doesn't mean that everyone who uses a chainsaw is stupid.

Name: Anonymous 2010-09-13 16:40

>>38
The problem is, >>33 might not have know it was a macro. Perhaps he was just told in the documentation that it is a thing that clamps its first argument to between its second and third arguments. He then does CLAMP(x++, y++, z++);. Is this his fault?

Name: Anonymous 2010-09-13 17:27

>>33
#define CLAMP(x, low, high)  {\
  void* x2 = x, low2 = low, high2 = high; \
  (((x2) > (high2)) ? (high2) : (((x2) < (low2)) ? (low2) : (x2)))\
}

Newer Posts
Don't change these.
Name: Email:
Entire Thread Thread List