|
What if I don't want to recompile? Could we maybe fetch 2 from a web server somewhere?
|
# ? May 12, 2016 22:41 |
|
|
# ? Jun 5, 2024 18:20 |
|
Jsor posted:What if I don't want to recompile? Could we maybe fetch 2 from a web server somewhere? That's way too specific. You need an XML and a protocol-agnostic API library which itself links to a generic io library which loads it. Otherwise, what if you're not prepared for Web 3.9 or IPv24?!
|
# ? May 12, 2016 22:54 |
|
What we need is for CIPM to develop a universal way for us to calculate 2 based on some universal constant.
|
# ? May 12, 2016 23:03 |
|
xzzy posted:What we need is for CIPM to develop a universal way for us to calculate 2 based on some universal constant. Maybe we could calculate 2 from the number two? No wait that's stupid sorry.
|
# ? May 12, 2016 23:05 |
|
xzzy posted:What we need is for CIPM to develop a universal way for us to calculate 2 based on some universal constant. Oh, that's easy: divide the circumference of a circle of unit radius by its area. Jsor posted:Maybe we could calculate 2 from the number two? No wait that's stupid sorry. Well, you could always write "1+1". That's always 2. Except when it's 0. poo poo. poo poo. drat.
|
# ? May 12, 2016 23:08 |
|
You guys are overthinking this againcode:
code:
|
# ? May 12, 2016 23:23 |
|
Klades posted:You guys are overthinking this again That has a race condition. What if the numbers two or zero change between the user's input and the test?
|
# ? May 12, 2016 23:55 |
|
Documentation horror. When I read the hardware manual for the power management unit 3 months ago: quote:...blah blah this *ALWAYS* happens regardless of whether you write blah blah before entering the low power state blah blah... posted:No no... it doesn't really work that way. It doesn't always do that, actually it's really a debug thing you shouldn't ever use... hmm... yeah... [long pause] It actually works like this *draws diagram that explains everything with perfect clarity. it's not in the manual*
|
# ? May 13, 2016 01:25 |
|
Ah, hardware manuals. Reminds me of when I had this really obscure C code, written by the manufacturer's engineers, from which I was trying to understand how to implement I/O communications with their card. I see this loop, and think it's supposed to be a delay, perhaps expecting CPU's clock speeds as they were in the 1990's or whenever this came out to provide enough to allow the hardware to respond to a signal. There was also some read from another port that I didn't quite understand. Well, after playing around with the various tools for real-time timing in Windows XP (hint: not very good nor reliable), and trying to extend the loop by how much I thought a modern CPU would be faster than an old one (wasteful and also did not work), I run into an old Linux hardware HOWTO which explains what that was: turns out the loop was completely irrelevant. Instead, if you read from the parallel port (regardless of whether anything is plugged in), you know you're going to get a 1ms delay. BAM! Now I can write my interfacing code. I get the impression that there's a lot of cargo cult in the coding world.
|
# ? May 13, 2016 01:44 |
|
Absurd Alhazred posted:Well, you could always write "1+1". That's always 2. code:
JawnV6 fucked around with this message at 02:29 on May 13, 2016 |
# ? May 13, 2016 02:27 |
|
TheresaJayne posted:I don;t know how bad this actually is, I personally don;t like this but its a rule we have at work - no Magic numbers so this Concern over magic numbers is solving entirely the wrong problem, because that is terrible code either way. code:
|
# ? May 13, 2016 05:12 |
|
KernelSlanders posted:Concern over magic numbers is solving entirely the wrong problem, because that is terrible code either way. Only on a vOv fucked around with this message at 06:35 on May 13, 2016 |
# ? May 13, 2016 05:22 |
|
The fourth byte is low and the first byte is high, so that there int is big-endian. So that cast is almost certain to produce something that will have to be byte-swapped. Also, in addition to being a probable strict-aliasing violation, casting to int* is quite likely to violate alignment rules unless you're exceptionally careful about your buffering. Also the original code was clearly Java, so it would perhaps not quite be fair to criticize it for not using C features, even if the features were being used correctly.
|
# ? May 13, 2016 05:41 |
|
vOv posted:Only on a little-endian system, and even then I'm pretty sure this violates strict aliasing. As long as data is a char array, there's no strict aliasing issue. Bigger problem is the original post seemed to be about Java (static final int as constants).
|
# ? May 13, 2016 05:41 |
|
It also involved loading bytes individually from a hardware register.
|
# ? May 13, 2016 05:48 |
|
eth0.n posted:As long as data is a char array, there's no strict aliasing issue. This is one of those big open questions, because on the one hand it would obviously be ridiculous to invalidate the huge amount of existing code that writes structured stuff into char arrays, and on the other hand that is not actually what the standard says. The C standard has a formal concept called an object, which is basically space for a value in memory. An object has an "effective type", and you're allowed to access it through an l-value of that type, or the same type with the wrong signedness, or a character type, all ignoring qualifiers. It's not always clear what constitutes an object, except that a declaration definitely creates an object whose effective type is its declared type. Note in particular that a declared variable of type char[1024] is an object with an effective type of char[1024], and the rule doesn't work in reverse: you're allowed to access an int object using a char l-value, but not a char object using an int l-value. The C committee has been really, really bad about coming up with a sensible rule here mostly because they can't agree about what should be forbidden. They can definitely agree that dumb things should be forbidden, and that reasonable things should be allowed, but they are really not sure about how to define either of those things in a way that actually permits either optimization or the writing of code.
|
# ? May 13, 2016 05:59 |
|
There's no alignment issue. The following works just fine.code:
|
# ? May 13, 2016 06:02 |
|
It isn't guaranteed to. The start of any particular array on the stack is likely to be aligned, but in real code you are probably reading the next four bytes, not the first four bytes, and that is not particularly likely to be aligned. That said, most architectures are pretty forgiving about alignment, especially x86, so it is not something that will always bite you. On the other hand, the compiler knows that, too, and will generally emit an unaligned load on those architectures the same way, while actually emitting the right sequence on platforms with stronger requirements; so really, you might as well get the alignment rules right. Edit: your example definitely only works on an architecture that doesn't enforce small alignments. rjmccall fucked around with this message at 06:17 on May 13, 2016 |
# ? May 13, 2016 06:15 |
|
KernelSlanders posted:There's no alignment issue. The following works just fine. "Works on my machine" is not the same as "works fine". (Especially if you're testing this on your desktop machine with a processor that will happily fix up your unaligned accesses for you.)
|
# ? May 13, 2016 06:17 |
|
vOv posted:Only on a little-endian system, and even then I'm pretty sure this violates strict aliasing. are there any important computers left that aren't little-endian
|
# ? May 13, 2016 06:31 |
|
Suspicious Dish posted:are there any important computers left that aren't little-endian IBM z/Architecture, and ARM can be switched to big-endian data mode (pretty sure instructions are fixed little-endian though). If SPARC counts, then it can switch endianness per-instruction!
|
# ? May 13, 2016 06:37 |
|
KernelSlanders posted:Concern over magic numbers is solving entirely the wrong problem, because that is terrible code either way. Doesn't quite work i am afraid, the data is actually 14k long and these are 4 bytes as a byte array, 4 bytes as int, 4 bytes as int, 4 bytes as int, (27 bits, 236 bits, 20 bits, 1 bit, 2 bits 5 bits, ) repeated n times Also that looks like C this is Java we are talking about Oh 0 1 and 2 are not magic numbers but 3 is (we know a song about that) https://www.youtube.com/watch?v=daWObuUptrQ TheresaJayne fucked around with this message at 06:39 on May 13, 2016 |
# ? May 13, 2016 06:37 |
|
Suspicious Dish posted:are there any important computers left that aren't little-endian I got it backwards, it actually only works on big-endian machines.
|
# ? May 13, 2016 06:40 |
|
Kazinsal posted:IBM z/Architecture so no then
|
# ? May 13, 2016 06:45 |
|
The cast also assumes that int is 32 bits. It's bad and the original code was correct and good.
|
# ? May 13, 2016 08:49 |
|
Why use x % 2 == 0 instead of x & 1 == 0? Am I missing the joke or something?
|
# ? May 13, 2016 09:24 |
|
dougdrums posted:Am I missing the joke or something? Um, I guess so? Posters are making a mountain out of a molehill in regards to how to "properly" code a parity check. That's the joke, such as it is.
|
# ? May 13, 2016 09:40 |
|
Absurd Alhazred posted:It also involved loading bytes individually from a hardware register.
|
# ? May 13, 2016 12:24 |
|
Kazinsal posted:IBM z/Architecture, and ARM can be switched to big-endian data mode (pretty sure instructions are fixed little-endian though). IBM POWER boxes running AIX and stuff are still big-endian, so is SPARC (and if you're counting mainframes then SPARC definitely counts, it's the largest of the remaining server RISCs by market share), ARM instructions can be and originally only were big-endian, and indeed most RISCs were originally big-endian and even now can be run in either endianness - it's not like it's a hard thing to do in hardware if you have fixed-width instructions. But yeah, in terms of sheer numbers it's either little-endian ARM or x86 by a landslide these days. Big-endian is definitely legacy at this point. Edit: oh, and Java is big-endian (e.g. the format of constants in class files, when you write integers out over RMI, etc), too. feedmegin fucked around with this message at 13:04 on May 13, 2016 |
# ? May 13, 2016 13:01 |
|
Jabor posted:"Works on my machine" is not the same as "works fine". (Especially if you're testing this on your desktop machine with a processor that will happily fix up your unaligned accesses for you.) Yup, here is what happens when I run your 'works fine' code, KernelSlanders - bash-2.05$ /opt/csw/bin/gcc test.c bash-2.05$ ./a.out Bus Error (core dumped) bash-2.05$ uname -m sun4u
|
# ? May 13, 2016 13:08 |
|
rjmccall posted:Note in particular that a declared variable of type char[1024] is an object with an effective type of char[1024], and the rule doesn't work in reverse: you're allowed to access an int object using a char l-value, but not a char object using an int l-value. The last time I had to do something like this I used union that was basically: code:
|
# ? May 13, 2016 13:42 |
|
Hammerite posted:Um, I guess so? Posters are making a mountain out of a molehill in regards to how to "properly" code a parity check. That's the joke, such as it is. I've just seen a lot of smart people do it (irl), so I've always been a little confused. But after compiling both on windows and diassembling them, the latter is more efficient. Modulus emits a cdq and xor and then subs from a register that should always be zero in this case, but still uses and imm to get the result. Using and directly results in and, test, jne, which is what you'd want... I don't know why people keep doing it. It's been a bit of a pet peeve. I thought it made no difference at this point, but I guess it still does.
|
# ? May 13, 2016 14:12 |
|
dougdrums posted:I don't know why people keep doing it. Because it's a perfectly acceptable way of determining if a number is even.
|
# ? May 13, 2016 14:38 |
|
dougdrums posted:I've just seen a lot of smart people do it (irl), so I've always been a little confused. But after compiling both on windows and diassembling them, the latter is more efficient. Modulus emits a cdq and xor and then subs from a register that should always be zero in this case, but still uses and imm to get the result. Have you tried compiling with ... any level of optimization at all?
|
# ? May 13, 2016 14:45 |
|
dougdrums posted:I've just seen a lot of smart people do it (irl), so I've always been a little confused. But after compiling both on windows and diassembling them, the latter is more efficient. Modulus emits a cdq and xor and then subs from a register that should always be zero in this case, but still uses and imm to get the result. When you say it "makes a difference," do you mean one that an end-user (or even your profiler) notices while using a fully-optimized build? It sure makes a difference in code readability, and not a good difference.
|
# ? May 13, 2016 14:53 |
|
dougdrums posted:I've just seen a lot of smart people do it (irl), so I've always been a little confused. But after compiling both on windows and diassembling them, the latter is more efficient. Otherwise, the main reason folks use "x % 2 == 0" is because it's a pretty direct encoding of the question "is x divisible by 2?". Conversely "x & 1 == 0" is more asking "is the first (one-indexed) bit of x unset?" Yes the results are equivalent*, but it's more about viewing x as an integer rather than a bag of bits. * equivalent in two's complement. They are not equivalent in ones' complement for negative integers.
|
# ? May 13, 2016 15:01 |
|
ExcessBLarg! posted:We might have talked about it before, but what's your opinion on unions in C99 and type punning? My opinion is that obvious reinterpretation through unions is an important language feature that it's important to not mis-compile. My read of the standards here is . Apparently the C++ committee is trying to improve this, but their current efforts are basically just attempting to clean up the formal objects model and improve the wording on unions; the intended language rule will still be that a union has one active member and you must take action to change it before you are allowed to read from a different member.
|
# ? May 13, 2016 16:32 |
|
A horror story for a day such a this... It twas Friday the 13th and the hands of the clock did tick down to the time of doom, a deadline that hung over the heads of the team in much a way the sword of Damocles did sit over the head of the commoner. Slowly the completion of the build drew near... 98%... 99%... 100% but wait! The indicator was red like the blood that spurts from a fatal wound to the heart from the dagger of a hated foe. Feverishly the eyes of those gathered dropped to the message and saw horror upon horror the truth of their dire situation. quote:Code Coverage Failures: Foolishly in haste test classes had not been written for many a month to cover their work to ensure that it was divine and functioned as the Masters had insisted. This problem was taken to the Masters and their response was swift. quote:There is always something with Salesforce, can't we throw them some cash or something to remove this stupid limit The developers entreated that it could not be done and if they could have more time to write the missing test classes everything would be better for all. The Masters looked upon them with scorn, test classes were just a techie thing to waste time away from the important task of making new things for the Holy Warriors of Sales to use to sell as much product as they could. The developers were beaten harshly with words about timelines, profit margins and how technical debt was not a real thing. Finally the leader of the developers agreed to do what he could to make the build work and deploy on time. As he slowly trudged, his pride fatally wounded and instructed his team to write test classes that execute the new code but to make all asserts pass without doing any real tests. After this command he retreated to his enclave and removed a bottle filled with a dark brown liquid, pouring it into a glass and weeping softly as if he believed no one could here him. BUT READER THIS IS NO FICTION! That lead developer works where I do and this very interaction happened on this very day! As we speak the developers write their sinful tests to meet a deadline that has no value other than for the Masters to get a bigger bonus this quarter. Let this tale be a cautionary one and never specifiy time in your development plans to write test classes and always role it into your normal dev time to hide it from the eyes of those who weild MBAs as one would a sword at the head of common sense.
|
# ? May 13, 2016 17:39 |
|
dougdrums posted:I've just seen a lot of smart people do it (irl), so I've always been a little confused. But after compiling both on windows and diassembling them, the latter is more efficient. Modulus emits a cdq and xor and then subs from a register that should always be zero in this case, but still uses and imm to get the result. i don't know what bad compiler you used but clang 3.7 emits the right thing unless I set -O0
|
# ? May 14, 2016 03:51 |
|
|
# ? Jun 5, 2024 18:20 |
|
BigPaddy posted:A horror story for a day such a this... This is amazing. I have my own testing story to share - while trying to figure out why some eight-line unit tests were taking upwards of four seconds each to run, I found out that some gibbering idiot had implemented a view model in such a way that when it was instantiated it automatically spun up a background thread that immediately blocked and was then forgot about forever. Forty unit tests that each create one of these view models, forty bored threads doing nothing, and the .NET task scheduler reduced to a mess of blood and tears because it has no idea what the gently caress I'm trying to do. In a twist you all saw coming, the gibbering idiot was me.
|
# ? May 14, 2016 06:34 |