Register a SA Forums Account here!
JOINING THE SA FORUMS WILL REMOVE THIS BIG AD, THE ANNOYING UNDERLINED ADS, AND STUPID INTERSTITIAL ADS!!!

You can: log in, read the tech support FAQ, or request your lost password. This dumb message (and those ads) will appear on every screen until you register! Get rid of this crap by registering your own SA Forums Account and joining roughly 150,000 Goons, for the one-time price of $9.95! We charge money because it costs us money per month for bills, and since we don't believe in showing ads to our users, we try to make the money back through forum registrations.
 
  • Post
  • Reply
gibbed
Apr 10, 2006

sex offendin Link posted:

There is no generalized way to byte-swap IEEE 754 numbers anywhere, they are for all intents and purposes non-portable.
And the very few places I've seen that swap floats just swap their bytes as if they were uints.

Adbot
ADBOT LOVES YOU

BigRedDot
Mar 6, 2008

sex offendin Link posted:

There is no generalized way to byte-swap IEEE 754 numbers anywhere, they are for all intents and purposes non-portable.
That was my point?

Lexical Unit
Sep 16, 2003

gibbed posted:

This is nitpicking and platform specific, although the cerr thing is stupid, yes. :colbert:
I would like to see this.

It seemed mostly OK to me, although I wouldn't make it a template.

I'm not sure about Painless's point about "creating a T instance makes the byteswap a lot less generic." The function is already too generic because it accepts things like struct foo { int i; float f; };, std::complex<float>, int a[10], ...

Normally yes you might want to avoid using a default constructor unless necessary because there's no reason to enforce the type has a default constructor if you don't need to. But in this case I don't think ByteSwap() should work at all on non-integral types, which are all default-constructible (obviously).


Printing an error message is unacceptable as well. There's code that attempts to call ByteSwap() on long doubles, which can exceed (and usually do) 8 bytes in size. It's evident from the history of this file that it's intended to be the end-all to byteswaping for a wide swath of our codebase. That it's intended to "just work" on floating point types. It does not.


As for the runtime switching, I re-coded it as a compile-time switch by just using templates and am seeing a 4x to 16x speed improvement.

There are other issues, namely:

Nitpicky, but: "ByteSwap() - CamelCase" vs "swap_data() - lowercase_underscore"; just pick one.

Why should a for loop calling ByteSwap() be slower than a single call to swap_data()? It shouldn't, and it doesn't have to be. Testing has shown that swap_data() is up to 10x faster than a loop over calls to ByteSwap(). The code is loving lazy.

As I already said, the code is too generic. I actually had the guy who wrote the code in my office the other day and he was saying things like, "well it's a template so you can't construct what types it accepts at all! A template accepts everything, you know, that's why it's called a template." A simple application of traits types and the error message isn't necessary and ByteSwap() only compiles when it's being used correctly.

Another thing, did you notice the signature of swap_data()? void swap_data(T* data, int n). What isn't immediately obvious is that n here is supposed to be the size of T * number of Ts. Why not avoid the issue entirely with a better signature? Or at least make the parameter size_t size or something for fucks sake.
code:
// ugly:
vector<int> ints = get_some_ints ();
swap_data (&ints.front (), ints.size () * sizeof (ints.front ()));

// why not this instead:
vector<int> ints = get_some_ints ();
swap_data (ints.begin (), ints.end ());

// and this too:
size_t n = get_n ();
int* ints = get_n_ints (n);
swap_data (ints, ints + n);

sex offendin Link posted:

There is no generalized way to byte-swap IEEE 754 numbers anywhere, they are for all intents and purposes non-portable.
What about putting them in a union with an unsigned integer type, byteswapping the integer and returning the integer? The integer can go over whatever I/O it needs to and be byteswapped back into a floating point type on the other end.

I'm honestly asking, wouldn't this work?

Lexical Unit fucked around with this message at 04:06 on Jul 8, 2009

Avenging Dentist
Oct 1, 2005

oh my god is that a circular saw that does not go in my mouth aaaaagh

Lexical Unit posted:

What about putting them in a union with an unsigned integer type, byteswapping the integer and returning the integer? The integer can go over whatever I/O it needs to and be byteswapped back into a floating point type on the other end.

I'm honestly asking, wouldn't this work?

Accessing a value stored in a union from a type other than the one that it was stored with is a violation of strict-aliasing (with the exception of accessing via char*). Furthermore, y'all need to shut up about the original code being unsafe since it casts to a char* and that's guaranteed to be safe by the ISO standard.

EDIT: Since I don't want to write an essay on this, I will defer to a good source: http://www.cellperformance.com/mike_acton/2006/06/understanding_strict_aliasing.html

Avenging Dentist fucked around with this message at 04:21 on Jul 8, 2009

Lexical Unit
Sep 16, 2003

So if you leveraged char* to do the swapping rather than a union hack, and for floating point types you returned an accommodating integer type rather than storing a byteswapped floating point value back into a floating point type, would you be reasonably safe then?

Probably due to lovely websites found through google:

quote:

the compiler very probably will return the double return value in an FPU register, and if not then, at some point the value may be loaded into an FPU register. This means the swapped result is loaded into an FP register, which then means the FP unit will take a look at the number, and then the fun begins.

when you swap,You move some of the mantissa data into the sign/exponent slot and the sign/exponent data becomes part of a mantissa. And guess what? Not all values of sign/exponent/mantissa are valid floating point numbers according to IEEE 754.

When a floating point unit is faced with an invalid value, it may actually change the value, or worse, throw an exception.
\/\/

Lexical Unit fucked around with this message at 04:29 on Jul 8, 2009

Avenging Dentist
Oct 1, 2005

oh my god is that a circular saw that does not go in my mouth aaaaagh
There is literally nothing in the C99/C++03 standards* that says you can't manipulate a floating point value from casting/type-punning to a char*. I don't see why people are obsessing about this for.

* Probably the same for other versions of C and C++, but I don't have access to the specs.

Avenging Dentist
Oct 1, 2005

oh my god is that a circular saw that does not go in my mouth aaaaagh

Lexical Unit posted:

Probably due to lovely websites found through google:

Then yeah keep the swapped data in a char buffer.

(Also note that I didn't say anything about manipulating the value of a floating-point number in a predictable way. :angel:)

(Also also you should really be using type traits to disable that if it's not a primitive type).

Avenging Dentist fucked around with this message at 04:57 on Jul 8, 2009

Lexical Unit
Sep 16, 2003

Nevermind. I really need some sleep. What you say makes perfect sense.

Lexical Unit fucked around with this message at 05:50 on Jul 8, 2009

Avenging Dentist
Oct 1, 2005

oh my god is that a circular saw that does not go in my mouth aaaaagh

Lexical Unit posted:

Edit: Wait, I think I misread that. Did you mean should or souldn't? I already mentioned that I used type traits to clamp that poo poo down.

I said primitive, not integral. (There's also no reason that byteswapped data needs to be returned with the same type as the original; in fact it would be more sensible to return it as a char buffer, since you really shouldn't be manipulating byteswapped data as though it were of the original type, floating-point or not. What I am saying here is "make it work with integral and floating point values and nothing else".)

tef
May 30, 2004

-> some l-system crap ->
hello world in prolog :v:

code:
#!/usr/bin/env swipl -q -t start -f 

%% chmod +x pharmacon.prolog && ./pharmacon.prolog 

%% hello world program in stack form

helloworld([
    pushz,inc,inc,dup,dup,mul,mul,dup,dup,inc,mul,
    prc,pushz,inc,inc,inc,dup,dup,mul,mul,add,inc,
    inc,prc,add,dec,prc,prc,inc,inc,inc,prc,pushz,
    inc,inc,dup,dup,mul,dup,inc,mul,inc,mul,inc,inc,
    prc,pushz,inc,inc,inc,dup,inc,mul,swap,sub,prc,
    swap,dup,pushz,inc,inc,dup,dup,mul,mul,add,prc,
    swap,pushz,inc,inc,inc,add,dup,dec,dec,dec,prc,
    pop,prc,swap,pop,pushz,inc,inc,dup,inc,mul,swap,
    sub,prc,pushz,inc,inc,dup,dup,mul,mul,swap,sub,
    prc,pop,inc,prc,pop,pushz,inc,inc,dup,dup,dup,
    inc,add,mul,add,inc,prc,dec,dec,dec,prc,pop
]).


%% peano arithmetic
z(z).
inc(S,s(S)).
dec(s(S),S).

add(z,S,S).
add(s(S),A,O) :- add(S,s(A),O).

sub(S,z,S).
sub(s(S),s(A),O) :- sub(S,A,O).

mul(_,z,z).
mul(z,_,z).
mul(S,s(z),S).
mul(s(z),S,S) :- \+ S = s(z).
mul(A,B,O) :- dec(A,A1), mul(A1,B,O1), add(B,O1,O).

num(0,z).
num(N,s(S)) :- \+ var(N), N> 0, N1 is N -1, num(N1,S).
num(N,s(S)) :- \+ var(S), num(N1,S), N is N1 + 1.

%% basic linked list/cons cells/

nil(nil).
pair(A,B,pair(A,B)).
head(pair(A,_),A).
tail(pair(_,B),B).

pairs([],nil).
pairs([H|T],pair(H,T1)) :- pairs(T,T1).

%% stack machine

run(P,O) :- pairs(P,Pa), nil(S), eval(Pa,S,Oa), stackback(Oa,O),!.

eval(I,S,S) :- nil(I).
eval(I,S,O) :- head(I,H), exec(H,S,O1),!, tail(I,T), eval(T,O1,O).

exec(pushz,S,O) :- z(Z),pair(Z,S,O).
exec(pop,S,O) :- tail(S,O).
exec(inc,S,O) :- pair(A,B,S), inc(A,A1), pair(A1,B,O).
exec(dup,S,O) :- head(S,A), pair(A,S,O).
exec(prc,S,S) :- head(S,A), num(N,A), writef('%n',[N]).
exec(prn,S,S) :- head(S,A), num(N,A), writef('%w',[N]).
exec(dec,S,O) :- pair(A,B,S), dec(A,A1), pair(A1,B,O).
exec(add,S,O) :- pair(A,At,S), pair(B,T,At), add(A,B,AB), pair(AB,T,O).
exec(swap,S,O) :- pair(A,At,S), pair(B,T,At), pair(A,T,Bt), pair(B,Bt,O).
exec(sub,S,O) :- pair(A,At,S), pair(B,T,At), sub(A,B,AB), pair(AB,T,O).
exec(mul,S,O) :- pair(A,At,S), pair(B,T,At), mul(A,B,AB), pair(AB,T,O).
exec(A,_,_) :- writef("Unknown %w\n",[A]), !, fail.

stackback(nil,[]).
stackback(pair(H,T),[H1|T1]) :- num(H1,H),stackback(T,T1).

start :- helloworld(H), run(H,[]).

Zombywuf
Mar 29, 2008

tef posted:

hello world in prolog :v:

code:
#!/usr/bin/env swipl -q -t start -f 
...

Looks reasonable to me, where's the horror?

Filburt Shellbach
Nov 6, 2007

Apni tackat say tujay aaj mitta juu gaa!

Zombywuf posted:

where's the horror?

It's prolog. :banjo:

tef
May 30, 2004

-> some l-system crap ->

Sartak posted:

It's prolog. :banjo:

:v::hf::smith:

BattleMaster
Aug 14, 2000

So did the guy who came up with Prolog think that assembly language wasn't ugly enough or something? Because for a moment I thought that was extremely macro-heavy assembly.

tef
May 30, 2004

-> some l-system crap ->
http://alain.colmerauer.free.fr/ArchivesPublications/HistoireProlog/19november92.pdf

it turns out you don't need to re-implement natural numbers or lists, or a stack machine to write hello world in prolog.

hint: hello world in prolog is actually :- writef("%w",["Hello, world!"]). or something.

UberJumper
May 20, 2007
woop
So i saw this today from ERDAS's software SDK...

code:
#define __(a) a
What the gently caress?

I was curious because they prototype their functions like:

<type> <function name> __((<parameter list>))

Dessert Rose
May 17, 2004

awoken in control of a lucid deep dream...
I'm reimplementing a tool that fills space. I don't have source for the original tool, but that's what Reflector is for!...

code:
if (Regex.IsMatch(input, "[0-9,]+ bytes free"))
 {
       MatchCollection matchs = Regex.Matches(input, "[0-9,]+ bytes free");
       this.lblFreeSpaceLabel.Text = "Free Space:\n" + matchs[0].ToString().Substring(0, matchs[0].Length - 11).ToString() + " bytes";
       string str = Regex.Replace(matchs[0].ToString().Substring(0, matchs[0].Length - 11).ToString(), ",", "");
       this.lMUFreeSpace = Convert.ToInt64(str);
}
...I don't think I'm missing much.

ed: I think my favorite part of this is that he knows to use regexes, but seems unaware of how exactly he should be using them.

Dessert Rose fucked around with this message at 23:59 on Jul 8, 2009

Vanadium
Jan 8, 2005

UberJumper posted:

So i saw this today from ERDAS's software SDK...

code:
#define __(a) a
What the gently caress?

I was curious because they prototype their functions like:

<type> <function name> __((<parameter list>))

It is so they can change it to #define __(a) () to support C compilers that do not implement function prototypes, I suspect.

RussianManiac
Dec 27, 2005

by Ozmaugh

Vanadium posted:

It is so they can change it to #define __(a) () to support C compilers that do not implement function prototypes, I suspect.

what C compiler doesn't support function prototypes, and how would that macro help? Do you mean so they can change it to a function pointer?

BigRedDot
Mar 6, 2008

RussianManiac posted:

what C compiler doesn't support function prototypes, and how would that macro help? Do you mean so they can change it to a function pointer?
Old, old, old pre-ansi "K&R" compilers. And no it doesn't change it to a function pointer. The macro would be redefined as
code:
#define __(a) ( )
You know, so if your compiler doesn't support prototypes, you won't give it one. Although I seem to recall always seeing "P_(a)" from cproto as the conventional spelling, not "__(a)"

RussianManiac
Dec 27, 2005

by Ozmaugh

BigRedDot posted:

Old, old, old pre-ansi "K&R" compilers. And no it doesn't change it to a function pointer. The macro would be redefined as
code:
#define __(a) ( )
You know, so if your compiler doesn't support prototypes, you won't give it one. Although I seem to recall always seeing "P_(a)" from cproto as the conventional spelling, not "__(a)"

I must be missing something. If you do #define __(a) ( ) then all you are doing is just getting rid of arguments to a function prototype, right, but the return type and name should still be there assuming that it looks like

<type> <function name> __((<parameter list>))

vvvvvvvvvvvvvvvvvvv

I am confused. Isn't a prototype just a function declaration without its implementation?

RussianManiac fucked around with this message at 07:50 on Jul 9, 2009

Avenging Dentist
Oct 1, 2005

oh my god is that a circular saw that does not go in my mouth aaaaagh

RussianManiac posted:

I must be missing something. If you do #define __(a) ( ) then all you are doing is just getting rid of arguments to a function prototype, right, but the return type and name should still be there assuming that it looks like

<type> <function name> __((<parameter list>))

Yes, hence K&R syntax. (Except that functions don't have prototypes in K&R C.)

tef
May 30, 2004

-> some l-system crap ->

Sartak posted:

It's prolog. :banjo:

This would explain the response I got to this childish job advert.

I wrote them an email berating them for using popular languages like erlang and haskell and so sent them a prolog response to elicit the following reply:

quote:

FAIL: We were trying to figure out why this is a fail.
But the fact is simply that we just hate you.

tripwire
Nov 19, 2004

        ghost flow

tef posted:

This would explain the response I got to this childish job advert.

I wrote them an email berating them for using popular languages like erlang and haskell and so sent them a prolog response to elicit the following reply:

What the hell kind of job advertisement is that!?

TSDK
Nov 24, 2003

I got a wooden uploading this one

tef posted:

This would explain the response I got to this childish job advert.
code:
Job Advert                  ## FAIL
Company                     ## FAIL
Potential Customers         ## Shun them. FAIL
Their Bank Manager          ## Hates them... FAIL
5+ Ninjas at dole office    ## FAIL NINJA-FAIL

Sharrow
Aug 20, 2007

So... mediocre.
The best part is that they're a PHP shop, according to an email response posted on Reddit.

Zombywuf
Mar 29, 2008

TSDK posted:

code:
Job Advert                  ## FAIL
Company                     ## FAIL
Potential Customers         ## Shun them. FAIL
Their Bank Manager          ## Hates them... FAIL
5+ Ninjas at dole office    ## FAIL NINJA-FAIL

code:
"Hello World".fluf() ## Cooler... Fail.
Yes, considering that cool is a fail.

Vanadium
Jan 8, 2005

RussianManiac posted:

I am confused. Isn't a prototype just a function declaration without its implementation?

It is, basically. But in C before prototypes, you would not give the parameter list when declaring functions.

Dessert Rose
May 17, 2004

awoken in control of a lucid deep dream...
Haha, I love their "fail" page. Someone submitted a GA program that evolves hello world.

tripwire
Nov 19, 2004

        ghost flow

Ryouga Inverse posted:

Haha, I love their "fail" page. Someone submitted a GA program that evolves hello world.

Clearly not 1337 enough for these badass renegades of code

Dijkstracula
Mar 18, 2003

You can't spell 'vector field' without me, Professor!

the dd and sed one is the only amusing "win" on that page; all the others are just spamming anonymous functions.

Avenging Dentist
Oct 1, 2005

oh my god is that a circular saw that does not go in my mouth aaaaagh
Once you see someone use "fail" as a noun you can pretty much ignore everything else they say since they are human scum.

spiritual bypass
Feb 19, 2008

Grimey Drawer

Avenging Dentist posted:

Once you see someone use "fail" as a noun you can pretty much ignore everything else they say since they are human scum.

I bet they have lolcat posters on the wall at their office.

Zombywuf
Mar 29, 2008

Avenging Dentist posted:

Once you see someone use "fail" as a noun you can pretty much ignore everything else they say since they are human scum.

http://www.askoxford.com/concise_oed/fail?view=uk

Avenging Dentist
Oct 1, 2005

oh my god is that a circular saw that does not go in my mouth aaaaagh

Believe it or not, the OED doesn't tell you which words are stupid.

Triple Tech
Jul 28, 2006

So what, are you quitting to join Homo Explosion?
I'm thinking it's not an actual job posting, just an elaborate troll. How could anyone take themselves seriously after asking for all that?

Zombywuf
Mar 29, 2008

Avenging Dentist posted:

Believe it or not, the OED doesn't tell you which words are stupid.

However you have just called dictionary corner human scum. This is tantamount to a declaration of war on the UK.

tripwire
Nov 19, 2004

        ghost flow
I was looking through some of those "fail"s and couldn't figure out how one of them was getting this text it operates on:
code:
import sys
import StringIO

stdout, sys.stdout = sys.stdout, StringIO.StringIO()

import this

sys.stdout, stdout = stdout, sys.stdout

import random
import subprocess
import time
import platform

cmd = 'clear'
if 'win' in platform.system().lower():
    cmd = 'cls'
cls = lambda : subprocess.Popen(cmd, shell=True).communicate()

sleeptime = 0.01
stdout.seek(0)
data = stdout.read()
inds = [1, 35, 42, 61, 122, 475, 512, 549, 568, 620, 696]
print data
time.sleep(sleeptime)                                                  ## Oh goodie, animated...
while [i for i,x in enumerate(data) if i not in inds and x.strip()]:   ## FAIL ( we accept only as statement modifier )
    x = random.choice([i for i,x in enumerate(data) if i not in inds and x.strip()])
    data = ''.join([data[:x], ' ', data[x+1:]])                        ## Looks like you know about slicing, suppose that's good.
    cls()
    print data
    time.sleep(sleeptime)
while len(data) != len(inds):                                          ## FAIL ( statement modifier.??? nope, so FAIL )
    x = random.choice([i for i,x in enumerate(data) if not x.strip()])
    data = ''.join([data[:x], data[x+1:]])
    cls()                                                              ## Reminds us of non-ninja programmer.
    print data
    time.sleep(sleeptime)

cls()
print data.replace('-',' ')
raw_input('\nPress ENTER to continue')
ugh those comments

Anyway at least I learned a neat trick: in python, "import this" prints the Zen of Python :)

Avenging Dentist
Oct 1, 2005

oh my god is that a circular saw that does not go in my mouth aaaaagh

Zombywuf posted:

However you have just called dictionary corner human scum. This is tantamount to a declaration of war on the UK.

Um yeah, I'm an American, we have a habit of fighting the UK.

Adbot
ADBOT LOVES YOU

haveblue
Aug 15, 2005



Toilet Rascal
Didn't the Oxford dictionary catch a lot of poo poo for adding internet slang to their most recent edition?

  • 1
  • 2
  • 3
  • 4
  • 5
  • Post
  • Reply